Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Final Printout

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 67

Prac. No.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Date

Topic
Write a JAVA program to sort numbers given by an array. Write a JAVA program to find mean and Std. Deviation of an array of numbers. Write a JAVA program to display 3x3 matrix. Write a JAVA program to find sum of two matrix. Write a JAVA program to find product of two matrix. Write a JAVA program to find transpose of a matrix. Write a JAVA program to find all divisors of a number. Write a JAVA program to check whether a number is a perfect number or not. Write a JAVA program to check whether a quadratic equation has real or complex roots. Write a JAVA program to check whether a year is a leap year or not Write a JAVA program to check whether a number is divisible by 5 or not. Write a JAVA program to find a factorial of a number. Write a JAVA program to find a number which is divisible by 4 or 6 but not both. Write a JAVA program to display all nos. between 1 and a given number which is divisible by 5. Write a JAVA program to display all nos. between 1 and a given number which is divisible by 6 or 8. Write a JAVA program to display all nos. between 1 and a given number which is divisible by 6 or 8 but not both. Write a JAVA program to find the sum of the sequence 1+1/2+1/4+1/8+...........+1/2n Write a JAVA program to find simple and compound interest using method overloading. Write a JAVA program to find area of a square and a rectangle using method overloading. Write a JAVA program to display appropriate messages using method overloading. Write a JAVA program to display report card of some students using method overloading. Write a JAVA program to calculate the volume of a cube and a cuboid using inheritance. Write a JAVA program to display appropriate messages using inheritance. Write a JAVA program to illustrate the concept of interface. Write an applet program to display the message What is your name? in an applet window. Write an applet program to display the face of a person. Write an applet program to insert username and password of a person and to display both. Write an applet program accept some elements and to the display the necessary output in the applet window.

Sign

28

29

Write an applet program that tracks the position of the mouse

Page 1 of 67

Practical No.: 1 Write a Java program to sort the numbers given by an array Code: public class Sort { public static void main(String ar[]) { int numbers[]= new int[5]; int k=0; System.out.print("Given List:"); for(int i=0; i<5; i++) { numbers[i]=Integer.parseInt(ar[k]); k++; System.out.print(" "+numbers[i]); } System.out.println("\n"); for(int i=0; i<5; i++) { for(int j=i+1; j<5; j++) { if(numbers[i]<numbers[j]) { int temp=numbers[i]; numbers[i]=numbers[j]; numbers[j]=temp; } } } System.out.print("Sorted List:"); for(int i=0; i<5; i++) { System.out.print(" "+numbers[i]); } System.out.println(" "); } }

Page 2 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Sort 12 78 26 99 5 Given List: 12 78 26 99 5 Sorted List: 99 78 26 12 5

Page 3 of 67

Practical No.:2 Write a Java program to find mean and standard deviation of an array of numbers. Code: class Mean { public static void main(String ar[]) { int numbers[]= new int[5]; int k=0; System.out.print("Given List:"); for(int i=0; i<5; i++) { numbers[i]=Integer.parseInt(ar[k]); k++; System.out.print(" "+numbers[i]); } System.out.println("\n"); double mean, sum=0; for(int i=0;i<5;i++) { sum=sum+numbers[i]; } mean=sum/5; System.out.println("Mean of given numbers is: "+mean); double dev,avg=0; for (int i = 0; i < 5; i++) { avg += Math.pow((numbers[i] - mean),2); } dev = Math.sqrt(avg / (5)); System.out.println("Standard deviation of given numbers is: "+dev); } }

Page 4 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Mean 11 12 33 54 75 Given List: 11 12 33 54 75 Mean of given numbers is: 37.0 Standard deviation of given numbers is: 24.698178070456937

Page 5 of 67

Practical No.:3 Write a Java program to display 3 x 3 matrix. Code: class Matrix { public static void main(String[] args) { int i,j,k; int a[][]=new int[3][3]; k=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { a[i][j]=Integer.parseInt(args [k]); k++; } } System.out.println("Matrix Elements are:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { System.out.print(a[i][j]+" "); } System.out.println(" "); } } }

Page 6 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Matrix 12 45 85 69 45 21 36 25 45 Matrix Elements are: 12 45 85 69 45 21 36 25 45

Page 7 of 67

Practical No.: 4 To initialize 2 two dimensional array of order 2X3 and to find their sum. The program should display the three arrays. Code: class MatSum { public static void main(String ar[]) { int i,j,k=0; int a[][]=new int[2][3]; int b[][]=new int[2][3]; int c[][]=new int[2][3]; for(i=0;i<2;i++) { for(j=0;j<3;j++) { a[i][j]=Integer.parseInt(ar[k]); k++; } } for(i=0;i<2;i++) { for(j=0;j<3;j++) { b[i][j]=Integer.parseInt(ar[k]); k++; } } for(i=0;i<2;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; } } System.out.println("First Matrix"); for(i=0;i<2;i++) { for(j=0;j<3;j++) { System.out.print(a[i][j]+" "); }
Page 8 of 67

System.out.println(" "); } System.out.println("Second Matrix"); for(i=0;i<2;i++) { for(j=0;j<3;j++) { System.out.print(b[i][j]+" "); } System.out.println(" "); } System.out.println("Resultant Matrix"); for(i=0;i<2;i++) { for(j=0;j<3;j++) { System.out.print(c[i][j]+" "); } System.out.println(" "); } } }

Page 9 of 67

Output: C:\Java\jdk1.6.0_12\bin>java MatSum 1 2 3 4 5 6 7 8 9 10 11 12 First Matrix 123 456 Second Matrix 789 10 11 12 Resultant Matrix 8 10 12 14 16 18

Page 10 of 67

Practical No.: 5 To initialize 2 two dimensional array of order 2X2 and to find their product. The program should display the three arrays. Code: class Multiply { public static void main(String[] args) { int i,j,k; int a[][]=new int[2][2]; int b[][]=new int[2][2]; int c[][]=new int[2][2]; k=0; for(i=0;i<2;i++) { for(j=0;j<2;j++) { a[i][j]=Integer.parseInt(args [k]); k++; } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { b[i][j]=Integer.parseInt(args [k]); k++; } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=0; } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { for(k=0;k<2;k++) { c[i][j]+=a[i][k]*b[k][j];
Page 11 of 67

} } } System.out.println("First matrix is:"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { System.out.print(a[i][j]+" "); } System.out.println(" "); } System.out.println("Second matrix is:"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { System.out.print(b[i][j]+" "); } System.out.println(" "); } System.out.println("Multiplied matrix is:"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { System.out.print(c[i][j]+" "); } System.out.println(" "); } } }

Page 12 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Multiply 2 12 22 54 14 18 87 69 First matrix is: 2 12 22 54 Second matrix is: 14 18 87 69 Multiplied matrix is: 1072 864 5006 4122

Page 13 of 67

Practical No.: 6 To initialize a two dimensional array of order 4X3 and to find its transpose. The program should display both the arrays. Code: class Transpose { public static void main(String args[]) { int i,j,k; int a[][]=new int[4][3]; k=0; for(i=0;i<4;i++) { for(j=0;j<3;j++) { a[i][j]=Integer.parseInt(args [k]); k++; } } System.out.println("The given matrix is:"); for(i=0;i<4;i++) { for(j=0;j<3;j++) { System.out.print(a[i][j]+" "); } System.out.println(" "); } System.out.println("Transpose of given matrix is:"); for(j=0;j<3;j++) { for(i=0;i<4;i++) { System.out.print(a[i][j]+" "); } System.out.println(" "); } } }

Page 14 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Transpose 11 21 33 54 45 76 79 48 19 10 25 78 The given matrix is: 11 21 33 54 45 76 79 48 19 10 25 78 Transpose of given matrix is: 11 54 79 10 21 45 48 25 33 76 19 78

Page 15 of 67

Practical No.:7 Create a class called A containing the instance variable y (integer) and defines a constructor to initialize the instance variable and a method in this class that will display all divisors of y. Class B contains the main() and calls the method for a value of your choice. Code: class A { int a; A (int num) { a=num; } void disp() { System.out.println("Divisors of "+a+" are: "); for(int i=1;i<=y;i++) { if(a%i==0) System.out.println(i+" "); } } } class B { public static void main(String args[]) { int num; num=Integer.parseInt(args[0]); A y=new A(num); System.out.println("Enter a number"); y.disp(); } }

Page 16 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B 4587 Enter a number Divisors of 4587 are: 1 3 11 33 139 417 1529 4587

Page 17 of 67

Practical No.: 8

Create a class called A containing the instance variable y (integer) and defines a constructor to initialize the instance variable and a method in this class that will check whether y is a perfect number or not. Class B contains the main() and calls the method for a value of your choice. Code: class A { public int a; public A(int num) { a=num; } void perfect() { int b=0; for(int i=1;i<=a-1;i++) { if(a%i==0) { b=b+i; } } if(a==b) { System.out.println(a+" is a perfect number"); } else System.out.println(a+" is not a perfect number"); } } class B { public static void main(String ar[]) { int num; num=Integer.parseInt(ar[0]); A y=new A(num); System.out.println("Enter a number:"); y.perfect(); }
Page 18 of 67

Page 19 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B 496 Enter a number: 496 is a perfect number C:\Java\jdk1.6.0_12\bin>java B 494 Enter a number: 494 is not a perfect number

Page 20 of 67

Practical No.: 9 Create a class called Quad containing a, b and c as variables, denoting the coefficient of a quadratic equation. The class defines a constructor to initialize these variables and a method that finds whether the quadratic equation for an instance of the class has real roots or complex roots. Create a class Quadratic containing the main() method. Make an instance (object) of the above class to find the nature of the roots and display proper messages. Code: class Quad { private int a,b,c; public Quad(int x,int y,int z) { a=x; b=y; c=z; } void check() { double eq=(b*b)-(4*a*c); if(eq>0) System.out.println("Both roots are real"); if(eq<0) System.out.println("Both roots are Complex"); } } class Quadratic { public static void main(String ar[]) { int x,y,z; x=Integer.parseInt(ar[0]); y=Integer.parseInt(ar[1]); z=Integer.parseInt(ar[2]); Quad q1=new Quad(x,y,z); System.out.println("Enter co-efficients:"); q1.check(); } }

Page 21 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Quadratic 2 1 1 Enter co-efficients: Both roots are Complex C:\Java\jdk1.6.0_12\bin>java Quadratic 1 3 1 Enter co-efficients: Both roots are real

Page 22 of 67

Practical No.:10 Create a class called A containing the instance variable y which denotes a year and defines a constructor to initialize the instance variable and a method that will check whether it is a leap year or not. Class B contains the main() and calls the above method and displays whether y is a leap year or not a leap year. Code class A { public int y; public A(int num) { y=num; } void leap() { if((y%4==0)&&(y%100!=0)||(y%400==0)) System.out.println(y+" is a leap year"); else System.out.println(y+" is not a leap year"); } } class B { public static void main(String ar[]) { int num; num=Integer.parseInt(ar[0]); A a=new A(num); System.out.println("Enter a year"); a.leap(); } }

Page 23 of 67

Output C:\Java\jdk1.6.0_12\bin>java B 2011 Enter a year 2011 is not a leap year C:\Java\jdk1.6.0_12\bin>java B 2005 Enter a year 2005 is not a leap year

Page 24 of 67

Practical No.:11 Create a class called A containing the instance variable y (integer) and defines a constructor to initialize the instance variable and a method in this class that will check whether it is divisible by 5. Class B contains the main() and calls the method for a value of your choice and displays whether y is divisible by 5 or not. Code: class A { public int a; public A(int num) { a=num; } void divisible() { if(a%5==0) System.out.println(a+" is divisible by 5"); else System.out.println(a+" is not divisible by 5"); } } class B3 { public static void main(String ar[]) { int num; num=Integer.parseInt(ar[0]); A y=new A(num); System.out.println("Enter a number"); y.divisible(); } }

Page 25 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B3 1000 Enter a number 1000 is divisible by 5 C:\Java\jdk1.6.0_12\bin>java B3 1001 Enter a number 1001 is not divisible by 5

Page 26 of 67

Practical No.:12 Create a class called A containing the instance variable y (integer) and defines a constructor to initialize the instance variable and a method in this class that will find the factorial of a number. Class B contains the main() and calls the method for a value of your choice and displays the factorial. Code: class A { public int a; public A(int num) { a=num; } void factorial(int num) { double fact=1.0; System.out.println("Factorial of "+num+" is:"); for(int j=1;j<=num;j++) fact =fact*j; System.out.println(fact); } } class B4 { public static void main(String ar[]) { int num; num=Integer.parseInt(ar[0]); A y=new A(num); System.out.println("Enter a number"); y.factorial(num); } }

Page 27 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B4 10 Enter a number Factorial of 10 is: 3628800.0

Page 28 of 67

Practical No.:13 Create a class called A containing the instance variable y (integer) and defines a constructor to initialize the instance variable and a method in this class that will check whether it is divisible by 4 or 6 but not both. Class B contains the main() and calls the method for a value of your choice and displays the proper message. Code: class A { public int a; public A(int num) { a=num; } void divisible() { if((a%4==0)&&((a%4==0)!=(a%6==0))) System.out.println(a+" is divisible by 4"); else if((a%6==0)&&((a%4==0)!=(a%6==0))) System.out.println(a+" is divisible by 6"); if((a%4==0)==(a%6==0)) System.out.println("ERROR"); } } class B5 { public static void main(String ar[]) { int num; num=Integer.parseInt(ar[0]); A y=new A(num); System.out.println("Enter a number"); y.divisible(); } }

Page 29 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B5 24 Enter a number ERROR C:\Java\jdk1.6.0_12\bin>java B5 8 Enter a number 8 is divisible by 4 C:\Java\jdk1.6.0_12\bin>java B5 18 Enter a number 18 is divisible by 6

Page 30 of 67

Practical No.:14 Create a class called A containing the instance variable y (integer) and defines a constructor to initialize the instance variable and a method in this class that will display all numbers between 1 and y that are divisible by 5. Class B contains the main() and calls the method for a value of your choice. Code: class A { public int a; public A(int num) { a=num; } void divisible(int num) { System.out.println("Numbers from 1 to "+num+" divisible by 5 for(int i=1;i<=num;i++) if(i%5==0) System.out.println(i); } } class B6 { public static void main(String ar[]) { int num; num=Integer.parseInt(ar[0]); A y=new A(num); System.out.println("Enter a number"); y.divisible(num); } }

are:");

Page 31 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B6 30 Enter a number Numbers from 1 to 30 divisible by 5 are: 5 10 15 20 25 30

Page 32 of 67

Practical No.:15 Create a class called A containing the instance variable y (integer) and defines a constructor to initialize the instance variable and a method in this class that will display all numbers between 1 and y that are divisible by 6 or 8. Class B contains the main() and calls the method for a value of your choice. Code: class A { public int a; public A(int num) { a=num;} void divisible(int num) { System.out.println("Numbers from 1 to "+num+" divisible by 6 or 8 are:"); for(int i=1;i<=num;i++) if((i%6==0)||(i%8==0)) System.out.println(i); } } class B7 { public static void main(String ar[]) { int num; num=Integer.parseInt(ar[0]); A y=new A(num); System.out.println("Enter a number"); y.divisible(num); } }

Page 33 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B7 30 Enter a number Numbers from 1 to 30 divisible by 6 or 8 are: 6 8 12 16 18 24 30

Page 34 of 67

Practical No.:16 Create a class called A containing the instance variable y (integer) and defines a constructor to initialize the instance variable and a method in this class that will display all numbers between 1 and y that are divisible by 6 or 8 not both. Class B contains the main() and calls the method for a value of your choice. Code: class A { public int a; public A(int num) { a=num; } void divisible(int num) { System.out.println("Numbers from 1 to "+num+" divisible by 6 or 8 are:"); for(int i=1;i<=num;i++) if(((i%6==0)||(i%8==0))&&((i%6==0)!=(i%8==0))) { System.out.println(i); } } } class B8 { public static void main(String ar[]) { int num; num=Integer.parseInt(ar[0]); A y=new A(num); System.out.println("Enter a number"); y.divisible(num); } }

Page 35 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B8 30 Enter a number Numbers from 1 to 30 divisible by 6 or 8 are: 6 8 12 16 18 30

Page 36 of 67

Practical No.:17 Create a class called A containing instance variable n and defines a constructor to initialize the instance variable and a method in this class that will find the following sum 1 + 1 / 2 + 1 / 4 + 1 / 8 + . . . . 1 / 2n. Class B contains the main() and calls the method and displays the sum. Code: class A { int a; A (int n) { a=n; } void calc() { int n; double sum=0; for(int i=0;i<a;i++) { sum = sum + 1/Math.pow(2,i); } System.out.println("The sum is: "+sum); } } class B9 { public static void main(String ar[]) { int n; n=Integer.parseInt(ar[0]); A y=new A(n); System.out.println("Enter a value for n"); System.out.println("\n"); y.calc(); } }

Page 37 of 67

Output: C:\Java\jdk1.6.0_12\bin>java B9 5 Enter a value for n The sum is: 1.9375

Page 38 of 67

Practical No.:18 Create a class called Interest with variables p and r, they being the principal and rate of interest respectively and the class defines a method to find the simple interest for a year, this method to be overloaded to find the compound interest for an amount invested for n number of years. Class Action creates two instances (objects) of the above class one to find the simple interest and the other to find the compound interest containing main() and display the respective interests using your own data values. Code: class Interest { double si=0,ci=0; double p,r; void sint(int x,int y) { p=x; r=y; si=(p*r)/100; System.out.println("The simple interest for a year = "+si); } void sint(int x,int y,int z){ int i=1; p=x; r=y; while(i<=z){ ci=(p*r)/100; p=ci; i++; } System.out.println("The compound interest for "+z+" years = "+ci); } } class ActionInterest { public static void main(String args[]) { Interest si=new Interest(); Interest ci=new Interest(); System.out.println("Enter the number of years:"); int z=Integer.parseInt(args[0]); si.sint(2500,5); ci.sint(2500,5,z); } }

Page 39 of 67

Output: C:\Java\jdk1.6.0_12\bin>java ActionInterest 6 Enter the number of years: The simple interest for a year = 125.0 The compound interest for 6 years = 3.90625E-5

Page 40 of 67

Practical No.:19 Create a class called Area with variable l and b, they being the breadth and height of a quadrilateral and the class defines a method A to find the area of a square, this method to be overloaded to find the area of a rectangle. Class Action creates two instances (objects) of the above class one to find the area of a square and the other to find the area of a rectangle containing main() and display the respective area using your own data values. Code: class Area { int l,b; void area(int a) { l=a; int area1= l*l; System.out.println(area1); } void area(int c ,int d) { l=c; b=d; int area2= l*b; System.out.println(area2); System.out.print("\n"); } } class ActionArea { public static void main(String ar[]) { Area A1=new Area(); Area A2=new Area(); System.out.print("Area of square is: "); A1.area(10); System.out.print("Area of rectangle is: "); A2.area(20,30); } }

Page 41 of 67

Output: C:\Java\jdk1.6.0_12\bin>java ActionArea Area of square is: 100 Area of rectangle is: 600

Page 42 of 67

Practical No.:20
Write Java program to define following classes: A class having three overloading methods. The first method accepts no parameters, the second method accept one string and third method accept one string and an integer. These methods display the following messages (i) Rose is beautiful flower once using first method. (ii) Sunflower is beautiful flower twice using second method.(iii) Marigold is beautiful flower four times using first method.Write a class having main( ) method to invoke above methods. Code: class OverLoading { String st; int num; void method() { System.out.println(); System.out.println("Rose is beautiful flower "); } void method(String str) { st = str; System.out.println(st); } void method(String str , int n) { num = n; st = str; for (int i=0 ; i<n ; i++) { System.out.println(st); } } } class Flower { public static void main( String args[]) { String s1,s2; int n=4; s1="Sunflower is a beautiful flower"; s2="Marigold is a beautiful flower"; OverLoading ov = new OverLoading(); ov.method(); ov.method(s1); ov.method(s1); ov.method(s2,4); } }
Page 43 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Flower Rose is beautiful flower Sunflower is a beautiful flower Sunflower is a beautiful flower Marigold is a beautiful flower Marigold is a beautiful flower Marigold is a beautiful flower Marigold is a beautiful flower

Page 44 of 67

Practical No.:21 Write a Java program to write the following classes: Class Student contains instance variables roll number, class, name, marks obtained in two papers of a subject and average. This class contains following constructors: (i) Default constructor to initialize class with F.Y.Bsc., marks1=0.0, marks2=0.0 (ii) Define another constructor to accept roll number, name and class as parameters and fix the corresponding instance variables.(iii) Define third constructor to accept all parameters except average which should be calculated on the basis of marks1 and marks2. Class also contains a method called show() to display the values of all instance variables. Now, create a class containing main() method to create 3 different objects of Student class. Each object should use different constructor. Invoke show() for all three objects. Code: class Student { int rollno; double average; String cls; String name; double[] marks = new double[2]; Student () { rollno = 1; name = "Virender"; cls ="F.Y.Bsc"; marks[0] = 0; marks[1] = 0; average = 0.0; } Student(int a, String b, String c ) { rollno = a; name = b; cls = c; marks[0] = 15; marks[1] = 20; average = (marks[0]+marks[1])/2; } Student(int a, String b, String c ,int d, int e ) { rollno = a; name = b; cls = c; marks[0] = d; marks[1] = e;
Page 45 of 67

average = (marks[0]+marks[1])/2; } void show () { System.out.print("\n"); System.out.println("The student details are:"); System.out.println("Roll no: "+rollno+"\tName: "+name+"\tClass: "+cls); System.out.println("Marks -- Paper 1 : "+marks[0]+"\tPaper 2 : "+marks[1]); System.out.println("Average of Paper 1 & 2 : "+average); } } class StudentDemo { public static void main(String args[]) { Student ob1 = new Student(); ob1.show(); Student ob2 = new Student(2,"Sachin","F.Y.Bsc"); ob2.show(); Student ob3 = new Student(3,"Rahul","F.Y.Bsc",23,20); ob3.show(); } }

Page 46 of 67

Output: C:\Java\jdk1.6.0_12\bin>java StudentDemo The student details are: Roll no: 1 Name: Virender Class: F.Y.Bsc Marks -- Paper 1 : 0.0 Paper 2 : 0.0 Average of Paper 1 & 2 : 0.0 The student details are: Roll no: 2 Name: Sachin Class: F.Y.Bsc Marks -- Paper 1 : 15.0 Paper 2 : 20.0 Average of Paper 1 & 2 : 17.5 The student details are: Roll no: 3 Name: Rahul Class: F.Y.Bsc Marks -- Paper 1 : 23.0 Paper 2 : 20.0 Average of Paper 1 & 2 : 21.5

Page 47 of 67

Practical No.:22 Develop the following classes: A Cube class contains length as instance variable with constructor. A surface() method calculates the surface of cube. display() method displays the length of a cube. A Cuboid class contains height and breadth and inherits length from Cube class. volume() method calculate volume of a cuboid. The display() method inherits display() of class Cube and height and breadth of Cuboid class. A Cube_Cuboid class defines an object of Cuboid class and displays surface and area.

Code: class Cube { int length ; Cube (int a) { length = a; } void surface () { int surf = 6*length*length; System.out.print("\n"); System.out.println("The surface area of the Cube is :" +surf); } void display () { System.out.print("\n"); System.out.println("The length of the Cube is : " +length); } } class Cuboid extends Cube { int height, breadth; Cuboid (int a , int b, int c) { super(a); height = b; breadth = c; } void volume () { int vol = length*height*breadth; System.out.print("\n"); System.out.println("The volume of cuboid is : " +vol); }
Page 48 of 67

void display () { super.display(); System.out.print("\n"); System.out.println("The height and breadth of cuboid is : "+height+" & " +breadth); } } class Cube_cuboid { public static void main(String args[]) { Cuboid Cube_cuboid = new Cuboid(2,3,6); Cube_cuboid.display(); Cube_cuboid.surface(); Cube_cuboid.volume(); } }

Page 49 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Cube_cuboid The length of the Cube is : 2 The height and breadth of cuboid is : 3 & 6 The surface area of the Cube is :24 The volume of cuboid is : 36

Page 50 of 67

Practical No.:23 Create a class called Disp containing the instance variable n, to display the following messages I am good whenever n < 0 and the message I am great otherwise. This class is inherited by another class Display containing the instance variable n. this class contains a method that will instantiate the two instance variables and a method which display the message I love apples if n10 and display I love mangoes otherwise. Create a class containing the main() and display the messages according to the value you have given to the two variables. Code: class Disp { int n; Disp(int a) { n = a; } void disp () { if (n<0) System.out.println("I am good"); else System.out.println("I am great"); } } class Display extends Disp { int n; Display(int a, int b) { super(a); this.n = b; } void display () { disp(); if (n!=10) System.out.println("I love apples"); else System.out.println("I love mangoes"); } }
Page 51 of 67

class Main { public static void main(String args[]) { System.out.println("Enter the number"); int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); Display obj = new Display(a,b); obj.display(); } }

Page 52 of 67

Output: C:\Java\jdk1.6.0_12\bin>java Main 5 10 Enter the number I am great I love mangoes C:\Java\jdk1.6.0_12\bin>java Main -5 10 Enter the number I am good I love mangoes C:\Java\jdk1.6.0_12\bin>java Main 5 -10 Enter the number I am great I love apples C:\Java\jdk1.6.0_12\bin>java Main -5 -10 Enter the number I am good I love apples

Page 53 of 67

Practical No.:24
Interfaces are sometimes used to indicate which classes have a certain capability or characteristic. Consider the following classes and interfaces. The abstract class Animal has abstract subclasses named Bird and Reptile. Classes Dove, Eagle, Hawk, Penguin, and Seagull extend Bird. Classes RattleSnake and Turtle extend Reptile. The ColdBlooded interface defines no constants and declares no methods. It is implemented by Reptile. The OceanDwelling interface also defines no constants and declares no methods. It is implemented by the Penguin, Seagull, and Turtle classes. Define all of these classes and implement the interfaces as specified. Create one instance of each class. Then display all cold-blodded animals and all ocean-dwelling animals. Code: interface ColdBlodded { } interface OceanDwelling { } abstract class Animal { } abstract class Bird extends Animal { } abstract class Reptile extends Animal { } class Dove extends Bird { } class Eagle extends Bird { } class Hawk extends Bird { } class Penguin extends Bird implements OceanDwelling { void show() { System.out.println("Penguin is an OceanDwelling Bird"); } } class Seagull extends Bird implements OceanDwelling { void show() { System.out.println("Seagull is an OceanDwelling Bird"); } } class RattleSnake extends Reptile implements ColdBlodded { void show() { System.out.println("RattleSnake is an ColdBlodded Reptile");
Page 54 of 67

} } class Turtle extends Reptile implements OceanDwelling , ColdBlodded { void show() { System.out.println("Turtle is an OceanDwelling and ColdBlodded Reptile"); } } class Implementing { public static void main ( String args[] ) { Penguin P = new Penguin(); P.show(); System.out.println(); Seagull S = new Seagull(); S.show(); System.out.println(); Turtle T = new Turtle(); T.show(); System.out.println(); RattleSnake R = new RattleSnake(); R.show(); } }

Page 55 of 67

Output:

C:\Java\jdk1.6.0_12\bin>java Implementing Penguin is an OceanDwelling Bird Seagull is an OceanDwelling Bird Turtle is an OceanDwelling and ColdBlodded Reptile RattleSnake is an ColdBlodded Reptile

Page 56 of 67

Practical No.:25
Write Java applet to insert the name of a person using HTML tag and display it with applet viewer in front of the message What is your name? Code: import java.awt.*; import java.applet.*; import java.awt.event.*; public class Name extends Applet { String str1; public void init() { str1=getParameter("name"); str1="What is your name? "+str1; } public void paint(Graphics g) { g.drawString(str1,10,50); } } HTML Code: <Applet code=Name.class width=500 height=200> <PARAM NAME="name" value="ABC"> </Applet>

Page 57 of 67

Output: C:\Java\jdk1.6.0_12\bin>javac Name.java C:\Java\jdk1.6.0_12\bin>appletviewer Name.html

Page 58 of 67

Practical No.:26 Write an applet to display a face of a person. Code: import java.awt.*; import java.applet.*; public class Face extends Applet { public void paint(Graphics g) { g.drawOval(40,40,120,150); g.drawOval(57,75,30,20); g.drawOval(110,75,30,20); g.fillOval(68,81,10,10); g.fillOval(121,81,10,10); g.drawOval(85,100,30,30); g.drawArc(60,125,80,40,180,180); g.drawOval(25,92,15,30); g.drawOval(160,92,15,30); } } HTML Code: <Applet code=Face.class width=500 height=300> </Applet>

Page 59 of 67

Output: C:\Java\jdk1.6.0_12\bin>javac Face.java C:\Java\jdk1.6.0_12\bin>appletviewer Face.html

Page 60 of 67

Practical No.:27 Write Java applet to insert the users name and pass word of a person using HTML tag and display it with applet viewer in front of the messages User ID, Password should be displayed Java Source Code: import java.awt.*; import java.applet.*; import java.awt.event.*; public class UsePass extends Applet { String str1,str2; public void init() { str1=getParameter("user"); str1="User Name: "+str1; str2=getParameter("password"); str2="Password: "+str2; } public void paint(Graphics g) { g.drawString(str1,10,50); g.drawString(str2,10,100); } } HTML Code: <HTML> <BODY> <APPLET code=UsePass.class width=300 height=200> <PARAM NAME="user" value="123"> <PARAM NAME="password" value="qwe6789"> </APPLET> </BODY> </HTML>

Page 61 of 67

Output: C:\javaprog>javac UsePass.java C:\javaprog>appletviewer UsePass.html

Page 62 of 67

(i)

Practical No.:28 Write a program to accept the elements of the classes Emp_no (ii) basic_pay (iii) Dept. These elements are passed through the applet using text field. The salary slips along with calculation of D.A., H.R.A., C.C.A and Gross Salary should be printed on the applet. The D.A and C.C.A are calculated as D.A. = 81% of basic salary if basic salary < 5000 if basic salary is in the range 5000 to 7000 if basic salary > 7000

= 51% of basic salary = 41% of basic salary C.C.A = 350/H.R.A. = 15% of basic salary

Code: import java.awt.*; import java.applet.*; public class Salary extends Applet { TextField t1,t2,t3; public void init() { t1=new TextField(8); t2=new TextField(8); t3=new TextField(8); add(t1); add(t2); add(t3); t1.setText("0"); t2.setText("0"); t3.setText("0"); } public void paint(Graphics g) { int bp=0,cca; double d=0.0,hra=0.0,gs=0.0; String s1,s2,s3,c; g.drawString("Input Empno, Dept, Salary in the boxes given",10,50); try { s1=t1.getText(); s2=t2.getText(); s3=t3.getText();
Page 63 of 67

bp=Integer.parseInt(s3); } catch(Exception e){ } if(bp<=5000) { d=(0.81)*bp; } else { if(bp>5000 && bp<=7000) { d=(0.51)*bp; } else { d=(0.41)*bp; } } cca=350; hra=0.15*bp; gs=bp+d+hra+cca; c=String.valueOf(gs); g.drawString("The gross salary is : "+c,10,75); } } HTML Code: <Applet code=Salary.class width=800 height=500> </Applet>

Page 64 of 67

Output: C:\javaprog>javac Salary.java C:\javaprog>appletviewer Salary.html

Page 65 of 67

Practical No: 29 QuestionWrite an applet that changes to red, when mouse is pressed and changes to green when the mouse is released and changes to white when mouse is exited. Java Source Code: import java.awt.*; import java.applet.*; import java.awt.event.*; public class MouseTest extends Applet implements MouseListener { Color[] c = new Color[] {Color.red,Color.green,Color.white}; String msg=""; public void init() { addMouseListener(this); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { msg="Mouse Exited"; setBackground(c[2]); repaint(); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { msg="Mouse Pressed"; setBackground(c[0]); repaint(); } public void mouseReleased(MouseEvent e) { msg="Mouse Released"; setBackground(c[1]); repaint(); } public void paint(Graphics g) { g.drawString(msg,100,100); } } HTML Code: <HTML> <BODY> <APPLET code=MouseTest.class width=300 height=200> </APPLET> </BODY> </HTML>
Page 66 of 67

Output: C:\javaprog>javac MoouseTest.java C:\javaprog>appletviewer MouseTest.html

Page 67 of 67

You might also like