Raghu Java Programming
Raghu Java Programming
nextInt();
nextFloat();
nextShort();
nextDouble();
nextLong();
nextByte();
next(); String
nextLine(); String
import java.util.Scanner;
class mainRunner
{
Public static void main( String args[] )
{
Scaner scn = new Scanner(System.in);
System.out.println(“Enter a interger value”);
int no = scn.nextint();
if(no>=0)
System.out.println( no + “is a Positive integer”);
else
System.out.ptintln(no + “is a Negative integer”);
}
}
2. WAP to check if your number is even or odd.
import java.util.Scanner;
public class EvenOdd {
}
}
double si = getSI(amt,r,y);
//double si = (amt*r*y)/100;
System.out.println("The Simple Interest is " + si);
}
static double getSI(long p,double r,double t)
{
double si = (p*r*t)/100;
return si;
}
}
System.out.println(area(5));
}
static double area ( double side)
{
return (side*side);
}
}
7. WAP to read the 4 subject marks from the user and display the result.
In any subject marks < 35 so fail; percentage> 80% so distinction,>60 first
class,>50 2nd class. Otherwise just pass
ALTERNATE 1: WITHOUT USING METHOD
import java.util.Scanner;
public class Exam {
public static void main(String[] args) {
double m1,m2,m3,m4,perc;
Scanner sc = new Scanner(System.in);
System.out.println("Enter student marks in 1st subject out of 100
");
m1= sc.nextDouble();
System.out.println("Enter student marks in 2nd subject out of 100
");
m2=sc.nextDouble();
System.out.println("Enter student marks in 3rd subject out of 100
");
m3=sc.nextDouble();
System.out.println("Enter student marks in 4th subject out of 100
");
m4=sc.nextDouble();
if(m1<35 || m2<35 || m3<35 ||m4<35)
{
System.out.println("Student failed");
}
else
{
perc = ((m1+m2+m3+m4)*100)/400;
if(perc>80)
{
System.out.println("Distinction");
}
else if (perc >60)
{
System.out.println("First Class");
}
else if (perc>50)
{
System.out.println("Second Class");
}
else
{
System.out.println("Fail");
}
}
marks();
}
8. WAP to read integer value from the user and print if it is even or odd.
import java.util.Scanner;
public class EvenOdd {
if (rs == true)
System.out.println("Even");
else
System.out.println("ODD");
}
static boolean isEven(int x){
if (x%2==0)
return true;
else
return false;
}
}
//System.out.println(c);
}
static double cube ( double no)
{
return (no*no*no);
}
}
13. WAP to define a method to perform arithmetic operation on any two values.
import java.util.Scanner;
public class EvenOddMethd {
if (rs == true)
System.out.println("Even");
else
System.out.println("ODD");
}
static boolean isEven(int x){
if (x%2==0)
return true;
else
return false;
}
}
while(i<=n)
{
System.out.println(i);
i+=2;
}
System.out.println(sum);
}
}
17. WAP to define a method to how many even numbers between 1 to n.
import java.util.Scanner;
public class EvenUptoN {
while(i<=10)
{
System.out.println(n+"*"+i+"="+n*i);
i++;
}
}
Here I’m defining two different class. 1st is to define a “FactorialN” method
and 2nd is to define the main method. While executing this in command prompt,
both the class has to be complied then only o/p will come.
}
26 WAP to define a method to count how many odd digits present in a number.
import java.util.Scanner;
public class OddDigitsInNumner {
while(no1!=0)
{
int rem=no1%10;
sum=sum+fact(rem);
no1=no1/10;
if (sum==no)
return true;
else
return false;
}
31. WAP to define a method to check your number is Armstrong number or not.
import java.util.Scanner;
public class ArmstrongNumb {
}
32. WAP to check your number is Disarium number or not.(Sum of power of
individual digit by their position).
import java.util.Scanner;
public class Disarium {
if(sum==no)
System.out.println("Number is Perfect Number");
else
System.out.println("Number is not Perfect Number");
}
34. WAP to define a method to check the number is happy number or not.
import java.util.Scanner;
public class HappyNumber {
if(no==1||no==7)
System.out.println("The number is a Happy Number");
else
System.out.println("The number is not a Happy Number");
}
while (count<=no)
{
c=a+b;
System.out.println(c);
a=b;
b=c;
count++;
}
}
while (c<no)
{
System.out.println(c);
c=a+b;
a=b;
b=c;
}
}
import java.util.Scanner;
sum+=c;
c=a+b;
a=b;
b=c;
}
return sum;
}
5 types of Literals
Integer ---- 0-9 (byte, int-4byte, long-8byte, short-2byte)
Floating point ---- 0-9 + . (float-4byte, double-8byte)
Char ---- ‘A’,’6’,’8’,’?’ (2byte)
String ---- “SSP”,”Programming”
Boolean ---- true/false (1bit)
}
System.out.println("Binary value is "+bin);
}
}
return hx;
}
}
return con;
}
}
ASCII VALUES
0-9 48-57
A-Z 65-90
a-z 97-122
10101
01010
10101
01010
10101
public class Pattern_1 {
METHOD 2:
public class Pattern_1 {
00000
11111
00000
11111
00000
12345
67891
23456
78912
34567
1*1*1
0*0*0
1*1*1
0*0*0
1*1*1
1*0*1
1*0*1
1*0*1
1*0*1
System.out.println();
}
}
ABCDE
ABCDE
ABCDE
ABCDE
ABCDE
System.out.println();
}
}
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
12345
12345
12345
12345
12345
1*2*3
1*2*3
1*2*3
1*2*3
1*2*3
*****
****
***
**
*
1
12
123
1234
12345
12345
1234
123
12
1
*
***
*****
*******
*********
}
70.WAP to print following pattern.
A
AB
ABC
ABCD
ABCDE
A
BC
DEF
GHIJ
KLMNO
}
72. WAP to print the following pattern.
A
BC
CDE
DEFG
EFGHI
FGHIJK
1
23
456
7891
23456
METHOD 1
public class Pattern_1 {
}
METHOD 2
*
**
***
****
*****
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
for(j=1;j<=i;j++)
{
System.out.print((char)k);
k++;
}
System.out.println();
}
}
}
79. WAP to print the following pattern.
1
00
111
0000
11111
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
81. WAP to print the following pattern.
*
***
*****
*******
*********
for(j=1;j<=2*i-1;j++)
{
System.out.print("*");
}
System.out.println();
}
}
for(j=1;j<=n-i+1;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
83. WAP to print the following pattern.
*****
****
***
**
*
for(j=1;j<=n-i+1;j++)
{
System.out.print("*");
}
System.out.println();
}
}
for(j=1;j<=i;j++)
{
System.out.print((char)k);
k++;
}
System.out.println();
}
}
}
85. WAP to print the following pattern.
A
AB
ABC
ACBD
ABCDE
for(j=1;j<=k;j++)
{
System.out.print("*");
}
System.out.println();
if(i<=n/2)
{
s--;
k=k+2;
}
else
{
s++;
k=k-2;
}
}
}
1
1*2
1*2*3
1*2*3*4
1*2*3
1*2
1
int l=1;
for(j=1;j<=k;j++)
{
if(j%2==0)
System.out.print("*");
else
{
System.out.print(l);
l++;
}
}
System.out.println();
if(i<=n/2)
{
s--;
k=k+2;
}
else
{
s++;
k=k-2;
}
}
}
for(j=1;j<=k;j++)
{
System.out.print(l);
}
System.out.println();
if(i<=n/2)
{
s--;
l++;
k=k+2;
}
else
{
s++;
l--;
k=k-2;
}
}
}
l=1;
for(j=1;j<=k;j++)
{
System.out.print(l);
if(j<=k/2)
l++;
else
l--;
}
System.out.println();
if(i<=n/2)
{
s--;
k=k+2;
}
else
{
s++;
k=k-2;
}
}
}
}
92. WAP to print the following pattern.
A
ABC
ABCDE
ABCDEFG
ABCDE
ABC
A
l=1;
for(j=1;j<=k;j++)
{
System.out.print((char)(j+l+63));
}
System.out.println();
if(i<=n/2)
{
s--;
k=k+2;
}
else
{
s++;
k=k-2;
}
}
}
for(j=1;j<=k;j++)
{
System.out.print((char)(l+64));
}
System.out.println();
if(i<=n/2)
{
s--;
l++;
k=k+2;
}
else
{
s++;
l--;
k=k-2;
}
}
}
l=1;
for(j=1;j<=k;j++)
{
System.out.print((char)(l+64));
if(j<=k/2)
l++;
else
l--;
}
System.out.println();
if(i<=n/2)
{
s--;
k=k+2;
}
else
{
s++;
k=k-2;
}
}
}
}
96. WAP to print the following pattern.
*
* *
* *
* *
* *
* *
*
for(j=1;j<=k;j++)
{
if(j==1||j==k)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
if(i<=n/2)
{
s--;
l++;
k=k+2;
}
else
{
s++;
l--;
k=k-2;
}
}
}
for(j=1;j<=k;j++)
{
if(j==1||j==k||j==k/2+1)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
if(i<=n/2)
{
s--;
l++;
k=k+2;
}
else
{
s++;
l--;
k=k-2;
}
}
}
for(j=1;j<=k;j++)
{
if(j==1||j==k||j==k/2+1||i==n/2+1)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
if(i<=n/2)
{
s--;
l++;
k=k+2;
}
else
{
s++;
l--;
k=k-2;
}
}
}
for(j=0;j<n;j++)
{
if(j==0||j==n-1||j==i||j==n-1-i||i==0||i==n-1)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
for(j=0;j<n;j++)
{
if(j==0||j==n-1||j==n/2||i==n/2||i==0||i==n-1)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
*******
** * **
* *** *
*******
* *** *
** * **
*******
for(j=0;j<n;j++)
{
if(j==0||j==n-1||j==n/2||i==n/2||i==0||i==n-
1||j==i||j==n-i-1)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
for(j=0;j<n;j++)
{
if(j==n/2||i==n/2||j==i||j==n-i-1)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
}
}
}
if(i==n/2+1||j==n/2+1||j==1&&i<=n/2||j==n&&i>n/2||i==1&&j>n/2||i==n&&j<
=n/2)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
106. WAP to print the following pattern.
* * * * * *
* * * * * *
* * * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * * *
* * * * * *
* * * * * *
for(j=1;j<=n;j++)
{
if(i<3&&j<3||i>n-3&&j<3||i==3||i==n-
2||j==3||j==n-2||j<3&&i>n-3||j>n-2&&i<3||j>n-2&&i>n-2)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
ARRAYS:
Array is an object in JAVA. Stored in heap memory.
int ar[]; //decleration
int []ar;
int[] ar;
ar = new int[5];//allocation
ar[3]=45;
ar[1]=42;
index value memory address
0 0 8000
1 42 8004
2 0 8008
3 45 8012
4 0 8016
psvm(__)
{
//<datatype> <identifier>[];
int a; //normal variable;
int ar[]; // or int []ar; int[] ar;
ar=new int[5]; //allocation
ar[2]=45;
ar[3]=67; //initialization
SOP(“number of elements: ”+ar.length);
For(int i=0;i<n;i++)
{
SOP(i+”-”+ar[i]);
}
}
import java.util.Scanner;
public class SumAndAverage {
108. WAP to read array elements from the user and define a method a method to
return the average of n elements.
import java.util.Scanner;
public class AvgMethod {
109. WAP to define a method to return how many even numbers in the passed
array.
110. WAP to define a method to return how many odd numbers present in the
array.
import java.util.Scanner;
public class EvenOddArray {
111. WAP to count and display how many negative and positive numbers present
in your array.
import java.util.Scanner;
public class NegPosArray {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements ");
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
// int c[]=new int[3];
int c[]=positive(arr);
System.out.println("Negative Numbers are : "+c[0]);
System.out.println("Positive Numbers are : "+c[1]);
System.out.println("Number of '0's are : "+c[2]);
}
112. WAP to read ‘n’ people’s weights based on that display lift is working
or not. Max capacity of your lift is 800kg.
import java.util.Scanner;
public class WorkingLift {
import java.util.Scanner;
public class DeleteDuplicates {
int no=remove(arr);
display(arr,no);
}
114. WAP to insert the element in the existing array to the specified index.
import java.util.Scanner;
public class InsertAtIndex {
import java.util.Scanner;
public class DeleteAtIndex {
import java.util.Scanner;
public class DeleteAtIndex {
int[] readArray()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements in the array");
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
return arr;
}
if(no==1)
brr=delIndex(arr);
else if(no==2)
brr=delElement(arr);
else
{
System.out.println("Wrong Choice");
arr=delete(arr);
}
return brr;
if(count==0)
{
System.out.println("Wrong choice");
System.out.println("Element not present in the Array");
crr=delElement(brr);
}
else
{
for(int i=0;i<n-1;i++)
{
if(i<ind)
crr[i]=brr[i];
else
crr[i]=brr[i+1];
}
}
return crr;
}
import java.util.Scanner;
public class PrimeInArray {
117. WAP count how many digits in the array (digits not number).
import java.util.Scanner;
public class DigitsInArray {
118. WAP check how many numbers are divisible by 4,3and 5 in the array.
import java.util.Scanner;
public class DivisibilityInArray {
}
119. WAP to define a method to return how many palindrome are there in the
array.
import java.util.Scanner;
public class PalindromeInArray {
120. WAP to define a method to return how many perfect numbers are there in
the array.
import java.util.Scanner;
public class PerfectNoInArray {
import java.util.Scanner;
public class CreateDuplicateArray {
import java.util.Scanner;
public class Combine2Arrays {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int arr[]=readArray(1);
int brr[]=readArray(2);
int crr[]=combine(arr,brr);
display(crr);
}
return crr;
}
122. WAP to add unique elements and return from the array.
import java.util.Scanner;
public class AddUniqueElements {
if(find==0)
{
sum+=arr[i];
}
}
return sum;
}
123. Mrs. Sofia as imported container of shoes. The container has different
size of shoes and needs to know the second highest shoes size in the
container. Could you help Sofia by writing a method to find the second
highest size of shoes in the container?
import java.util.Scanner;
public class Problem123 {
import java.util.Scanner;
public class Problem123 {
125. WAP to define a method to display the pair of elements who’s sum is
equal to n.
import java.util.Scanner;
public class PairSumToN {
}
}
import java.util.Scanner;
public class ZigzagArray
{
public static void main(String[] args)
{
int a[]=readArr();
int b[]=readArr();
int c[]=zigzag(a, b);
System.out.println("zigzag array is ");
for(int i=0;i<c.length;i++)
{
System.out.println(c[i]);
}
}
if(b1<b.length)
{
c[i]=b[b1];
b1++;
i++;
}
}
return c;
}
import java.util.Scanner;
public class MergeArrayInSortedForm
{
public static void main(String[] args)
{
int a[]=readArr();
int b[]=readArr();
int c[]=merge(a,b);
System.out.println("Merge Sorted Array is : ");
for(int i=0;i<c.length;i++)
{
System.out.println(c[i]);
}
}
static int[] merge(int[] a, int[] b)
{
int c[]=new int[a.length+b.length];
for(int i=0,m=0,n=0;i<c.length;i++)
{
if(m<a.length&&n<b.length)
{
if(a[m]<b[n])
{
c[i]=a[m];
m++;
}
else
{
c[i]=b[n];
n++;
}
}
else if(m<a.length)
{
c[i]=a[m];
m++;
}
else
{
c[i]=b[n];
n++;
}
}
return c;
}
static int[] readArr()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the how many elements: ");
int n= sc.nextInt();
int ar[]=new int[n];
System.out.println("Enter "+ n+" values ");
for(int i=0;i<n;i++)
{
ar[i]= sc.nextInt();
}
return ar;
}
}
import java.util.Scanner;
public class OccurenceOfElement
{
public static void main(String[] args)
{
int a[]=readArr();
occurence(a);
}
static int[] readArr()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the how many elements: ");
int n= sc.nextInt();
int ar[]=new int[n];
System.out.println("Enter "+ n+" values ");
for(int i=0;i<n;i++)
{
ar[i]= sc.nextInt();
}
return ar;
}
static void occurence(int a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
int c=1;
for(int j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
c++;
a[j]=a[n-1];
n--;
j--;
}
}
System.out.println(a[i]+" is repeated by "+c+" times");
}
}
129. WAP to first and second biggest element from the array without sorting.
import java.util.Scanner;
public class TwoBiggestElementInArray {
130. WAP to calculate the sum and average of the even element of the array.
import java.util.Scanner;
public class SumAndAverageInArray {
131. WAP to find the first and second smallest element of the array.
import java.util.Scanner;
public class TwoSmallestElementInArray {
import java.util.Scanner;
public class ReverseArrayElement {
import java.util.Scanner;
public class SwapAdjacentEle {
134. WAP to swap the first half elements of an array with the second half of
an array.
import java.util.Scanner;
public class SwapHalfArray {
import java.util.Scanner;
public class NumberToSentence {
static String
one[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten
","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Ei
ghteen","Nineteen"};
static String
two[]={"","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","N
inty"};
nToWord(n/10000000%100,"Crore ");
nToWord(n/100000%100," Lakh");
nToWord(n/1000%100," Thousand");
nToWord(n/100%10," Humdred");
nToWord(n%100,"");
}
Class:
public class Problem136 {
int id;
String name;
double per;
void display(int i)
{
System.out.println("Details of "+(i+1)+" Student is ");
System.out.println(id+" "+name+" "+per);
}
137. WAP to enter the info of employees and display the detail of the
employee with the least salary.
Class:
public class Problem137
{
int id;
String name;
double sal;
public Problem137(int id, String name, double salary)
{
this.id = id;
this.name = name;
this.sal = salary;
}
void display(int i)
{
System.out.println("Details of "+(i+1)+" Employee is ");
System.out.println(id+" "+name+" "+sal);
}
void display()
{
System.out.println("Details of Employee is ");
System.out.println(this.id+" "+this.name+" "+this.sal);
}
}
Main Method:
import java.util.Scanner;
public class MainProblem137 {
Problem137 eh=emp[0];
for (int i = 0; i < emp.length; i++)
{
if(eh.sal<emp[i].sal)
{
eh=emp[i];
}
}
eh.display();
}
}
138. WAP to count number of vowels, consonant, digits, special characters,
capital letters, small letters in the given string.
import java.util.Scanner;
public class Problem138 {
for(int i=0;i<st.length();i++)
{
char ch = st.charAt(i);
if(ch>='A'&&ch<='Z')
{
nu++;
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
nv++;
else
nc++;
}
else if(ch>='a'&&ch<='z')
{
nl++;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
nv++;
else
nc++;
}
else if(ch>='0'&&ch<='9')
nd++;
else
ns++;
}
System.out.println("Number of Vowels : "+ nv);
System.out.println("Number of Consonant : "+ nc);
System.out.println("Number of Digits : "+ nd);
System.out.println("Number of Upper case Letters : "+ nu);
System.out.println("Number of Lower case Letters : "+ nl);
System.out.println("Number of Special Characters : "+ns);
}
import java.util.Scanner;
public class Problem139 {
c=0;
while(i<st.length()&&st.charAt(i)!=' ')
{
c++;
i++;
}
if(c>0)
{
wc++;
System.out.println(wc+"--->"+c);
}
}
}
140. WAP to display how many characters in each words along with the word.
import java.util.Scanner;
public class Problem140 {
for(int i=0;i<st.length();i++)
{
int c=0;
String t="";
while(i<st.length()&&st.charAt(i)!=' ')
{
t=t+st.charAt(i);
c++;
i++;
}
if(c>0)
{
System.out.println(t+" waor has "+c+" words");
}
}
}
import java.util.Scanner;
public class Problem141 {
142. WAP to convert every word first character to capital and remaining
characters to lower.
import java.util.Scanner;
public class Problem142 {
import java.util.Scanner;
public class Problem143 {
144. WAP to count how many even length words present in your sentence.
import java.util.Scanner;
public class Problem144 {
import java.util.Scanner;
public class Problem145 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String st=sc.nextLine();
char ch[]=st.toCharArray();
int sum=0;
for (int i = 0; i < ch.length; i++)
{
if(ch[i]>='1'&&ch[i]<='9')
{
sum=sum+ch[i]-48;
}
}
System.out.println("Sum is : "+sum);
}
import java.util.Scanner;
public class Problem146 {
if(ch[i]=='A'||ch[i]=='E'||ch[i]=='I'||ch[i]=='O'||ch[i]=='U'||ch[i]=='
a'||ch[i]=='e'||ch[i]=='i'||ch[i]=='o'||ch[i]=='u')
{
c++;
}
i++;
}
}
System.out.println("Total Number of words starting with vowel are
: "+c);
}
import java.util.Scanner;
public class Problem147 {
import java.util.Scanner;
public class Problem148 {
return st;
}
149. WAP to read two strings and define a method to check those two strings
are Anagram are not. E.g. s1=keep s2=peek, s1=listen s2=silent, s1=debit card
s2=bad credit, s1=mother in law s2=Hitler mother. Ignore space and case
sensitivity.
import java.util.Scanner;
public class Problem149 {
st1=sort(st1);
st2=sort(st2);
boolean t=check(st1,st2);
if(t==true)
{
System.out.println("Strings are Anagram");
}
else
System.out.println("Strings are not Anagram");
}
150. WAP to count number of occurrence of each character in the given string.
import java.util.Scanner;
public class Problem150 {
}
151. WAP to convert string to number.
import java.util.Scanner;
public class Problem151 {
import java.util.Scanner;
public class Problem152 {
import java.util.Scanner;
public class Problem153 {
boolean u=check(fs,ss);
if(u==true)
{
System.out.println("Second Word is Present");
}
else
System.out.println("Second Word is not Present");
int c=countWord(fs,ss);
System.out.println("Second Word is Present "+c+" times");
for(int i=0;i<c1.length;i++)
{
int j=0,k=i;
while(i<c1.length&&j<c2.length&&c1[k]==c2[j])
{
k++;
j++;
}
if(j==c2.length)
return true;
}
return false;
CLASS:
import java.util.Scanner;
return days;
}
}
MAIN METHOD:
import java.util.Scanner;
public class Problem154Main {
Time Class:
import java.util.Scanner;
public class Time
{
int sec,min,hr;
public Time(int sec, int min, int hr) {
super();
if(hr==12)
{
hr=0;
}
this.sec = sec;
this.min = min;
this.hr = hr;
}
static Time readTime()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Time(hr::min::sec)");
int hr=sc.nextInt();
int min=sc.nextInt();
int sec=sc.nextInt();
return new Time(sec,min,hr);
}
int getTime()
{
int s=0;
s=(hr*60*60)+(min*60)+sec;
return s;
}
}
Main Method :
2-D ARRAY
int a[][];
int [][]a;
int[][] a;
int mat[][];
mat[1][1]=456;
SOP(“Number of rows:”+mat.length);
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
SOPrint(ar[i][j];
}
SOPrintln();
}
In following questions I have made a separate class for Matrix Input() and
Matrix Output(). I have made object in each of the following question to read
and display the Matrix.
import java.util.Scanner;
public class Matrix {
int[][] readMatrix()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and columns");
int r=sc.nextInt();
int c=sc.nextInt();
156. WAP to define a method to add two matrix. Also print the biggest element
in the added matrix.
}
}
}
}
161. WAP to define a method to reverse the row or column element in the
matrix.
import java.util.Scanner;
if(n==1)
{
arr=reverseRows(arr);
}
else if(n==2)
arr=reverseColumn(arr);
else
System.out.println("Wrong Choice");
m.displayMatrix(arr);
1 2 3
4 5 6
7 8 9
90 Degree Right
7 4 1
8 5 2
9 6 3
90 Degree Left
3 6 9
2 5 8
1 4 7
import java.util.Scanner;
import javax.sql.rowset.RowSetWarning;
164. WAP to display the biggest element from the diagonal (Primary and
Secondary).
int pbig=arr[0][0];
int sbig=arr[arr.length-1][0];
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
{
if(i==j)
{
if(arr[i][j]>pbig)
pbig=arr[i][j];
}
if(i+j==arr.length-1)
{
if(sbig<arr[i][j])
{
sbig=arr[i][j];
}
}
}
}
165. WAP to display the matrix element in the clockwise spiral order.
1 2 3
4 5 6
7 8 9
To
1 2 3 6 9 8 7 4 5
if(n%2!=0)
System.out.print(arr[n/2][n/2]+" ");
}
}
166. WAP to display the matrix element in the anti-clockwise spiral order.
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
To
1 6 1 6 1 2 3 4 5 0 5 0 5 4 3 2 7 2 7 8 9 4 9 8 3
if(n%2!=0)
System.out.print(arr[n/2][n/2]+" ");
}
}