Java Practical Programs Sem5
JAVA PRACTICAL ASSIGNMENT:
ASSIGNMENT 1
SET - A
Assignment 1
/* Assignment 1 Set-A a)
Using javap, view the methods of the following classes from the lang package: java.lang.Object , java.lang.String and java.util.Scanner, and also Compile sample program. Type the following command and view and view the bytecodes, javap -c MyClass. */
public class Ass1_SetA_a { int num; public Ass1_SetA_a() { num = 0; } public Ass1_SetA_a(int num) { this.num = num; } public static void main(String args[]) { Ass1_SetA_a obj1 = new Ass1_SetA_a(); if(args.length > 0) { int n = Integer.parseInt(args[0]); Ass1_SetA_a obj2 = new Ass1_SetA_a(n); System.out.println("Num of object first:"+obj1.num); System.out.println("Num of object second:"+obj2.num); } else System.out.println("Insufficient Arguments..."); } }a]:Using javap, view the methods of the following classes from the lang package: java.lang.Object , java.lang.String and java.util.Scanner, and also Compile sample program. Type the following command and view and view the bytecodes, javap -c MyClass.
/* Assignment 1 Set-A a)
Using javap, view the methods of the following classes from the lang package:
java.lang.Object , java.lang.String and java.util.Scanner, and also Compile sample program.
Type the following command and view and view the bytecodes, javap -c MyClass.
*/
public class Ass1_SetA_a {
int num;
public Ass1_SetA_a()
{
num = 0;
}
public Ass1_SetA_a(int num)
{
this.num = num;
}
public static void main(String args[])
{
Ass1_SetA_a obj1 = new Ass1_SetA_a();
if(args.length > 0)
{
int n = Integer.parseInt(args[0]);
Ass1_SetA_a obj2 = new Ass1_SetA_a(n);
System.out.println("Num of object first:"+obj1.num);
System.out.println("Num of object second:"+obj2.num);
}
else
System.out.println("Insufficient Arguments...");
}
}
OUPTUT:
PS C:\Users\Swapnil\IdeaProjects\demo1\helloj\src> javac Ass1_SetA_a.java
PS C:\Users\Swapnil\IdeaProjects\demo1\helloj\src> java Ass1_SetA_a 88
Num of object first:0
Num of object second:88
PS C:\Users\jambl\IdeaProjects\demo1\helloj\src> javap -c Ass1_SetA_a
Compiled from "Ass1_SetA_a.java"
public class Ass1_SetA_a {
int num;
public Ass1_SetA_a();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_0
6: putfield #7 // Field num:I
9: return
public Ass1_SetA_a(int);
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iload_1
6: putfield #7 // Field num:I
9: return
public static void main(java.lang.String[]);
Code:
0: new #8 // class Ass1_SetA_a
3: dup
4: invokespecial #13 // Method "<init>":()V
7: astore_1
8: aload_0
9: arraylength
10: ifle 62
13: aload_0
14: iconst_0
15: aaload
16: invokestatic #14 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
19: istore_2
20: new #8 // class Ass1_SetA_a
23: dup
24: iload_2
}
b]: Write a program to calculate perimeter and area of triangle,
(hint : area =length * breadth, perimeter = 2 *(length+breadth)
import java.util.Scanner;
public class Ass1_SetA_b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length and breadth of the rectangle:");
float length = sc.nextFloat();
float breadth = sc.nextFloat();
float area = length * breadth;
float perimeter = 2 * (length + breadth);
System.out.println("The area of rectangle is :"+area);
System.out.println("The perimeter of rectangle is :"+perimeter);
}
}
OUTPUT:
Enter the length and breadth of the rectangle:
5.5
8.5
The area of rectangle is :46.75
The perimeter of rectangle is :28.0
c] Write a menu driven program to perform the following operation:
i. Calculate the volume of cylinder (volume:Ï€ × r² × h).
ii. Find the factorial of the given number.
iii. Check number is armstrong or not.
iv. Exit.
/*c] Write a menu driven program to perform the following operation:
i. Calculate the volume of cylinder (volume:Ï€ × r² × h).
ii. Find the factorial of the given number.
iii. Check number is armstrong or not.
iv. Exit.
*/
import java.util.Scanner;
import java.lang.Math;
public class FactorialArmstrong {
public static void main(String[] args) {
int ch;
int r;
int h;
int fact=1;
int num;
int last;
int result=0;
int len;
int digits=0;
Scanner sc = new Scanner(System.in);
do {
System.out.println("\n1.calculate volume of cylinder:\n2:find factorial \n3:chk armstrong\n4:Exit");
System.out.println("enter ur choice:");
ch = sc.nextInt();
switch(ch) {
case 1:
System.out.println("enter the radius and height:");
r = sc.nextInt();
h = sc.nextInt();
System.out.println("volume of cylindr is:" + 3.14 * r * r * h);
break;
case 2:
System.out.println("enter the number:");
num = sc.nextInt();
int temp = num;
while (temp > 0) {
fact = fact * temp;
temp--;
}
System.out.println("factorial of "+num+" is:"+fact);
fact =1;
break;
case 3:
System.out.println("Enter the number:"); //153
num = sc.nextInt();
temp=num;
while(temp>0)
{
temp=temp/10;
digits ++;
}
temp = num;
while(temp>0)
{
last = temp%10;
result += (Math.pow(last, digits));
temp=temp/10;
}
if(num==result)
System.out.println(num+" is armstrong number..");
else
System.out.println(num+" is not armstrong number..");
result =0;
last =0;
temp=0;
digits=0;
break;
case 4:
System.out.println("Exit");
break;
default:
System.out.println("Invalid case!!!");
}
}while(ch!=4);
}
}
/* output:
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
1
enter the radius and height:
1
2
volume of cylindr is:6.28
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
2
enter the number:
3
factorial of 3 is:6
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
2
enter the number:
4
factorial of 4 is:24
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
2
enter the number:
5
factorial of 5 is:120
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
3
Enter the number:
33
33 is not armstrong number..
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
3
Enter the number:
3
3 is armstrong number..
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
3
Enter the number:
2
2 is armstrong number..
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
3
Enter the number:
153
153 is armstrong number..
1.calculate volume of cylinder:
2:find factorial
3:chk armstrong
4:Exit
enter ur choice:
4
Exit
*/
d] Write a program to accept the array element and display in reverse order.
public class Ass1_SetA_d
{
public static void main(String[] args)
{
int a[] = {1,2,3,4,5};
System.out.println("The Orginal Array:");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]+" ");
}
System.out.println("Array is Reverse Order:");
for (int i = a.length-1; i >= 0; i--) {
System.out.print(a[i] + " ");
}
}
}
OUTPUT:
The Orginal Array:
1
2
3
4
5
Array is Reverse Order:
5 4 3 2 1
SET - B
a] Write a java program to display the system date and time in various formats shown
Current date is : 31/08/2021
Current date is : 08-31-2021
Current date is : Tuesday August 31 2021
Current date and time is : Fri August 31 15:25:59 IST 2021
Current date and time is : 31/08/21 15:25:59 PM +0530
Current time is : 15:25:59
Current week of year is : 35
Current week of month : 5
Current day of the year is : 243
Note: Use java.util.Date and java.text.SimpleDateFormat class
import java.text.SimpleDateFormat;
import java.util.Date;
public class Ass1_SetB_a
{
public static void main(String[] args)
{
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String strDate = formatter.format(date);
System.out.println("Current date is: "+strDate);
formatter = new SimpleDateFormat("MM-dd-yyyy");
strDate = formatter.format(date);
System.out.println("Current date is: "+strDate);
formatter = new SimpleDateFormat("EEEEEE MMMM dd yyyy");
strDate = formatter.format(date);
System.out.println("Current date is: "+strDate);
formatter = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");
strDate = formatter.format(date);
System.out.println("Current date and time is: "+strDate);
formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss a Z");
strDate = formatter.format(date);
System.out.println("Current date and time is: "+strDate);
formatter = new SimpleDateFormat("hh:mm:ss");
strDate = formatter.format(date);
System.out.println("Current time is: "+strDate);
formatter = new SimpleDateFormat("w");
strDate = formatter.format(date);
System.out.println("Current week of year is: "+strDate);
formatter = new SimpleDateFormat("W");
strDate = formatter.format(date);
System.out.println("Current week of the month is: "+strDate);
formatter = new SimpleDateFormat("D");
strDate = formatter.format(date);
System.out.println("Current day of the year: "+strDate);
}
}
OUTPTU:
Current date is: 08/11/2022
Current date is: 11-08-2022
Current date is: Tuesday November 08 2022
Current date and time is: Tue November 08 22:41:11 IST 2022
Current date and time is: 08/11/22 22:41:11 pm +0530
Current time is: 10:41:11
Current week of year is: 46
Current week of the month is: 2
Current day of the year: 312
b] Define a class Mynumber having one private int data member. Write a default constructor to initialize it to 0 and another constructor to initialize it to a value (Use this) . Write methods isNegative, isZero, isOdd, isEven. Create an object in main . Use command line argument to pass a value to the object (Hint: convert string argument to integer) and perform the above tests. provide javadoc comments for all constructor and methods and generate the html help file.
public class Ass1_SetB_b
{
private int x;
Ass1_SetB_b()
{
x = 0;
}
Ass1_SetB_b(int x)
{
this.x = x;
}
void isZero()
{
if(x==0)
System.out.println("The number is Zero");
}
void isPositive()
{
if(x>0)
System.out.println("The number is Positive");
}
void isNegative()
{
if(x<0)
System.out.println("The number is Negative");
}
void isOdd()
{
if(x%2!=0)
System.out.println("The number is Odd");
}
void isEven()
{
if(x%2==0)
System.out.println("The number is Even");
}
public static void main(String [] args) throws ArrayIndexOutOfBoundsException
{
int num=Integer.parseInt(args[0]);
Ass1_SetB_b obj=new Ass1_SetB_b(num);
obj.isNegative();
obj.isPositive();
obj.isEven();
obj.isOdd();
obj.isZero();
}
}
OUTPUT:
PS C:\Users\swapniljamble\IdeaProjects\demo1\helloj\src> java Ass1_SetB_b 50
The number is Positive
PS C:\Users\swapniljamble\IdeaProjects\demo1\helloj\src> java Ass1_SetB_b -88
The number is Negative
The number is Even
PS C:\Users\swapniljamble\IdeaProjects\demo1\helloj\src> java Ass1_SetB_b 0
The number is Even
The number is Zero
PS C:\Users\swapniljamble\IdeaProjects\demo1\helloj\src> java Ass1_SetB_b 99
The number is Positive
The number is Odd
PS C:\Users\swapniljamble\IdeaProjects\demo1\helloj\src> javadoc Ass1_SetB_b.java
Loading source file Ass1_SetB_b.java...
Constructing Javadoc information...
Building index for all the packages and classes...
Standard Doclet version 18.0.2+9-61
Building tree for all the packages and classes...
Generating .\Ass1_SetB_b.html...
Ass1_SetB_b.java:1: warning: no comment
public class Ass1_SetB_b
^
Ass1_SetB_b.java:43: warning: no comment
public static void main(String [] args) throws ArrayIndexOutOfBoundsException
^
Generating .\package-summary.html...
Generating .\package-tree.html...
Generating .\overview-tree.html...
Building index for all classes...
Generating .\allclasses-index.html...
Generating .\allpackages-index.html...
Generating .\index-all.html...
Generating .\index.html...
Generating .\help-doc.html...
2 warnings
PS C:\Users\swapniljamble\IdeaProjects\demo1\helloj\src>
c] Write a menu driven program to perform the following operations on
multidimensional array in matrix.
i. Addition
ii. Multiplication
iii. Transpose of any matrix
iv. Exit.
import java.util.Scanner;
class Ass1_SetB_c
{
public static void main(String arr[])
{
int a,b,n,ch;
Scanner sc = new Scanner(System.in);
System.out.println("How many number of rows in matrix:");
a = sc.nextInt();
System.out.println("How many number of columns in matrix:");
b = sc.nextInt();
int m1[][] = new int[a][b];
int m2[][] = new int[a][b];
System.out.println("Enter the 1st matrix:");
for(int i=0; i<m1.length; i++)
{
for(int j=0; j<m1.length; j++)
{
m1[i][j] = sc.nextInt();
}
}
System.out.println("1st matrix is:");
for(int i=0; i<m1.length; i++)
{
for(int j=0; j<m1.length; j++)
{
System.out.print(m1[i][j]+" ");
}
System.out.println();
}
System.out.println("Enter the 2st matrix:");
for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
m2[i][j] = sc.nextInt();
}
}
System.out.println("2st matrix is:");
for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
System.out.print(m2[i][j]+" ");
}
System.out.println();
}
do
{
System.out.println("1.Addition:");
System.out.println("2.Multiplication:");
System.out.println("3.Transpose of matrix:");
System.out.println("4.Exit:");
System.out.println("Plz Enter ur choice:");
ch = sc.nextInt();
switch(ch)
{
case 1:
int sum[][] = new int[a][b];
for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
sum[i][j] = m1[i][j] + m2[i][j];
}
}
System.out.println("Addition of m1 and m2:");
for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
break;
case 2:
int mul[][] = new int[a][b];
for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
mul[i][j] = m1[i][j] * m2[i][j];
}
}
System.out.println("Multiplication of m1 and m2:");
for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
System.out.print(mul[i][j]+" ");
}
System.out.println();
}
break;
case 3:
int trn[][] = new int[a][b];
for(int i=0; i<m1.length; i++)
{
for(int j=0; j<m1.length; j++)
{
trn[i][j] =m1[j][i];
}
}
System.out.println("Transpose of m1:");
for(int i=0; i<m1.length; i++)
{
for(int j=0; j<m1.length; j++)
{
System.out.print(trn[i][j]+" ");
}
System.out.println();
}
break;
}
}while(ch!=4);
}
}
OUTPUT:
How many number of rows in matrix:
2
How many number of columns in matrix:
2
Enter the 1st matrix:
1 2
3 1
1st matrix is:
1 2
3 1
Enter the 2st matrix:
2 1
1 3
2st matrix is:
2 1
1 3
1.Addition:
2.Multiplication:
3.Transpose of matrix:
4.Exit:
Plz Enter ur choice:
1
Addition of m1 and m2:
3 3
4 4
1.Addition:
2.Multiplication:
3.Transpose of matrix:
4.Exit:
Plz Enter ur choice:
2
Multiplication of m1 and m2:
2 2
3 3
1.Addition:
2.Multiplication:
3.Transpose of matrix:
4.Exit:
Plz Enter ur choice:
3
Transpose of m1:
1 3
2 1
1.Addition:
2.Multiplication:
3.Transpose of matrix:
4.Exit:
Plz Enter ur choice:
4
Process finished with exit code 0
// write a program to accept n names of country and display them in descending order.
import java.util.Scanner;
class Ass1_SetC_a
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.println("How many Country u want to sort in descending order:");
int n = sc.nextInt();
String name[]=new String[n];
System.out.println("Enter the country names:");
for(int i=0; i<n; i++)
{
name[i] = sc.next();
}
String temp;
for(int j=0;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if((name[j].compareTo(name[k])) < 0 )
{
temp=name[j];
name[j]=name[k];
name[k]=temp;
}
}
}
System.out.println("\n Sorted City in Descending order:");
for(int i=0;i<n;i++)
{
System.out.println(name[i]);
}
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass1_SetC_a.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass1_SetC_a
How many Country u want to sort in descending order:
5
Enter the country names:
India
Korea
America
England
Shrilanka
Sorted City in Descending order:
Shrilanka
Korea
India
England
America
*/
/* write a menu driven program to perform a following operatios on 2D array:
i. Sum of diagonal element
ii. sum of upper diagonal elements
iii. sum of lower diagonal elements
iv . Exit.
*/
import java.util.Scanner;
public class Ass1_SetC_b
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int choice;
int SumDiagonal = 0;
int SumUpperDiagonal = 0;
int SumLowerDiagonal = 0;
System.out.println("How many row you want?:");
int a = sc.nextInt();
System.out.println("How many Column you want?:");
int b= sc.nextInt();
int m1[][] = new int[a][b];
System.out.println("Enter the matrix : ");
for (int i = 0; i < m1.length; i++)
{
for (int j = 0; j < m1.length; j++)
{
m1[i][j]=sc.nextInt();
}
}
System.out.println("Matrix is : ");
for (int i = 0; i < m1.length; i++)
{
for (int j = 0; j < m1.length; j++)
{
System.out.print(m1[i][j]+" ");
}
System.out.println(" ");
}
do {
System.out.println("\n1.Sum of Diagonal element.");
System.out.println("2.Sum of upper diagonal elements.");
System.out.println("3.Sum of lower diagonal elements.");
System.out.println("4.Exist.");
System.out.println("Enter the Choice:");
choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.println(" ");
for(int i = 0; i < a ; i++)
{
System.out.format("\nThe Matrix Item at [" + i + "][" + i +"] position = " + m1[i][i]);
for(int j = 0; j < b; j++)
{
if(j == i)
{
SumDiagonal += m1[i][j];
}
}
}
System.out.println("\nThe Sum of Diagonal Elements of given matrix = " + SumDiagonal);
SumDiagonal = 0;
break;
case 2:
System.out.println(" ");
System.out.println("\nSum of upper diagonal elements of given Matrix:");
for(int i = 0; i < a ; i++)
{
for(int j = 0; j < b; j++)
{
if(j >= i)
{
SumUpperDiagonal += m1[i][j];
}
}
}
System.out.println(SumUpperDiagonal);
SumUpperDiagonal = 0;
break;
case 3:
System.out.println("\n---Sum of lower diagonal elements of the given Matrix---");
for(int i = 0; i < a ; i++)
{
for(int j = 0; j < b; j++)
{
if(j <= i)
{
SumLowerDiagonal += m1[i][j];
}
}
}
System.out.println(SumLowerDiagonal);
SumLowerDiagonal = 0;
break;
case 4:
System.exit(0);
default:
System.out.println("Wrong Input!!!");
break;
}
} while (choice < '1' || choice > '4');
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass1_SetC_b.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass1_SetC_b
How many row you want?:
3
How many Column you want?:
3
Enter the matrix :
1 2 3
3 1 2
2 3 2
Matrix is :
1 2 3
3 1 2
2 3 2
1.Sum of Diagonal element.
2.Sum of upper diagonal elements.
3.Sum of lower diagonal elements.
4.Exist.
Enter the Choice:
1
The Matrix Item at [0][0] position = 1
The Matrix Item at [1][1] position = 1
The Matrix Item at [2][2] position = 2
The Sum of Diagonal Elements of given matrix = 4
1.Sum of Diagonal element.
2.Sum of upper diagonal elements.
3.Sum of lower diagonal elements.
4.Exist.
Enter the Choice:
2
Sum of upper diagonal elements of given Matrix:
11
1.Sum of Diagonal element.
2.Sum of upper diagonal elements.
3.Sum of lower diagonal elements.
4.Exist.
Enter the Choice:
3
---Sum of lower diagonal elements of the given Matrix---
12
1.Sum of Diagonal element.
2.Sum of upper diagonal elements.
3.Sum of lower diagonal elements.
4.Exist.
Enter the Choice:
5
Wrong Input!!!
1.Sum of Diagonal element.
2.Sum of upper diagonal elements.
3.Sum of lower diagonal elements.
4.Exist.
Enter the Choice:
4
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src>
*/
OR
import java.util.Scanner;
public class Pract_Ass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r,c;
System.out.println("enter the row of matrix:");
r = sc.nextInt();
System.out.println("enter the column of matrix:");
c = sc.nextInt();
int m1[][] = new int[r][c];
int m2[][] = new int[r][c];
System.out.println("enter the m1 matrix:");
for(int i=0;i<=m1.length-1;i++) {
for (int j = 0; j <= m1.length - 1; j++) {
m1[i][j] = sc.nextInt();
}
}
for(int i=0;i<=m1.length-1;i++) {
for (int j = 0; j <= m1.length - 1; j++) {
System.out.print(m1[i][j]+" ");
}
System.out.println("");
}
System.out.println("enter the m2 matrix:");
for(int i=0;i<=m2.length-1;i++) {
for (int j = 0; j <= m2.length - 1; j++) {
m2[i][j] = sc.nextInt();
}
}
for(int i=0;i<=m2.length-1;i++) {
for (int j = 0; j <= m2.length - 1; j++) {
System.out.print(m2[i][j]+" ");
}
System.out.println("");
}
System.out.println("Sum Of Diagonal Elements:");
int sum[][] = new int[r][c];
for(int i=0;i<=m2.length-1;i++) {
for (int j = 0; j <= m2.length - 1; j++) {
if(i == j)
sum[i][j] = m1[i][j] + m2[i][j];
}
System.out.println("");
}
for(int i=0;i<=sum.length-1;i++) {
for (int j = 0; j <= sum.length - 1; j++) {
System.out.print(sum[i][j]+" ");
}
System.out.println("");
}
System.out.println("Sum Of Upper Diagonal Elements:");
int sumud[][] = new int[r][c];
for(int i=0;i<=sumud.length-1;i++) {
for (int j = 0; j <= sumud.length - 1; j++) {
if(j >= i)
sumud[i][j] = m1[i][j] + m2[i][j];
}
System.out.println("");
}
for(int i=0;i<=sumud.length-1;i++) {
for (int j = 0; j <= sumud.length - 1; j++) {
System.out.print(sumud[i][j]+" ");
}
System.out.println("");
}
System.out.println("Sum Of Lower Diagonal Elements:");
int sumld[][] = new int[r][c];
for(int i=0;i<=sumld.length-1;i++) {
for (int j = 0; j <= sumld.length - 1; j++) {
if(i >= j)
sumld[i][j] = m1[i][j] + m2[i][j];
}
System.out.println("");
}
for(int i=0;i<=sumud.length-1;i++) {
for (int j = 0; j <= sumld.length - 1; j++) {
System.out.print(sumld[i][j]+" ");
}
System.out.println("");
}
}
}
enter the row of matrix:
3
enter the column of matrix:
3
enter the m1 matrix:
1 2 3
1 2 3
4 2 1
1 2 3
1 2 3
4 2 1
enter the m2 matrix:
1 2 3
2 3 1
2 2 2
1 2 3
2 3 1
2 2 2
Sum Of Diagonal Elements:
2 0 0
0 5 0
0 0 3
Sum Of Upper Diagonal Elements:
2 4 6
0 5 4
0 0 3
Sum Of Lower Diagonal Elements:
2 0 0
3 5 0
6 4 3
/* write a program to display the 1 to 15 tabless.
1 * 1 = 1 2 * 1 = 2 ......... 15 * 1 = 15
1 * 2 = 2 2 * 2 = 2 ......... 15 * 2 = 30
... ... ... ...
1*10= 10 2*10= 20 ... 15*10= 150
*/
public class Ass1_SetC_c
{
public static void main(String[] args)
{
System.out.print("\nMultiplication table from 1 to 15:\n");
for(int i=1;i<=10;i++)
{
for(int j=1;j<=15;j++)
{
System.out.printf("%d*%d =%d ", j,i, j * i);
}
System.out.printf("\n");
}
}
}
/* OUTPUT:
PS C:\Users\jambl\IdeaProjects\demo1\helloj\src> javac Ass1_SetC_c.java
PS C:\Users\jambl\IdeaProjects\demo1\helloj\src> java Ass1_SetC_c
Multiplication table from 1 to 15:
1*1 =1 2*1 =2 3*1 =3 4*1 =4 5*1 =5 6*1 =6 7*1 =7 8*1 =8 9*1 =9 10*1 =10 11*1 =11 12*1 =12 13*1 =13 14*1 =14 15*1 =15
1*2 =2 2*2 =4 3*2 =6 4*2 =8 5*2 =10 6*2 =12 7*2 =14 8*2 =16 9*2 =18 10*2 =20 11*2 =22 12*2 =24 13*2 =26 14*2 =28 15*2 =30
1*3 =3 2*3 =6 3*3 =9 4*3 =12 5*3 =15 6*3 =18 7*3 =21 8*3 =24 9*3 =27 10*3 =30 11*3 =33 12*3 =36 13*3 =39 14*3 =42 15*3 =45
1*4 =4 2*4 =8 3*4 =12 4*4 =16 5*4 =20 6*4 =24 7*4 =28 8*4 =32 9*4 =36 10*4 =40 11*4 =44 12*4 =48 13*4 =52 14*4 =56 15*4 =60
1*5 =5 2*5 =10 3*5 =15 4*5 =20 5*5 =25 6*5 =30 7*5 =35 8*5 =40 9*5 =45 10*5 =50 11*5 =55 12*5 =60 13*5 =65 14*5 =70 15*5 =75
1*6 =6 2*6 =12 3*6 =18 4*6 =24 5*6 =30 6*6 =36 7*6 =42 8*6 =48 9*6 =54 10*6 =60 11*6 =66 12*6 =72 13*6 =78 14*6 =84 15*6 =90
1*7 =7 2*7 =14 3*7 =21 4*7 =28 5*7 =35 6*7 =42 7*7 =49 8*7 =56 9*7 =63 10*7 =70 11*7 =77 12*7 =84 13*7 =91 14*7 =98 15*7 =105
1*8 =8 2*8 =16 3*8 =24 4*8 =32 5*8 =40 6*8 =48 7*8 =56 8*8 =64 9*8 =72 10*8 =80 11*8 =88 12*8 =96 13*8 =104 14*8 =112 15*8 =120
1*9 =9 2*9 =18 3*9 =27 4*9 =36 5*9 =45 6*9 =54 7*9 =63 8*9 =72 9*9 =81 10*9 =90 11*9 =99 12*9 =108 13*9 =117 14*9 =126 15*9 =135
1*10=10 2*10 =20 3*10 =30 4*10 =40 5*10 =50 6*10 =60 7*10 =70 8*10 =80 9*10 =90 10*10 =100 11*10 =110 12*10 =12 13*10 =130 14*10 =140 15*10 =150
*/
ASSIGNMENT 2:
/*
Create an employee class(id,name,deptname,salary). Define a default and parameterized constructor.
Use 'this' keyword to initialize instance variables.Keep a count of objects created.
Create objects using parameterized constructor and display the object count after each object is created.
(Use static member and method). Also display the contents of each object
*/
class Student
{
static int count;
int id;
String name;
String depname;
float salary;
Student()
{
id = 0;
name = null;
depname = null;
salary = 0.0f;
}
Student(int id, String name, String depname, float salary)
{
this.id = id;
this.name = name;
this.depname = depname;
this.salary = salary;
}
void display()
{
count++;
System.out.println("ID : " + id);
System.out.println("Name : " + name);
System.out.println("Department Name : " + depname);
System.out.println("Salary : " + salary);
System.out.println("The Count of object : " + count);
}
}
public class Ass2_SetA_a
{
public static void main(String[] args)
{
Student student1 = new Student();
Student student2 = new Student(50,"Swapnil","Computer Science",95000.0F);
Student student3 = new Student(5,"Hermione","Computer Science",100000.0F);
student1.display();
student2.display();
student3.display();
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass2_SetA_a.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass2_SetA_a
ID : 0
Name : null
Department Name : null
Salary : 0.0
The Count of object : 1
ID : 50
Name : Swapnil
Department Name : Computer Science
Salary : 95000.0
The Count of object : 2
ID : 5
Name : Hermione
Department Name : Computer Science
Salary : 100000.0
The Count of object : 3
*/
Or
import java.util.Scanner;
public class Pract_Ass {
int id,salary;
String name,dep;
static int count;
Pract_Ass()
{
id = 0;
salary = 0;
name = null;
dep = null;
count++;
}
Pract_Ass(int id,int salary,String name,String dep)
{
this.id = id;
this.salary = salary;
this.name = name;
this.dep = dep;
count++;
}
public String toStirng()
{
return "\nid:"+id+" name:"+name+" salary:"+salary+" department:"+dep;
}
public static void main(String[] args) {
Pract_Ass obj = new Pract_Ass();
Pract_Ass obj1 = new Pract_Ass(1,500,"swapnil","cs");
Pract_Ass obj2 = new Pract_Ass(2,400,"saurabh","math");
Pract_Ass obj3 = new Pract_Ass();
Pract_Ass obj4 = new Pract_Ass(8,100,"sonny","lyrics");
System.out.println(obj.toStirng());
System.out.println(obj1.toStirng());
System.out.println(obj2.toStirng());
System.out.println(obj3.toStirng());
System.out.println(obj4.toStirng());
System.out.println("total number of object created :"+count);
}
}
/*
Define Student class(roll_no, name, percentage) to create n objects of the Student class.
Accept details from the user for each object.
Define a static method “sortStudent” which sorts the array on the basis of percentage.
*/
import java.util.Scanner;
class Student
{
int rollno;
String name;
float per;
static int count;
Student()
{ }
Student(int rollno, String name, float per)
{
count++;
this.rollno = rollno;
this.name = name;
this.per = per;
}
void display()
{
System.out.println(rollno+"\t"+name+"\t"+per);
}
float getper()
{
return per;
}
static void counter()
{
System.out.println(count+" object is created\n");
}
public static void sortStudent(Student s[],int n)
{
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(s[j].getper()>s[j+1].getper())
{
Student t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
}
for(int i=0;i<n;i++)
s[i].display();
}
}
class Ass2_SetA_b
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of Student:");
int n=sc.nextInt();
Student p[]=new Student[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter Roll number:");
int rollno = sc.nextInt();
System.out.print("Enter Name:");
String name = sc.next();
System.out.print("Enter percentage:");
float per = sc.nextFloat();
p[i]=new Student(rollno,name,per);
p[i].counter();
}
System.out.println("\nSorting Students on the basis of percentage:");
System.out.println("\nRollNo\t Name \t Percentage");
Student.sortStudent(p,Student.count);
}
}
/* OUTPUT:
Enter no. of Student:
5
Enter Roll number:1
Enter Name:Swapnil
Enter percentage:95
1 object is created
Enter Roll number:2
Enter Name:Tony
Enter percentage:99
2 object is created
Enter Roll number:3
Enter Name:Omkar
Enter percentage:88
3 object is created
Enter Roll number:4
Enter Name:Avi
Enter percentage:77
4 object is created
Enter Roll number:5
Enter Name:Raju
Enter percentage:50
5 object is created
Sorting Students on the basis of percentage:
RollNo Name Percentage
5 Raju 50.0
4 Avi 77.0
3 Omkar 88.0
1 Swapnil 95.0
2 Tony 99.0
*/
/*
Write a java program to accept 5 numbers using command line arguments
sort and display them.
*/
import java.util.Arrays;
public class Ass2_SetA_c
{
public static void main(String[] args)
{
System.out.println("The command line argument are:");
for(int i=0; i<args.length; i++)
{
System.out.println(args[i]+ " ");
}
System.out.println("After Sorting the element is : ");
for(int i=0; i<args.length; i++)
{
Arrays.sort(args);
}
for(int i=0; i<args.length; i++)
{
System.out.println(args[i]);
}
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass2_SetA_c.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass2_SetA_c 5 2 4 3 1
The command line argument are:
5
2
4
3
1
After Sorting the element is :
1
2
3
4
5
*/
/*
Write a java program that take input as a person name in the format of first, middle and last name
and then print it in the form last, first and middle name,
where in the middle name of first character is capital letter.
*/
import java.util.Scanner;
public class Ass2_SetA_d
{
String fname,mname,lname;
int length;
void accept()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter First Name :");
fname=s.next();
System.out.println("Enter Middle Name :");
mname=s.next();
System.out.println("Enter Last Name :");
lname=s.next();
length = mname.length();
String FirstChar = mname.substring(0,1);
String RemainingStr = mname.substring(1,length);
FirstChar = FirstChar.toUpperCase();
mname = FirstChar + RemainingStr;
}
void display()
{
System.out.println("Last Name :"+lname);
System.out.println("First Name :"+fname);
System.out.println("Middle Name :"+mname);
}
public static void main(String args[])
{
Ass2_SetA_d person=new Ass2_SetA_d();
person.accept();
System.out.println("\n Full name is:");
person.display();
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass2_SetA_d.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass2_SetA_d
Enter First Name :
swapnil
Enter Middle Name :
dattatraya
Enter Last Name :
jamble
Full name is:
Last Name : jamble
First Name : swapnil
Middle Name : Dattatraya
*/
Assignment 2: Set B
Write a Java program to create a Package “SY” which has a class SYMarks (members – ComputerTotal, MathsTotal, and ElectronicsTotal).
Create another package TY which has a class TYMarks (members – Theory, Practicals).
Create n objects of Student class (having rollNumber, name, SYMarks and TYMarks).
Add the marks of SY and TY computer subjects and calculate the Grade (‘A’ for >= 70, ‘B’ for >= 60 ‘C’ for >= 50 , Pass Class for > =40 else ‘FAIL’)
and display the result of the student in proper format.
1)
package SYMarks;
import java.util.Scanner;
public class syclass {
public int Computer,Math,Electronics;
public syclass(){}
public void get()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter marks of students for Computer, Maths and Electronics subject out of 200 ");
Computer=sc.nextInt();
Math = sc.nextInt();
Electronics = sc.nextInt();
}
}
Assignment 2: Set B
2)
package TYMarks;
import java.util.Scanner;
public class tyclass
{
public int TheoryMarks,PracticalMarks;
public tyclass() {}
public void get() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the marks of the Theory out of 400 and Practicals out of 200: ");
TheoryMarks = sc.nextInt();
PracticalMarks = sc.nextInt();
}
}
Assignment 2: Set B
3)
/*
Write a Java program to create a Package “SY” which has a class SYMarks (members – ComputerTotal, MathsTotal, and ElectronicsTotal).
Create another package TY which has a class TYMarks (members – Theory, Practicals).
Create n objects of Student class (having rollNumber, name, SYMarks and TYMarks).
Add the marks of SY and TY computer subjects and calculate the Grade (‘A’ for >= 70, ‘B’ for >= 60 ‘C’ for >= 50 , Pass Class for > =40 else ‘FAIL’)
and display the result of the student in proper format.
*/
// program for n object of Student-
package sytyclass;
import SYMarks.syclass;
import TYMarks.tyclass;
import java.util.Scanner;
class StudentInfo{
int rollno;
String name,grade;
public float gt,tyt,syt;
public float per;
public void get()
{
System.out.println("Enter roll number and name of the student: ");
Scanner sc = new Scanner(System.in);
rollno=sc.nextInt();
name = sc.next();
}
}
public class mainclass {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of students:");
int n= sc.nextInt();
syclass sy[]=new syclass[n];
tyclass ty[]=new tyclass[n];
StudentInfo si[]=new StudentInfo[n];
for(int i=0;i<n;i++)
{
si[i]=new StudentInfo();
sy[i]=new syclass();
ty[i]=new tyclass();
si[i].get();
sy[i].get();
ty[i].get();
si[i].syt=sy[i].Computer+sy[i].Electronics+sy[i].Math;
si[i].tyt=ty[i].PracticalMarks+ty[i].TheoryMarks;
si[i].gt=si[i].syt+si[i].tyt;
si[i].per=(si[i].gt/1200)*100;
if(si[i].per>=70) si[i].grade="A";
else if(si[i].per>=60) si[i].grade="B";
else if(si[i].per>=50) si[i].grade="C";
else if(si[i].per>=40) si[i].grade="Pass";
else si[i].grade="Fail";
}
System.out.println("Roll No\tName\tSyTotal\tTyTotal\tGrandTotal\tPercentage\tGrade");
for(int i=0;i<n;i++)
{
System.out.println(si[i].rollno+"\t"+si[i].name+"\t"+si[i].syt+"\t"+si[i].tyt+"\t"+si[i].gt+"\t\t"+si[i].per+"\t\t"+si[i].grade);
}
}
}
/* OUTPUT:
C:\Users\Swapniljamble\Documents\javapad programs>javac -d . syclass.java
C:\Users\Swapniljamble\Documents\javapad programs>javac -d . tyclass.java
C:\Users\Swapniljamble\Documents\javapad programs>javac -d . mainclass.java
C:\Users\Swapniljamble\Documents\javapad programs>java sytyclass.mainclass
Enter the number of students:
3
Enter roll number and name of the student:
50
swapnil
Enter marks of students for Computer, Maths and Electronics subject out of 200
155
66
190
Enter the marks of the Theory out of 400 and Practicals out of 200:
350
150
Enter roll number and name of the student:
10
rahul
Enter marks of students for Computer, Maths and Electronics subject out of 200
90
100
150
Enter the marks of the Theory out of 400 and Practicals out of 200:
300
100
Enter roll number and name of the student:
20
viraj
Enter marks of students for Computer, Maths and Electronics subject out of 200
55
22
51
Enter the marks of the Theory out of 400 and Practicals out of 200:
100
120
Roll No Name SyTotal TyTotal GrandTotal Percentage Grade
50 swapnil 411.0 500.0 911.0 75.916664 A
10 rahul 340.0 400.0 740.0 61.666668 B
20 viraj 128.0 220.0 348.0 29.0 Fail
C:\Users\Swapniljamble\Documents\javapad programs>
/*
Define a class CricketPlayer (name,no_of_innings,no_of_times_notout, totatruns, bat_avg).
Create an array of n player objects .Calculate the batting average for each player using static method avg().
Define a static sort method which sorts the array on the basis of average.
Display the player details in sorted order.
*/
import java.util.Scanner;
class Cricket {
String name;
int inning, tofnotout, totalruns;
float batavg;
public Cricket(){
name=null;
inning=0;
tofnotout=0;
totalruns=0;
batavg=0;
}
public void get()
{
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter the name, no of innings, no of times not out, total runs: ");
name=sc.next();
inning = sc.nextInt();
tofnotout = sc.nextInt();
totalruns = sc.nextInt();
}
public void put(){
System.out.println("\nName :"+name);
System.out.println("no of innings :"+inning);
System.out.println("no times notout:"+tofnotout);
System.out.println("total runs :"+totalruns);
System.out.println("bat avg :"+batavg);
}
static void avg(int n, Cricket c[])
{
try
{
for(int i=0;i<n;i++)
{
c[i].batavg=c[i].totalruns/c[i].inning;
}
}
catch(ArithmeticException e)
{
System.out.println("Invalid Arguments...");
}
}
static void sort(int n, Cricket c[])
{
String temp1;
int temp2,temp3,temp4;
float temp5;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(c[i].batavg<c[j].batavg)
{
temp1=c[i].name;
c[i].name=c[j].name;
c[j].name=temp1;
temp2=c[i].inning;
c[i].inning=c[j].inning;
c[j].inning=temp2;
temp3=c[i].tofnotout;
c[i].tofnotout=c[j].tofnotout;
c[j].tofnotout=temp3;
temp4=c[i].totalruns;
c[i].totalruns=c[j].totalruns;
c[j].totalruns=temp4;
temp5=c[i].batavg;
c[i].batavg=c[j].batavg;
c[j].batavg=temp5;
}
}
}
}
}
public class Ass2_SetB_b
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of Cricket Player:");
int n = sc.nextInt();
Cricket c[]=new Cricket[n];
for(int i=0;i<n;i++)
{
c[i]=new Cricket();
c[i].get();
}
Cricket.avg(n,c);
Cricket.sort(n, c);
for(int i=0;i<n;i++)
{
c[i].put();
}
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass2_SetB_b.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass2_SetB_b
Enter the number of Cricket Player:
3
Enter the name, no of innings, no of times not out, total runs:
Swapnil
50
30
1000
Enter the name, no of innings, no of times not out, total runs:
Avi
60
50
3000
Enter the name, no of innings, no of times not out, total runs:
Tony
30
10
500
Name :Avi
no of innings :60
no times notout:50
total runs :3000
bat avg :50.0
Name :Swapnil
no of innings :50
no times notout:30
total runs :1000
bat avg :20.0
Name :Tony
no of innings :30
no times notout:10
total runs :500
bat avg :16.0
*/
ASSIGNMENT 3:
/*
Write a program for multilevel inheritance such that country is inherited from continent.
State is inherited from country. Display the place, state, country and continent.
*/
import java.util.Scanner;
class Contient
{
String continent;
Scanner sc = new Scanner(System.in);
public void coninput()
{
System.out.println("Enter the Continent: ");
continent =sc.next();
}
}
class Country extends Contient
{
String country;
public void couinput()
{
System.out.println("Enter the Country: ");
country = sc.next();
}
}
class State extends Country
{
String state;
public void stateinput()
{
System.out.println("Enter the State: ");
state = sc.next();
}
}
class Multilevel_Inheritance extends State
{
String area;
public void AreaInput()
{
System.out.println("Enter the Area: ");
area = sc.next();
}
}
public class Ass3_SetA_a
{
public static void main(String[] args)
{
Multilevel_Inheritance obj = new Multilevel_Inheritance();
obj.coninput();
obj.couinput();
obj.stateinput();
obj.AreaInput();
System.out.println("Continent : "+obj.continent);
System.out.println("Country : "+obj.country);
System.out.println("State : "+obj.state);
System.out.println("Area : "+obj.area);
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass3_SetA_a.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass3_SetA_a
Enter the Continent:
Asia
Enter the Country:
India
Enter the State:
Maharashtra
Enter the Area:
Supe
Continent : Asia
Country : India
State : Maharashtra
Area : Supe
*/
/*
Define an abstract class Staff with protected members id and name.
Define a parameterized constructor. Define one subclass OfficeStaff with member department.
Create n objects of OfficeStaff and display all details.
*/
import java.util.*;
abstract class Staff
{
protected int id;
protected String name;
Staff(int id, String name)
{
this.id = id;
this.name = name;
}
abstract public void display();
}
class OfficeStaff extends Staff
{
String dept;
OfficeStaff(int id, String name, String dept)
{
super(id, name);
this.dept = dept;
}
public void display()
{
System.out.println("\t|\t" + id + "\t\t" + name + "\t\t" + dept + "\t\t" + "|");
System.out.println("------------------------------------------------------------------------");
}
}
public class Ass3_SetA_b
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Total number of Employee: ");
int n = sc.nextInt();
OfficeStaff s[] = new OfficeStaff[n];
for (int i = 0; i < n; i++)
{
System.out.println("\nEnter " + (i + 1) + " number employee Details: \n");
System.out.println("Enter ID: ");
int id = sc.nextInt();
sc.nextLine();
System.out.println("Enter Employee Name: ");
String name = sc.nextLine();
System.out.println("Enter Dept Name: ");
String dept = sc.nextLine();
s[i] = new OfficeStaff(id, name, dept);
}
System.out.println("\t\t\t\t Employee Details");
System.out.println("------------------------------------------------------------------------");
System.out.println("\t|\tID\t\tName\t\tDept\t\t|");
System.out.println("------------------------------------------------------------------------");
for (int i = 0; i < n; i++)
{
s[i].display();
}
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass3_SetA_b.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass3_SetA_b
Enter Total number of Employee:
3
Enter 1 number employee Details:
Enter ID:
1
Enter Employee Name:
Swapnil
Enter Dept Name:
Computer Science
Enter 2 number employee Details:
Enter ID:
2
Enter Employee Name:
Omkar
Enter Dept Name:
Air Force
Enter 3 number employee Details:
Enter ID:
3
Enter Employee Name:
Raju Bhai
Enter Dept Name:
Navy
Employee Details
------------------------------------------------------------------------
| ID Name Dept |
------------------------------------------------------------------------
| 1 Swapnil Computer Science |
------------------------------------------------------------------------
| 2 Omkar Air Force |
------------------------------------------------------------------------
| 3 Raju Bhai Navy |
------------------------------------------------------------------------
*/
import java.util.Scanner;
interface operation
{
void area();
void volume();
final double PI = 3.142;
}
class cylinder implements operation
{
int r;
int h;
cylinder(int r, int h)
{
this.r=r;
this.h=h;
}
public void area()
{
System.out.println("Area :" + (2 * PI * r * h + 2 * PI * r * r) +" sq units\n");
}
public void volume()
{
System.out.println("Volume : " + (PI * r * r * h) +" cubic units\n");
}
}
public class Ass3_SetA_c
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius: ");
int r = sc.nextInt();
System.out.println("Enter height: ");
int h = sc.nextInt();
cylinder c = new cylinder(r,h);
c.area();
c.volume();
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass3_SetA_c.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass3_SetA_c
Enter radius:
5
Enter height:
9
Area :439.88 sq units
Volume : 706.9499999999999 cubic units
*/
/*
Write a program to find the cube of given number using function interface.
*/
import java.util.*;
interface function
{
void cube(int n);
}
class demo implements function
{
public void cube(int n)
{
System.out.println("Cube :" + (n * n * n));
}
}
public class Ass3_SetA_d
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number for finding cube: ");
int n = sc.nextInt();
function d = new demo();
d.cube(n);
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass3_SetA_d.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass3_SetA_d
Enter the number for finding cube:
5
Cube :125
*/
/* Set B
Create an abstract class order having members id,description.
Create two subclasses Purchase Order and Sales Order having members customer name and Vendor name respectively.
Define methods accept and display in all cases.
Create 3 objects each of Purchase Order and Sales Order and accept and display details.
*/
import java.util.*;
abstract class order
{
int id;
String descp;
Scanner sc = new Scanner(System.in);
public void setData(int id, String descp)
{
this.id=id;
this.descp=descp;
}
abstract public void accept();
abstract public void display();
}
class purchase_order extends order
{
String cname;
public void accept()
{
System.out.println("Enter Customer Name :");
String n = sc.nextLine();
cname=n;
}
public void display()
{
System.out.println("\t"+id +"\t"+descp+"\t\t"+cname);
}
}
class sales_order extends order
{
String vname;
public void accept()
{
System.out.println("Enter Vendor Name :");
String n = sc.nextLine();
vname=n;
}
public void display(){
System.out.println("\t"+id +"\t"+descp+"\t\t"+vname);
}
}
public class Ass3_SetB_a
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
purchase_order p[] = new purchase_order[3];
for (int i = 0; i < 3; i++)
{
p[i] = new purchase_order();
System.out.println("\nEnter "+(i+1)+" Customer Data :");
System.out.println("Enter ID :");
int cid = sc.nextInt();
sc.nextLine();
System.out.println("Enter Description :");
String desc = sc.nextLine();
p[i].setData(cid, desc);
p[i].accept();
}
System.out.println("\n\t\tPurchased Details.\n");
System.out.println("\tID\tDescription\tCname");
for (int i = 0; i < 3; i++)
{
p[i].display();
}
sales_order s[] = new sales_order[3];
for (int i = 0; i < 3; i++)
{
s[i] = new sales_order();
System.out.println("\nEnter "+(i+1)+" Vendor Data :");
System.out.println("Enter ID :");
int cid = sc.nextInt();
sc.nextLine();
System.out.println("Enter Description :");
String desc = sc.nextLine();
s[i].setData(cid, desc);
s[i].accept();
}
System.out.println("\n\t\tSales Details.\n");
System.out.println("\tID\tDescription\tVname");
for (int i = 0; i < 3; i++)
{
s[i].display();
}
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass3_SetB_a.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass3_SetB_a
Enter 1 Customer Data :
Enter ID :
10
Enter Description :
laptop
Enter Customer Name :
Mr.Tony
Enter 2 Customer Data :
Enter ID :
11
Enter Description :
TV
Enter Customer Name :
Mr.Saurabh
Enter 3 Customer Data :
Enter ID :
12
Enter Description :
Mobile
Enter Customer Name :
Mr.Sanket
Purchased Details.
ID Description Cname
10 laptop Mr.Tony
11 TV Mr.Saurabh
12 Mobile Mr.Sanket
Enter 1 Vendor Data :
Enter ID :
1
Enter Description :
Mobile
Enter Vendor Name :
Mr.Rahul
Enter 2 Vendor Data :
Enter ID :
2
Enter Description :
laptop
Enter Vendor Name :
Mr.Virat
Enter 3 Vendor Data :
Enter ID :
3
Enter Description :
TV
Enter Vendor Name :
Mr.Kedar
Sales Details.
ID Description Vname
1 Mobile Mr.Rahul
2 laptop Mr.Virat
3 TV Mr.Kedar
*/
/*
Write a program to using marker interface create a class product(product_id, product_name, product_cost, product_quantity) define a default and parameterized constructor. Create objects of class product and display the contents of each object and Also display the object count.
*/
import java.util.*;
interface MarkerInt {
}
class product implements MarkerInt {
int pid, pcost, quantity;
String pname;
static int cnt;
// Default constructor
product() {
pid = 1;
pcost = 10;
quantity = 1;
pname = "pencil";
cnt++;
}
// Parameterized constructor
product(int id, String n, int c, int q) {
pid = id;
pname = n;
pcost = c;
quantity = q;
cnt++;
System.out.println("\nCOUNT OF OBJECT IS : " + cnt + "\n");
}
public void display() {
System.out.println("\t" +pid + "\t" + pname + "\t" + pcost + "\t" + quantity);
}
}
public class Ass3_SetB_b
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Product : ");
int n = sc.nextInt();
product pr[] = new product[n];
for (int i = 0; i < n; i++) {
System.out.println("\nEnter " + (i + 1) + " Product Details :\n");
System.out.println("Enter Product ID: ");
int pid = sc.nextInt();
sc.nextLine();
System.out.println("Enter Product Name: ");
String pn = sc.nextLine();
System.out.println("Enter Product Cost:");
int pc = sc.nextInt();
System.out.println("Enter Product Quantity:");
int pq = sc.nextInt();
pr[i] = new product(pid, pn, pc, pq);
}
System.out.println("\n\t\t Product Details\n");
System.out.println("\tId\tPname\tCost\tQuantity\n");
for (int i = 0; i < n; i++)
{
pr[i].display();
}
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass3_SetB_b.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass3_SetB_b
Enter Number of Product :
3
Enter 1 Product Details :
Enter Product ID:
50
Enter Product Name:
Laptop
Enter Product Cost:
50000
Enter Product Quantity:
2
COUNT OF OBJECT IS : 1
Enter 2 Product Details :
Enter Product ID:
10
Enter Product Name:
TV
Enter Product Cost:
30000
Enter Product Quantity:
1
COUNT OF OBJECT IS : 2
Enter 3 Product Details :
Product Details
Id Pname Cost Quantity
50 Laptop 50000 2
10 TV 30000 1
20 Mobile 100000 5
*/
/*
Create an interface Department containing attributes deptName and deptHead.
It also has abstract methods for printing the attributes.
Create a class hostel containing hostelName, hostelLocation and numberOfRooms.
The class contains method printing the attributes.
Then write Student class extending the Hostel class and implementing the Department interface.
This class contains attributes studentName, regdNo, electiveSubject and avgMarks.
Write suitable printData method for this class. Also, implement the abstract methods of the Department interface.
Write a driver class to test the Student class. The program should be menu driven containing the options:
1. Admit new student
2. Migrate a student
3. Display details of a student
For the third option , a search is to be made on the basis of the entered registration Number.
*/
import java.util.Scanner;
interface Department
{
public void getDeptName();
public void getDeptHead();
}
class Hostel
{
protected String hname,hlocation;
int noofroom;
void getHostelName()
{
System.out.println("Name Of the Hostel : " + hname);
}
void getHostelLocation()
{
System.out.println("Hostel Location : " + hlocation);
}
void getNoOfRoom()
{
System.out.println("Total Room : " + noofroom);
}
}
class Student extends Hostel implements Department
{
String sname,regno,elesub;
String deptName,deptHead;
int avgMarks;
void getStudentName()
{
System.out.println("Student : " + sname);
}
String getStudentRegNo()
{
return regno;
}
void getElectiveSubject()
{
System.out.println("Elective Subject : " + elesub);
}
void getAvgMarks()
{
System.out.println("Average Marks : " + avgMarks);
}
public void getDeptName()
{
System.out.println("Department Name : " + deptName);
}
public void getDeptHead()
{
System.out.println("Department Head : " + deptHead);
}
void addStudent()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Student name : ");
sname=sc.nextLine();
System.out.print("Enter Registration Number : ");
regno=sc.nextLine();
System.out.print("Enter Elective Subject : ");
elesub=sc.nextLine();
System.out.print("Enter Hostel Name : ");
hname=sc.nextLine();
System.out.print("Enter Hostel Location : ");
hlocation=sc.nextLine();
System.out.print("Enter Department Name : ");
deptName=sc.nextLine();
System.out.print("Enter Department Head : ");
deptHead=sc.nextLine();
System.out.print("Enter No of room : ");
noofroom=sc.nextInt();
System.out.print("Enter Avg Marks : ");
avgMarks=sc.nextInt();
}
void migrate()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter new Department Name : ");
deptName=sc.nextLine();
System.out.print("Enter new Department Head : ");
deptHead=sc.nextLine();
}
void display()
{
getStudentName();
System.out.println("Student Registration No is : " + getStudentRegNo());
getElectiveSubject();
getAvgMarks();
getDeptName();
getDeptHead();
}
}
public class Ass3_SetC_a
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
Student []st=new Student[100];
int sno=0;
String rno;
int ch;
boolean b;
while(true)
{
System.out.println("\n 1. Admit a student");
System.out.println(" 2. Migrate a student");
System.out.println(" 3. Display");
System.out.println(" 4. Exit");
System.out.println("\n Pls enter Your Choice:");
ch = sc.nextInt();
switch(ch)
{
case 1:
st[sno]=new Student();
st[sno++].addStudent();
break;
case 2:
System.out.println("\nEnter Registration no : ");
rno=sc.next();
b=false;
for(int i=0;i<sno;i++)
{
if(st[i].getStudentRegNo().equals(rno))
{
b=true;
st[i].migrate();
break;
}
}
if(b==false)
{
System.out.println("\nStudent Not Found");
}
break;
case 3:
System.out.println("\nEnter Registration no : ");
rno = sc.next();
b=false;
for(int i=0;i<sno;i++)
{
if(st[i].getStudentRegNo().equals(rno))
{
b=true;
st[i].display();
break;
}
}
if(b==false)
{
System.out.println("Student Not Found");
}
break;
case 4:
System.exit(0);
default:
System.out.println("--Invalid Entry--");
}
}
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass3_SetC_a.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass3_SetC_a
1. Admit a student
2. Migrate a student
3. Display
4. Exit
Pls enter Your Choice:
3
Enter Registration no :
50
Student Not Found
1. Admit a student
2. Migrate a student
3. Display
4. Exit
Pls enter Your Choice:
1
Enter Student name : Swapnil
Enter Registration Number : 50
Enter Elective Subject : Computer Network
Enter Hostel Name : A
Enter Hostel Location : Supe
Enter Department Name : Computer Science
Enter Department Head : Dr.Ladkat
Enter No of room : 500
Enter Avg Marks : 88
1. Admit a student
2. Migrate a student
3. Display
4. Exit
Pls enter Your Choice:
1
Enter Student name : Saurabh
Enter Registration Number : 1
Enter Elective Subject : Math
Enter Hostel Name : B
Enter Hostel Location : Supe
Enter Department Name : Mathematics
Enter Department Head : Dr.Jamble
Enter No of room : 300
Enter Avg Marks : 95
1. Admit a student
2. Migrate a student
3. Display
4. Exit
Pls enter Your Choice:
3
Enter Registration no :
1
Student : Saurabh
Student Registration No is : 1
Elective Subject : Math
Average Marks : 95
Department Name : Mathematics
Department Head : Dr.Jamble
1. Admit a student
2. Migrate a student
3. Display
4. Exit
Pls enter Your Choice:
3
Enter Registration no :
50
Student : Swapnil
Student Registration No is : 50
Elective Subject : Computer Network
Average Marks : 88
Department Name : Computer Science
Department Head : Dr.Ladkat
1. Admit a student
2. Migrate a student
3. Display
4. Exit
Pls enter Your Choice:
2
Enter Registration no :
50
Enter new Department Name : Google.com
Enter new Department Head : Dr.Sundar Pichai
1. Admit a student
2. Migrate a student
3. Display
4. Exit
Pls enter Your Choice:
3
Enter Registration no :
50
Student : Swapnil
Student Registration No is : 50
Elective Subject : Computer Network
Average Marks : 88
Department Name : Google.com
Department Head : Dr.Sundar Pichai
1. Admit a student
2. Migrate a student
3. Display
4. Exit
Pls enter Your Choice:
4
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src>
*/
ASSIGNMENT 4:
/*
a) Define a class patient (patient_name, patient_age,patient_oxy_level,patient_HRCT_report).
Create an object of patient.
Handle appropriate exception while patient oxygen level less than 95% and
HRCT scan report greater than 10,
then throw user defined Exception " Patient is Covid Positive(+) and Need to Hospitalized"
otherwise display its information.
*/
import java.io.*;
import java.util.Scanner;
class LessOxygenException extends Exception
{
public String toString()
{
return "Patient is covid Positive";
}
}
class MoreHRCTException extends Exception
{
public String toString()
{
return "Need to hospitalize";
}
}
class Patient
{
String pname;
int page,olevel,hrct;
void accept() throws LessOxygenException, MoreHRCTException
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the name of Patient :");
pname = s.next();
System.out.println("Enter the age of Patient :");
page = s.nextInt();
System.out.println("Enter the Oxygen level :");
olevel = s.nextInt();
if (olevel<95)
{
throw new LessOxygenException();
}
System.out.println("Enter the HRCT level :");
hrct = s.nextInt();
if (hrct>10)
{
throw new MoreHRCTException();
}
}
void display()
{
System.out.println("\nPatient infromation:");
System.out.println("Patient Name:" + pname);
System.out.println("Patient Age:" + page);
System.out.println("Patient Oxygen Level:" + olevel);
System.out.println("Patient HRCT:" + hrct);
}
}
public class Ass4_SetA_a
{
public static void main(String[] args)
{
Patient p = new Patient();
try{
p.accept();
p.display();
}
catch (LessOxygenException ol)
{
System.out.println(ol);
}
catch (MoreHRCTException hl)
{
System.out.println(hl);
}
}
}
/* OUTPUT:
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> javac Ass4_SetA_a.java
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass4_SetA_a
Enter the name of Patient :
Swapnil
Enter the age of Patient :
23
80
Patient is covid Positive
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass4_SetA_a
Enter the name of Patient :
Saurabh
Enter the age of Patient :
24
Enter the Oxygen level :
100
Enter the HRCT level :
15
Need to hospitalize
PS C:\Users\Swapniljamble\IdeaProjects\demo1\helloj\src> java Ass4_SetA_a
Enter the name of Patient :
Omkar
Enter the age of Patient :
22
Enter the Oxygen level :
100
Enter the HRCT level :
7
Patient infromation:
Patient Name:Omkar
Patient Age:22
Patient Oxygen Level:100
Patient HRCT:7
*/
Assignment 5:
Set B
b)
Program Name: Email.java
/* ) Write a Java program to design a screen using Awt that will take a user name and
password. If the user name and password are not same, raise an Exception with
appropriate message. User can have 3 login chances only. Use clear button to clear
the TextFields.
*/
import javax.naming.InvalidNameException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class InvalidPasswordException extends Exception
{
InvalidPasswordException()
{
System.out.println("Invalid Name and Password!!!");
}
}
public class Email extends Frame implements ActionListener {
Label userNameLabel, userPasswordLabel;
Button Login, Clear;
TextField username, password, message;
Panel p;
char ch = '*';
int attempt = 0;
void setMethod() {
p = new Panel();
setTitle("Email");
setSize(350, 300);
setVisible(true);
setResizable(false);
userNameLabel = new Label("Enter the UserName:");
userPasswordLabel = new Label("Enter the UserPassword:");
username = new TextField(20);
password = new TextField(20);
password.setEchoChar(ch);
message = new TextField(20);
message.setEnabled(false);
Login = new Button("Login");
Clear = new Button("Clear");
p.add(userNameLabel);
p.add(username);
p.add(userPasswordLabel);
p.add(password);
p.add(Login);
p.add(Clear);
p.add(message);
add(p);
Login.addActionListener(this);
Clear.addActionListener(this);
}
public static void main(String[] args) {
Email obj = new Email();
obj.setMethod();
}
public void actionPerformed(ActionEvent ae) {
try
{
if(attempt < 3)
{
if (ae.getSource() == Clear)
{
username.setText("");
password.setText("");
username.requestFocus();
}
if (ae.getSource() == Login)
{
String uname = username.getText();
String pname = password.getText();
if (uname.compareTo(pname) == 0)
{
message.setText("Valid");
System.out.println("Email is Valid..");
}
else
{
message.setText("Invalid");
throw new InvalidPasswordException();
}
}
attempt++;
}
else
{
System.out.println("Attempt is greater than 3!!!");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
s
nice
ReplyDeleteimport java.util.Scanner;
ReplyDeleteclass continent
{
String Continent_name;
continent()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the continent name:");
Continent_name= sc.next();
}
}
class country extends continent
{
String Country_name;
country()
{
super();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the country name:");
Country_name = sc.next();
}
}
class state extends country
{
String State_name;
state()
{
super();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the state name:");
State_name = sc.next();
}
void display()
{
System.out.println("The continent name is:"+super.Continent_name);
System.out.println("The Country name is:"+super.Country_name);
System.out.println("The State name is:"+State_name);
}
}
public class Multilevel_Inheritance
{
public static void main(String arg[])
{
System.out.println("Multilevel Inheritance");
state obj1 = new state();
obj1.display();
}
}