Java Final Printout

download Java Final Printout

of 67

Transcript of Java Final Printout

  • 8/3/2019 Java Final Printout

    1/67

    Page 1 of 67

    Prac. No.

    Date Topic

    1 Write a JAVA program to sort numbers given by an array.2 Write a JAVA program to find mean and Std. Deviation of an

    array of numbers.3 Write a JAVA program to display 3x3 matrix.

    4 Write a JAVA program to find sum of two matrix.5 Write a JAVA program to find product of two matrix.6 Write a JAVA program to find transpose of a matrix.7 Write a JAVA program to find all divisors of a number.8 Write a JAVA program to check whether a number is a perfect

    number or not.9 Write a JAVA program to check whether a quadratic equation

    has real or complex roots.10 Write a JAVA program to check whether a year is a leap year or

    not11 Write a JAVA program to check whether a number is divisible

    by 5 or not.12 Write a JAVA program to find a factorial of a number.13 Write a JAVA program to find a number which is divisible by 4

    or 6 but not both.14 Write a JAVA program to display all nos. between 1 and a

    given number which is divisible by 5.15 Write a JAVA program to display all nos. between 1 and a

    given number which is divisible by 6 or 8.16 Write a JAVA program to display all nos. between 1 and a

    given number which is divisible by 6 or 8 but not both.17 Write a JAVA program to find the sum of the sequence

    1+1/2+1/4+1/8+...........+1/2 n 18 Write a JAVA program to find simple and compound interest

    using method overloading.

    19 Write a JAVA program to find area of a square and a rectangleusing method overloading.20 Write a JAVA program to display appropriate messages using

    method overloading.21 Write a JAVA program to display report card of some students

    using method overloading.22 Write a JAVA program to calculate the volume of a cube and a

    cuboid using inheritance.23 Write a JAVA program to display appropriate messages using

    inheritance.24 Write a JAVA program to illustrate the concept of interface.25 Write an applet program to display the message What is your

    name? in an applet window.26 Write an applet program to display the face of a person.27 Write an applet program to insert username and password of a

    person and to display both.

    28Write an applet program accept some elements and to the

    display the necessary output in the applet window.29 Write an applet program that tracks the position of the

    mouse

  • 8/3/2019 Java Final Printout

    2/67

    Page 2 of 67

    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

  • 8/3/2019 Java Final Printout

    3/67

    Page 3 of 67

    O utput: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

  • 8/3/2019 Java Final Printout

    4/67

    Page 4 of 67

    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

  • 8/3/2019 Java Final Printout

    5/67

    Page 5 of 67

    O utput: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.0

    Standard deviation of given numbers is: 24.698178070456937

  • 8/3/2019 Java Final Printout

    6/67

    Page 6 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

  • 8/3/2019 Java Final Printout

    7/67

    Page 7 of 67

    O utput: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

  • 8/3/2019 Java Final Printout

    8/67

    Page 8 of 67

    Practical No.: 4

    T o initialize 2 two dimensional array of order 2X3 and to find their sum. T he program shoulddisplay 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

  • 8/3/2019 Java Final Printout

    9/67

    Page 9 of 67

    System.out.println(" ");}System.out.println("Second Matrix");for(i=0;i

  • 8/3/2019 Java Final Printout

    10/67

    Page 10 of 67

    O utput: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 6

    Second Matrix7 8 910 11 12Resultant Matrix8 10 1214 16 18

  • 8/3/2019 Java Final Printout

    11/67

    Page 11 of 67

    Practical No.: 5

    T o initialize 2 two dimensional array of order 2X2 and to find their product. T he programshould 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

  • 8/3/2019 Java Final Printout

    12/67

    Page 1 2 of 67

    }}}System.out.println("First matrix is:");for(i=0;i

  • 8/3/2019 Java Final Printout

    13/67

    Page 1 3 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java Multiply 2 12 22 54 14 18 87 69First matrix is:2 1222 54

    Second matrix is:14 1887 69Multiplied matrix is:1072 8645006 4122

  • 8/3/2019 Java Final Printout

    14/67

    Page 1 4 of 67

    Practical No.: 6

    T o initialize a two dimensional array of order 4X3 and to find its transpose. T he programshould display both the arrays.

    Code:class T ranspose{

    public static void main(String args[]){int i,j,k;int a[][]=new int[4][3];k=0;for(i=0;i

  • 8/3/2019 Java Final Printout

    15/67

    Page 1 5 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java T ranspose 11 21 33 54 45 76 79 48 19 10 25 78T he given matrix is:11 21 3354 45 76

    79 48 1910 25 78T ranspose of given matrix is:11 54 79 1021 45 48 2533 76 19 78

  • 8/3/2019 Java Final Printout

    16/67

    Page 16 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

  • 8/3/2019 Java Final Printout

    17/67

    Page 17 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java B 4587Enter a number Divisors of 4587 are:13

    113313941715294587

  • 8/3/2019 Java Final Printout

    18/67

    Page 1 8 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

  • 8/3/2019 Java Final Printout

    19/67

    Page 1 9 of 67

    }

  • 8/3/2019 Java Final Printout

    20/67

    Page 2 0 of 67

    O utput: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

  • 8/3/2019 Java Final Printout

    21/67

    Page 2 1 of 67

    Practical No.: 9

    Create a class called Qu ad containing a, b and c as variables, denoting the coefficient of aquadratic equation. T he class defines a constructor to initialize these variables and a methodthat finds whether the quadratic equation for an instance of the class has real roots or complexroots. Create a class Qu adratic 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

  • 8/3/2019 Java Final Printout

    22/67

    Page 22 of 67

    O utput: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

  • 8/3/2019 Java Final Printout

    23/67

    Page 23 of 67

    Practical No.:10

    Create a class called A containing the instance variable y which denotes a year and defines aconstructor to initialize the instance variable and a method that will check whether it is a leapyear or not. Class B contains the main() and calls the above method and displays whether y isa 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");elseSystem.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();}}

  • 8/3/2019 Java Final Printout

    24/67

    Page 24 of 67

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

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

  • 8/3/2019 Java Final Printout

    25/67

    Page 25 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 isdivisible by 5. Class B contains the main() and calls the method for a value of your choiceand 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");elseSystem.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();}}

  • 8/3/2019 Java Final Printout

    26/67

    Page 2 6 of 67

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

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

  • 8/3/2019 Java Final Printout

    27/67

    Page 2 7 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 anumber. Class B contains the main() and calls the method for a value of your choice anddisplays 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

  • 8/3/2019 Java Final Printout

    28/67

    Page 28 of 67

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

  • 8/3/2019 Java Final Printout

    29/67

    Page 29 of 67

    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 isdivisible by 4 or 6 but not both. Class B contains the main() and calls the method for a valueof 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("ERR O R"); }}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(); }}

  • 8/3/2019 Java Final Printout

    30/67

    Page 3 0 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java B5 24Enter a number ERR O R

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

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

  • 8/3/2019 Java Final Printout

    31/67

    Page 3 1 of 67

    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 avalue 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

  • 8/3/2019 Java Final Printout

    32/67

    Page 32 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java B6 30Enter a number

    Numbers from 1 to 30 divisible by 5 are:5

    1015202530

  • 8/3/2019 Java Final Printout

    33/67

    Page 33 of 67

    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 methodfor 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

  • 8/3/2019 Java Final Printout

    34/67

    Page 34 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java B7 30Enter a number

    Numbers from 1 to 30 divisible by 6 or 8 are:6

    81216182430

  • 8/3/2019 Java Final Printout

    35/67

    Page 35 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 callsthe 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

  • 8/3/2019 Java Final Printout

    36/67

    Page 3 6 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java B8 30Enter a number

    Numbers from 1 to 30 divisible by 6 or 8 are:6

    812161830

  • 8/3/2019 Java Final Printout

    37/67

    Page 3 7 of 67

    Practical No.:17 Create a class called A containing instance variable n and defines a constructor to initializethe instance variable and a method in this class that will find the following sum 1 + 1 / 2 + 1 /4 + 1 / 8 + . . . . 1 / 2 n. Class B contains the main() and calls the method and displays thesum.

    Code:class A{int a;A (int n){a=n;}void calc(){

    int n;double sum=0;for(int i=0;i

  • 8/3/2019 Java Final Printout

    38/67

    Page 38 of 67

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

    T he sum is: 1.9375

  • 8/3/2019 Java Final Printout

    39/67

    Page 39 of 67

    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, thismethod 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 thesimple interest and the other to find the compound interest containing main() and display therespective 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(" T he simple interest for a year = "+si); }void sint(int x,int y,int z){int i=1;

    p=x;r=y;while(i

  • 8/3/2019 Java Final Printout

    40/67

    Page 4 0 of 67

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

  • 8/3/2019 Java Final Printout

    41/67

    Page 4 1 of 67

    Practical No.:19Create a class called Area with variable l and b, they being the breadth and height of aquadrilateral and the class defines a method A to find the area of a square, this method to beoverloaded to find the area of a rectangle. Class Action creates two instances (objects) of theabove class one to find the area of a square and the other to find the area of a rectanglecontaining 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);}}

  • 8/3/2019 Java Final Printout

    42/67

    Page 42 of 67

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

  • 8/3/2019 Java Final Printout

    43/67

    Page 43 of 67

    Practical No.:20Write Java program to define following classes: A class having three overloading methods.T he first method accepts no parameters, the second method accept one string and thirdmethod accept one string and an integer. T hese methods display the following messages (i)Rose is beautiful flower once using first method. (ii) Sunflower is beautiful flower twiceusing second method.(iii) Marigold is beautiful flower four times using first method.Write aclass 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

  • 8/3/2019 Java Final Printout

    44/67

    Page 44 of 67

    O utput:C:\J ava \j dk1.6.0_12 \ bin> j ava Flower

    Rose is beautiful flowerSunflower is a beautiful flower

    Sunflower is a beautiful flowerMarigold is a beautiful flowerMarigold is a beautiful flowerMarigold is a beautiful flowerMarigold is a beautiful flower

  • 8/3/2019 Java Final Printout

    45/67

    Page 45 of 67

    Practical No.:21Write a Java program to write the following classes: Class Student contains instance variablesroll number, class, name, marks obtained in two papers of a subject and average. T his classcontains 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 andclass 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 marks1and marks2. Class also contains a method called show() to display the values of all instancevariables. 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 threeobjects.

    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;

  • 8/3/2019 Java Final Printout

    46/67

    Page 4 6 of 67

    average = (marks[0]+marks[1])/2;}void show (){System.out.print("\n");

    System.out.println(" T he 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();}}

  • 8/3/2019 Java Final Printout

    47/67

    Page 4 7 of 67

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

    T he student details are:Roll no: 1 Name: Virender Class: F.Y.Bsc

    Marks -- Paper 1 : 0.0 Paper 2 : 0.0Average of Paper 1 & 2 : 0.0

    T he 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

    T he 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

  • 8/3/2019 Java Final Printout

    48/67

    Page 48 of 67

    Practical No.:22Develop the following classes: A Cube class contains length as instance variable withconstructor. A surface() method calculates the surface of cube. display() method displays thelength of a cube. A Cuboid class contains height and breadth and inherits length from Cubeclass. volume() method calculate volume of a cuboid. T he display() method inherits display()of class Cube and height and breadth of Cuboid class. A Cube_Cuboid class defines an objectof 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(" T he surface area of the Cube is :" +surf);}void display (){System.out.print("\n");System.out.println(" T he 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(" T he volume of cuboid is : " +vol);}

  • 8/3/2019 Java Final Printout

    49/67

    Page 49 of 67

    void display (){super.display();System.out.print("\n");System.out.println(" T he 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();}}

  • 8/3/2019 Java Final Printout

    50/67

    Page 5 0 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java Cube_cuboid

    T he length of the Cube is : 2

    T he height and breadth of cuboid is : 3 & 6

    T he surface area of the Cube is :24

    T he volume of cuboid is : 36

  • 8/3/2019 Java Final Printout

    51/67

    Page 5 1 of 67

    Practical No.:23Create a class called Disp containing the instance variable n, to display the followingmessages I am good whenever n < 0 and the message I am great otherwise. T his class isinherited by another class Display containing the instance variable n. this class contains amethod that will instantiate the two instance variables and a method which display themessage I love apples if n 10 and display I love mangoes otherwise. Create a classcontaining the main() and display the messages according to the value you have given to thetwo variables.

    Code:class Disp{int n;Disp(int a){n = a;

    }void disp (){if (n

  • 8/3/2019 Java Final Printout

    52/67

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

  • 8/3/2019 Java Final Printout

    53/67

    Page 53 of 67

    O utput:C:\Java\jdk1.6.0_12\bin>java Main 5 10Enter the number I am greatI love mangoes

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

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

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

  • 8/3/2019 Java Final Printout

    54/67

  • 8/3/2019 Java Final Printout

    55/67

    Page 55 of 67

    }}class Turtle extends Reptile implements OceanDwelling ,ColdBlodded{

    void show()

    { System.out.println("Turtle is an OceanDwelling andColdBlodded 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();

    }}

  • 8/3/2019 Java Final Printout

    56/67

    Page 5 6 of 67

    O utput:C:\J ava \j dk1.6.0_12 \ bin> j ava ImplementingPenguin is an OceanDwelling Bird

    Seagull is an OceanDwelling Bird

    Turtle is an OceanDwelling and ColdBlodded Reptile

    RattleSnake is an ColdBlodded Reptile

  • 8/3/2019 Java Final Printout

    57/67

    Page 5 7 of 67

    Practical No.:25Write Java applet to insert the name of a person using H T ML tag and display it with appletviewer in front of the message What is your name?

    Code:

    import j ava.awt.*;import j ava.applet.*;import j ava.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);}

    }

    HT ML Code:

  • 8/3/2019 Java Final Printout

    58/67

    Page 58 of 67

    O utput:C:\J ava \j dk1.6.0_12 \ bin> j avac Name. j avaC:\J ava \j dk1.6.0_12 \ bin>appletviewer Name.html

  • 8/3/2019 Java Final Printout

    59/67

    Page 59 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.draw O val(40,40,120,150);g.draw O val(57,75,30,20);g.draw O val(110,75,30,20);g.fill O val(68,81,10,10);g.fill O val(121,81,10,10);g.draw O val(85,100,30,30);g.drawArc(60,125,80,40,180,180);g.draw O val(25,92,15,30);g.draw O val(160,92,15,30);}}

    HT ML Code:

  • 8/3/2019 Java Final Printout

    60/67

    Page 60 of 67

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

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

  • 8/3/2019 Java Final Printout

    61/67

    Page 61 of 67

    Practical No.:27 Write Java applet to insert the users name and pass word of a person using H T ML tag anddisplay it with applet viewer in front of the messages User ID, Password should bedisplayed

    Java Source Code:import j ava.awt.*;import j ava.applet.*;import j ava.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);

    }}

    HT ML Code:

  • 8/3/2019 Java Final Printout

    62/67

    Page 6 2 of 67

    O utput:C:\j avaprog> j avac UsePass. j ava

    C:\j avaprog>appletviewer UsePass.html

  • 8/3/2019 Java Final Printout

    63/67

    Page 6 3 of 67

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

    (i) Emp_no (ii) basic_pay (iii) Dept.T hese elements are passed through the applet using text field. T he salary slips along withcalculation of D.A., H.R.A., C.C.A and Gross Salary should be printed on the applet. T heD.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{T extField t1,t2,t3;

    public void init(){t1=new T extField(8);t2=new T extField(8);

    t3=new T extField(8);add(t1);add(t2);add(t3);t1.set T ext("0");t2.set T ext("0");t3.set T ext("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.get T ext();s2=t2.get T ext();s3=t3.get T ext();

  • 8/3/2019 Java Final Printout

    64/67

    Page 6 4 of 67

    bp=Integer.parseInt(s3);}catch(Exception e){ }if(bp5000 && bp

  • 8/3/2019 Java Final Printout

    65/67

    Page 6 5 of 67

    O utput:C:\javaprog>javac Salary.javaC:\javaprog>appletviewer Salary.html

  • 8/3/2019 Java Final Printout

    66/67

    Page 66 of 67

    Practical No: 29

    QuestionWrite an applet that changes to red, when mouse is pressed and changes to greenwhen the mouse is released and changes to white when mouse is exited.

    Java Source Code:import j ava.awt.*;import j ava.applet.*;import j ava.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 mouse Clicked(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);

    }}

    HT ML Code:

  • 8/3/2019 Java Final Printout

    67/67

    O utput:C:\j avaprog> j avac MoouseTest. j ava

    C:\j avaprog>appletviewer MouseTest.html