//Assignment No:1
Set A
b) Write a program to calculate perimeter and area of reactangle.
Code:
π
- import java.util.*;
- public class rect
- {
- int l, b, area, perimeter;
- void accept()
- {
- Scanner sc =new Scanner(System.in);
- System.out.println("Enter Length:\t");
- l = sc.nextInt();
- System.out.println("Enter Breadth:\t");
- b = sc.nextInt();
- }
- void calculate()
- {
- area = l*b;
- perimeter = 2*(l+b);
- }
- void disp()
- {
- System.out.println("Area of Rectangle:\t"+area);
- System.out.println("Perimeter of Rectangle:\t"+perimeter);
- }
- public static void main(String[] args)
- {
- rect r = new rect();
- r.accept();
- r.calculate();
- r.disp();
- }
- }
//Execute
//Output
/*
Enter Length:
4
Enter Breadth:
8
Area of Rectangle: 32
Perimeter of Rectangle: 24
*/
c) Write a program menu driven program to perform the following operations.
i) Calculate the volume of cylinder.(hint: Volume : pi*r*r*h)
ii) Find the factorial of given number.
iii) Check the number is Armstrong or not.
iv) Exit
Code:
π
- import java.util.*;
- public class menu_driven
- {
- void factorial()
- {
- int i, n, fact=1;
- Scanner sc = new Scanner(System.in);
- System.out.println("\n Enter Number:\t");
- n = sc.nextInt();
- for (i=1;i<=n ;i++ )
- {
- fact = fact*i;
- }
- System.out.println("\n Factorial of "+n+" is "+fact);
- }
- void cylinder()
- {
- float r,h;
- Scanner sc = new Scanner(System.in);
- System.out.println("\n Enter Radius:\t");
- r= sc.nextFloat();
- System.out.println("\n Enter Height:\t");
- h= sc.nextFloat();
- float volume = ((22/7)*r*r*h);
- System.out.println("\nVolume of cylinder is:\t"+volume);
- }
- void armstrong()
- {
- int n,p=0;
- System.out.println("Enter 3 digit number");
- Scanner sc = new Scanner(System.in);
- n = sc.nextInt();
- int temp = n;
- while (n > 0) {
- int rem = n % 10;
- p = (p) + (rem * rem * rem);
- n = n / 10;
- }
- if (temp == p)
- {
- System.out.println(temp+" It is Armstrong No.");
- }
- else
- {
- System.out.println(temp+" It is not an Armstrong No.");
- }
- }
- public static void main(String[] args) {
- int choice;
- menu_driven md = new menu_driven();
- Scanner sc = new Scanner(System.in);
- System.out.println("Menu Driven Operations");
- do{
- System.out.println("\n1.Volume of Cylinder");
- System.out.println("2.Factorial");
- System.out.println("3.Armstrong");
- System.out.println("4.Exit");
- System.out.print("Which Operation Perform:\t");
- choice = sc.nextInt();
- switch(choice)
- {
- case 1:
- md.cylinder();
- break;
- case 2:
- md.factorial();
- break;
- case 3:
- md.armstrong();
- break;
- case 4:
- System.exit(0);
- break;
- default:
- System.out.println("\n>>Wrong Chioce<<");
- }
- }while(choice != 4);
- }
- }
//Execute
//Ouput
/*
Menu Driven Operations
1.Volume of Cylinder
2.Factorial
3.Armstrong
4.Exit
Which Operation Perform: 1
Enter Radius:
5
Enter Height:
8
Volume of cylinder is: 600.0
1.Volume of Cylinder
2.Factorial
3.Armstrong
4.Exit
Which Operation Perform: 2
Enter Number:
6
Factorial of 6 is 720
1.Volume of Cylinder
2.Factorial
3.Armstrong
4.Exit
Which Operation Perform: 3
Enter 3 digit number
370
370 It is Armstrong No.
1.Volume of Cylinder
2.Factorial
3.Armstrong
4.Exit
Which Operation Perform: 4
*/
d) Write a program to accpet the array element and display in reverse order.
Code:
π
- import java.util.*;
- public class rev_arr
- {
- int arr[] = new int[5];
- void accept()
- {
- Scanner sc =new Scanner(System.in);
- System.out.println("Enter Values for array:");
- for(int i=0; i<5;i++)
- arr[i] = sc.nextInt();
- }
- void disp()
- {
- System.out.println("Values of Array:\t ");
- for(int i=0; i<5;i++)
- System.out.print(" "+arr[i]);
- System.out.println();
- System.out.println("Value of Array in reverse order: \t");
- for(int i=4; i >=0; i--)
- System.out.print(" "+arr[i]);
- System.out.println();
- }
- public static void main(String[] args)
- {
- rev_arr ra = new rev_arr();
- ra.accept();
- ra.disp();
- }
- }
//Execute
//Output
/*
Enter Values for array:
5
7
6
1
8
Values of Array:
5 7 6 1 8
Value of Array in reverse order:
8 1 6 7 5
*/
Set B:
a) Write a java program to display the system date and time in various formats shown below.
Current Date is:23/09/2022
Current Date is:09-23-2022
Current Date is: Friday September 23 2022
Current Date and Time is :Fri September 23 08:09:21 IST 2022
Current Date and Time is:23/09/2022 08:44:21 am +0530
Current time is:08:44:21
Current week of year is:39
Current week of month:4
Current Day of year is:266
Code:
π
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class DateFormatter
- {
- public static void main(String[] args) {
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
- String str = sdf.format(date);
- System.out.println("Current Date is:"+str);
- sdf = new SimpleDateFormat("MM-dd-yyyy");
- str = sdf.format(date);
- System.out.println("Current Date is:"+str);
- sdf = new SimpleDateFormat("EEEE MMMM dd yyyy");
- str = sdf.format(date);
- System.out.println("Current Date is:"+str);
- sdf = new SimpleDateFormat("E MMMM dd HH:MM:ss z yyyy");
- str = sdf.format(date);
- System.out.println("Current Date and Time is :"+str);
- sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a Z");
- str = sdf.format(date);
- System.out.println("Current Date and Time is:"+str);
- sdf = new SimpleDateFormat("hh:mm:ss");
- str = sdf.format(date);
- System.out.println("Current Time is:"+str);
- sdf = new SimpleDateFormat("w");
- str = sdf.format(date);
- System.out.println("Current Week of year is:"+str);
- sdf = new SimpleDateFormat("W");
- str = sdf.format(date);
- System.out.println("Current week of month:"+str);
- sdf = new SimpleDateFormat("D");
- str = sdf.format(date);
- System.out.println("Current Day of year is:"+str);
- }
- }
//Execute
//Output:
/*
Current Date is:23/09/2022
Current Date is:09-23-2022
Current Date is: Friday September 23 2022
Current Date and Time is :Fri September 23 08:09:21 IST 2022
Current Date and Time is:23/09/2022 08:44:21 am +0530
Current time is:08:44:21
Current week of year is:39
Current week of month:4
Current Day of year is:266
*/
b)Define a class MyNumber having one private int data member, write a default constructor to initialize to it 0 and another constructor to initilize it to a value(Use this). Write methods isNegative, isPositive, isPositive, isZero, isOdd, isEven.Create an object in main.Use command line arguments to pass a value to the object (Hint: convert string argument to integer) and perform the above tests. Provide javadoc comments for all constructors and methods and generate the html help file.
Code:
π
- import java.util.*;
- public class MyNumber
- {
- private int x;
- /**
- Constuctor
- @return No return value, as it is constructor
- */
- public MyNumber()
- {
- x=0;
- }
- /**
- @param x Represents the new value
- */
- public MyNumber(int x)
- {
- this.x=x;
- }
- /**
- Member function
- @return Boolean return either true or false*/
- public boolean isNegative()
- {
- if(x < 0)
- return true;
- else
- return false;
- }
- public boolean isPositive()
- {
- if(x>0)
- return true;
- else
- return false;
- }
- public boolean isZero()
- {
- if(x==0)
- return true;
- else
- return false;
- }
- public boolean isEven()
- {
- if(x%2 == 0)
- return true;
- else
- return false;
- }
- public boolean isOdd()
- {
- if(x%2!=0)
- return true;
- else
- return false;
- }
- public static void main(String[] args) {
- int x = Integer.parseInt(args[0]);
- MyNumber m = new MyNumber(x);
- if(m.isNegative())
- {
- System.out.println(x+" is Negative Number");
- }
- if(m.isPositive())
- {
- System.out.println(x+" is Positive Number");
- }
- if(m.isZero())
- {
- System.out.println(x+" is Zero Number");
- }
- if(m.isEven())
- {
- System.out.println(x+" is Even Number");
- }
- if(m.isOdd())
- {
- System.out.println(x+" is Odd Number");
- }
- }
- }
//Execute
πJavaDoc file click here...
//Output
/*
5 is Positive Number
5 is Odd Number
4 is Positive Number
4 is Even Number
0 is Zero Number
0 is Even Number
-2 is Negative Number
-2 is Even Number
*/
c) Write a menu driven program to perform the following operations on multidimensional array is matrix.
i.Addition of matrix
ii.Multiplication of matrix
iii.Tranpose of matrix
iv.Exit
Code:
π
- import java.util.Scanner;
- public class MenuMatrix
- {
- public void addition()
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter size of row and column: ");
- int r = sc.nextInt();
- int c = sc.nextInt();
- int[ ][ ] m1 = new int [r][c];
- int[ ][ ] m2 = new int [r][c];
- System.out.println("\nEnter values of matrix M1:\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print("Enter element at index "+(i+1)+" and "+(j+1)+" :");
- m1[i][j] = sc.nextInt();
- }
- }
- System.out.println("\nEnter values of matrix M2:\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print("Enter element at index "+(i+1)+" and "+(j+1)+" :");
- m2[i][j] = sc.nextInt();
- }
- }
- System.out.println("\nSum of M1 and M2:");
- int [ ][ ] sum = new int [r][c];
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- sum[i][j] = m1[i][j] + m2[i][j];
- }
- }
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print(sum[i][j]+ " ");
- }
- System.out.println("");
- }
- sc.close();
- }
- public void Multiplication()
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter size of row and column:\t");
- int r =sc.nextInt();
- int c = sc.nextInt();
- int[ ][ ] m1=new int[r][c];
- int[ ][ ] m2=new int [r][c];
- System.out.println("\n Enter the value of matrix M1:\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print("\nEnter element at index "+(i+1)+ " and " +(j+1)+":");
- m1[i][j] = sc.nextInt();
- }
- }
- System.out.println("\n Enter the value of matrix M2:\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print("\nEnter element at index "+(i+1)+ " and " +(j+1)+":");
- m2[i][j] = sc.nextInt();
- }
- }
- System.out.println("\nMultiplication of M1 and M2:\n");
- int[][] mul= new int[r][c];
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- for(int k=0; k<c; k++)
- {
- mul[i][j] = mul[i][j]+m1[i][k]*m2[k][j];
- }
- }
- }
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print(mul[i][j]+" ");
- }
- System.out.println();
- }
- sc.close();
- }
- public void transpose()
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter size of row and column: ");
- int r= sc.nextInt();
- int c = sc.nextInt();
- int[ ][ ] m = new int[r][c];
- System.out.println("Enter value of matrix:\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print("Enter element at index "+(i+1)+" and "+(j+1)+" :");
- m[i][j]=sc.nextInt();
- }
- }
- System.out.println("\nEntered Matrix..\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print(m[i][j]+" ");
- }
- System.out.println();
- }
- System.out.println("\nTranspose Matrix..\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print(m[j][i]+" ");
- }
- System.out.println();
- }
- sc.close();
- }
- public static void main(String[] args)
- {
- MenuMatrix m = new MenuMatrix();
- Scanner sc = new Scanner(System.in);
- System.out.println("\n1.Addition of Matrix.\n2.Multiplication of Matrix. \n3.Transpose of Matrix. \n4.Exit.");
- System.out.println("Enter Your Choice");
- int choice = sc.nextInt();
- switch(choice)
- {
- case 1:
- m.addition();
- break;
- case 2:
- m.Multiplication();
- break;
- case 3:
- m.transpose();
- break;
- case 4:
- System.exit(0);
- break;
- default:
- System.out.println("\n>>Wrong Chioce<<");
- }
- }
- }
//Execute
//Output
/*
1.Addition of Matrix.
2.Multiplication of Matrix.
3.Transpose of Matrix.
4.Exit.
Enter Your Choice
3
Enter size of row and column:
2
2
Enter value of matrix:
Enter element at index 1 and 1 :1
Enter element at index 1 and 2 :0
Enter element at index 2 and 1 :1
Enter element at index 2 and 2 :0
Entered Matrix..
1 0
1 0
Transpose Matrix..
1 1
0 0
1.Addtion of Matrix.
2.Multiplication of Matrix.
3.Transpose of Matrix.
4.Exit.
Enter Your Choice
1
Enter size of row and column:
2
2
Enter values of matrix M1:
Enter element at index 1 and 1:1
Enter element at index 1 and 2:2
Enter element at index 2 and 1:3
Enter element at index 2 and 2:5
Enter values of matrix M2:
Enter element at index 1 and 1:4
Enter element at index 1 and 2:5
Enter element at index 2 and 1:4
Enter element at index 2 and 2:7
Sum of M1 and M2:
5 7
7 12
1.Addtion of Matrix.
2.Multiplication of Matrix.
3.Transpose of Matrix.
4.Exit.
Enter YOur Choice
2
Enter size of row and column:
2
2
Enter the value of matrix M1:
Enter element at index 1 and 1:4
Enter element at index 1 and 2:5
Enter element at index 2 and 1:1
Enter element at index 2 and 2:4
Enter the value of matrix M2:
Enter element at index 1 and 1:1
Enter element at index 1 and 2:15
Enter element at index 2 and 1:5
Enter element at index 2 and 2:
14
Multiplication of M1 and M2:
29 130
21 71
*/
Set C:
a) Write a program to accpet n names of country and display them in descending order.
Code:
π
- import java.util.*;
- class City
- {
- String a[ ];
- int n;
- City()
- {
- Scanner s=new Scanner(System.in);
- System.out.print("Enter how many city you want to enter : ");
- n=s.nextInt();
- a=new String[n];
- for(int i=0;i<n;i++)
- {
- System.out.print("Enter "+(i+1)+" City Name: ");
- a[i]=s.next();
- }
- }
- void display()
- {
- String temp="";
- for(int i=0;i<n;i++)
- {
- for(int j=i+1;j<n;j++)
- {
- if(a[i].compareTo(a[j])>0)
- {
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- }
- }
- System.out.println("\nSorted Cities are ");
- for(int i=0;i<n;i++)
- {
- System.out.println((i+1)+"."+a[i]);
- }
- }
- }
- class TestCity
- {
- public static void main(String args[])
- {
- City obj=new City();
- obj.display();
- }
- }
//Execute
//Output
/*
Enter how many city you want to enter : 3
Enter 1 City Name: Nashik
Enter 2 City Name: Pune
Enter 3 City Name: Mumbai
Sorted Cities are
1.Mumbai
2.Nashik
3.Pune
*/
b) Write a menu driven program to perform the following operations on 2d array.
i. Sum of digonal elements
ii. Sum of upper digonal elements
iii. Sum of lower digonal elements
iv. Exit
Code:
π
- import java.util.Scanner;
- public class DiaMatrix
- {
- public void diagonalsum()
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter size of row and column: ");
- int r = sc.nextInt();
- int c = sc.nextInt();
- int[][] m = new int [r][c];
- System.out.println("\nEnter values of matrix:\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print("Enter element at index "+(i+1)+" and "+(j+1)+" :");
- m[i][j] = sc.nextInt();
- }
- }
- System.out.println("\nEntered Matrix..\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print(m[i][j]+" ");
- }
- System.out.println();
- }
- int sum = 0;
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- if (i==j)
- {
- sum = sum+m[i][j];
- }
- }
- }
- System.out.println("Sum Of Diagonal Elements : "+sum);
- sc.close();
- }
- public void upperdiagonalsum()
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter size of row and column: ");
- int r = sc.nextInt();
- int c = sc.nextInt();
- int[][] m = new int [r][c];
- System.out.println("\nEnter values of matrix:\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print("Enter element at index "+(i+1)+" and "+(j+1)+" :");
- m[i][j] = sc.nextInt();
- }
- }
- System.out.println("\nEntered Matrix..\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print(m[i][j]+" ");
- }
- System.out.println();
- }
- int sum = 0;
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- if (i > j)
- {
- sum = sum+m[i][j];
- }
- }
- }
- System.out.println("Sum Of Upper Diagonal Elements : "+sum);
- sc.close();
- }
- public void lowerdiagonalsum()
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter size of row and column: ");
- int r = sc.nextInt();
- int c = sc.nextInt();
- int[][] m = new int [r][c];
- System.out.println("\nEnter values of matrix:\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print("Enter element at index "+(i+1)+" and "+(j+1)+" :");
- m[i][j] = sc.nextInt();
- }
- }
- System.out.println("\nEntered Matrix..\n");
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- System.out.print(m[i][j]+" ");
- }
- System.out.println();
- }
- int sum = 0;
- for(int i=0; i<r; i++)
- {
- for(int j=0; j<c; j++)
- {
- if (i < j)
- {
- sum = sum+m[i][j];
- }
- }
- }
- System.out.println("Sum Of Lower Diagonal Elements : "+sum);
- sc.close();
- }
- public static void main(String[] args)
- {
- DiaMatrix d = new DiaMatrix();
- Scanner sc = new Scanner(System.in);
- System.out.println("\n1.Sum of Diagonal Elements.\n2.Sum of Upper Diagonal Elements. \n3.Sum of Lower Diagonal Elements.. \n4.Exit.");
- System.out.println("Enter Your Choice");
- int choice = sc.nextInt();
- switch(choice)
- {
- case 1:
- d.diagonalsum();
- break;
- case 2:
- d.upperdiagonalsum();
- break;
- case 3:
- d.lowerdiagonalsum();
- break;
- case 4:
- System.exit(0);
- break;
- default:
- System.out.println("\n>>Wrong Chioce<<");
- }
- }
- }
//Execute
/*Output
1.Sum of Diagonal Elements.
2.Sum of Upper Diagonal Elements.
3.Sum of Lower Diagonal Elements..
4.Exit.
Enter Your Choice
1
Enter size of row and column:
3
3
Enter values of matrix:
Enter element at index 1 and 1 :1
Enter element at index 1 and 2 :2
Enter element at index 1 and 3 :3
Enter element at index 2 and 1 :4
Enter element at index 2 and 2 :1
Enter element at index 2 and 3 :5
Enter element at index 3 and 1 :1
Enter element at index 3 and 2 :7
Enter element at index 3 and 3 :1
Entered Matrix..
1 2 3
4 1 5
1 7 1
Sum Of Diagonal Elements : 3
1.Sum of Diagonal Elements.
2.Sum of Upper Diagonal Elements.
3.Sum of Lower Diagonal Elements..
4.Exit.
Enter Your Choice
2
Enter size of row and column:
3
3
Enter values of matrix:
Enter element at index 1 and 1 :1
Enter element at index 1 and 2 :2
Enter element at index 1 and 3 :3
Enter element at index 2 and 1 :4
Enter element at index 2 and 2 :1
Enter element at index 2 and 3 :5
Enter element at index 3 and 1 :1
Enter element at index 3 and 2 :7
Enter element at index 3 and 3 :1
Entered Matrix..
1 2 3
4 1 5
1 7 1
Sum Of Upper Diagonal Elements : 12
1.Sum of Diagonal Elements.
2.Sum of Upper Diagonal Elements.
3.Sum of Lower Diagonal Elements..
4.Exit.
Enter Your Choice
3
Enter size of row and column:
3
3
Enter values of matrix:
Enter element at index 1 and 1 :1
Enter element at index 1 and 2 :2
Enter element at index 1 and 3 :3
Enter element at index 2 and 1 :4
Enter element at index 2 and 2 :1
Enter element at index 2 and 3 :5
Enter element at index 3 and 1 :1
Enter element at index 3 and 2 :7
Enter element at index 3 and 3 :1
Entered Matrix..
1 2 3
4 1 5
1 7 1
Sum Of Lower Diagonal Elements : 10
*/
c) Write a programti display the 1 to 15 tables.
(1*1=1 2*1=2 ...... 15*1=15
1*2=2 2*2=4 ...... 15*2=30
...... ........ .......
1*10=10 2*10=20 15*10=150 )
Code:
π
- public class table
- {
- public static void main(String[] args)
- {
- //int n =15;
- for(int i=1; i <= 10; i++)
- {
- for(int j=1; j<=15; j++)
- {
- int ans = i*j;
- System.out.print(i+" * "+j+" = "+ans+"\t");
- }
- System.out.println();
- }
- }
- }
//Execute
/*Output
*/
//ThE ProFessoR