Download docx - Java Practicals

Transcript
Page 1: Java Practicals

Practical No.: 1Write 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(" "); }}

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

Sorted List: 99 78 26 12 5

Page 2: Java Practicals

Practical No.:2Write 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); }}

Output:C:\Java\jdk1.6.0_12\bin>java Mean 11 12 33 54 75Given List: 11 12 33 54 75

Mean of given numbers is: 37.0Standard deviation of given numbers is: 24.698178070456937

Page 3: Java Practicals

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(" "); } }}

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

Page 4: Java Practicals

Practical No.: 4To 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]+" "); } System.out.println(" ");

Page 5: Java Practicals

} 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(" "); } }}

Output:C:\Java\jdk1.6.0_12\bin>java MatSum 1 2 3 4 5 6 7 8 9 10 11 12First Matrix1 2 34 5 6Second Matrix7 8 910 11 12Resultant Matrix8 10 1214 16 18

Page 6: Java Practicals

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 7: Java Practicals

} } } 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(" "); } }}

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

Page 8: Java Practicals

Practical No.: 6To 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 9: Java Practicals

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

Page 10: Java Practicals

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(); }}

Output:C:\Java\jdk1.6.0_12\bin>java B 4587Enter a numberDivisors of 4587 are:13113313941715294587

Page 11: Java Practicals

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 12: Java Practicals

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

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

Page 13: Java Practicals

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 14: Java Practicals

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

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

Page 15: Java Practicals

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(); }}

OutputC:\Java\jdk1.6.0_12\bin>java B 2011Enter a year2011 is not a leap year

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

Page 16: Java Practicals

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(); }}

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

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

Page 17: Java Practicals

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); }}

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

Page 18: Java Practicals

Practical No.:13Create 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(); }}

Output:C:\Java\jdk1.6.0_12\bin>java B5 24Enter a numberERROR

C:\Java\jdk1.6.0_12\bin>java B5 8Enter a number8 is divisible by 4

C:\Java\jdk1.6.0_12\bin>java B5 18Enter a number18 is divisible by 6

Page 19: Java Practicals

Practical No.:14Create 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 are:"); 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); }}Output:C:\Java\jdk1.6.0_12\bin>java B6 30Enter a numberNumbers from 1 to 30 divisible by 5 are:51015202530

Page 20: Java Practicals

Practical No.:15Create 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); }}

Output:C:\Java\jdk1.6.0_12\bin>java B7 30Enter a numberNumbers from 1 to 30 divisible by 6 or 8 are:681216182430

Page 21: Java Practicals

Practical No.:16Create 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); }}

Output:C:\Java\jdk1.6.0_12\bin>java B8 30Enter a numberNumbers from 1 to 30 divisible by 6 or 8 are:6812161830

Page 22: Java Practicals

Practical No.:17Create 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(); }}

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

The sum is: 1.9375

Page 23: Java Practicals

Practical No.:18Create 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); }}

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

Page 24: Java Practicals

Practical No.:19Create 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); }}

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

Page 25: Java Practicals

Practical No.:21Write 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 26: Java Practicals

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(); }}

Output:C:\Java\jdk1.6.0_12\bin>java StudentDemo

The student details are:Roll no: 1 Name: Virender Class: F.Y.BscMarks -- Paper 1 : 0.0 Paper 2 : 0.0Average of Paper 1 & 2 : 0.0

The student details are:Roll no: 2 Name: Sachin Class: F.Y.BscMarks -- Paper 1 : 15.0 Paper 2 : 20.0Average of Paper 1 & 2 : 17.5

The student details are:Roll no: 3 Name: Rahul Class: F.Y.BscMarks -- Paper 1 : 23.0 Paper 2 : 20.0Average of Paper 1 & 2 : 21.5

Page 27: Java Practicals

Practical No.:22Develop 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 28: Java Practicals

} 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(); }}

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 29: Java Practicals

Practical No.:23Create 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 n≠10 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 30: Java Practicals

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(); }}

Output:C:\Java\jdk1.6.0_12\bin>java Main 5 10Enter the numberI am greatI love mangoes

C:\Java\jdk1.6.0_12\bin>java Main -5 10Enter the numberI am goodI love mangoes

C:\Java\jdk1.6.0_12\bin>java Main 5 -10Enter the numberI am greatI love apples

C:\Java\jdk1.6.0_12\bin>java Main -5 -10Enter the numberI am goodI love apples

Page 31: Java Practicals

Practical No.:25Write 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="ANEESH NAIR"></Applet>

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

Page 32: Java Practicals

Practical No.:26Write 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>

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

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

Page 33: Java Practicals

Practical No.:27Write 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

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:<Applet code=UsePass.class width=500 height=300><PARAM NAME="user" value="Aneesh"> <PARAM NAME="password" value="arc140194"> </Applet>

Output:C:\Java\jdk1.6.0_12\bin>javac UsePass.javaC:\Java\jdk1.6.0_12\bin>appletviewer UsePass.html

Page 34: Java Practicals

Practical No.:28Write a program to accept the elements of the classes

1. 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

= 51% of basic salary if basic salary is in the range 5000 to 7000

= 41% of basic salary if basic salary > 7000

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 35: Java Practicals

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>

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