Program of Ootobject

Embed Size (px)

Citation preview

  • 7/27/2019 Program of Ootobject

    1/34

    INDEX

    S.No.

    PROGRAMS SIGNATURE

    1. WAP To Print Hello

    2. WAP To Print Numeric Series

    3. WAP To Print 3x3 Matrix

    4. WAP To Implement Switch Case

    5. WAP To Implement Scanners

    6. WAP To Implement Arguments7. WAP To Implement Constructor

    8. WAP To Implement ParameterConstructor

    9. WAP To Implement Overload

    10. WAP To Implement Call Value

    11. WAP To Implement Call Refer

    12. WAP To Implement Abstract

    13. WAP To Implement Inheritance

    14. WAP To Implement interface call back

    15. WAP To Implement interface extend

    16. WAP To Implement interface stack

    17. WAP To Implement access inheritance

    18. WAP To Implement stack interface19. WAP To Implement package

    20. WAP To Implement my thread

    21. WAP To Implement multi catch22. WAP To Implement throw exception

    23. WAP To Implement simple applet

    24. WAP To Implement frame

    25. WAP To Implement panel

    1

  • 7/27/2019 Program of Ootobject

    2/34

    PROGRAM:-1

    Write a program to print HELLO.

    class demo1

    {

    public static void main(String... s)

    {

    System.out.print("Hello java");

    }

    }

    Output:

    Hello java

    2

  • 7/27/2019 Program of Ootobject

    3/34

    PROGRAM-2

    Write a program to print:-

    1

    2 3

    4 5 6

    class series

    {

    public static void main(int Arr[])

    {

    int a=1;

    int b,c;

    for(b=1;b

  • 7/27/2019 Program of Ootobject

    4/34

    Output :

    1

    2 3

    4 5 6

    4

  • 7/27/2019 Program of Ootobject

    5/34

    PROGRAM-3

    Write a program to print a matrix of order 3*3.

    class ArrayDemo

    {

    public static void main(String[] args)

    {

    int array[][]= {{4,5,6},{6,8,9},{3,2,1}};

    System.out.println("Matrix : ");

    for(int i = 0;i

  • 7/27/2019 Program of Ootobject

    6/34

    PROGRAM-4

    Write a program to implement SWITCH CASE.

    public class SwitchDemo

    {

    public static void main(String[] args)

    {

    int month = 8;

    String monthString;

    switch (month)

    {

    case 1: monthString = "January";

    break;

    case 2: monthString = "February";

    break;

    case 3: monthString = "March";

    break;

    case 4: monthString = "April";

    break;

    case 5: monthString = "May";

    break;

    case 6: monthString = "June";

    break;

    case 7: monthString = "July";

    break;

    case 8: monthString = "August";

    6

  • 7/27/2019 Program of Ootobject

    7/34

    break;

    case 9: monthString = "September";

    break;

    case 10: monthString = "October";

    break;

    case 11: monthString = "November";

    break;

    case 12: monthString = "December";

    break;

    default: monthString = "Invalid month";

    break;

    }

    System.out.println(monthString=+monthstring);

    }

    }

    Output:

    August

    7

  • 7/27/2019 Program of Ootobject

    8/34

    PROGRAM-5

    Write a program to implement SCANNER.

    import java.util.*;

    class Scan

    {

    Public static void main(String args[])

    {

    System.out.println(enter the name and sirname:);

    Scnner sc = new Scanner(System.in);

    String a = sc.next line();

    System.out.println(a);

    }

    }

    Output :

    Enter the name and sirname : michael jackson

    8

  • 7/27/2019 Program of Ootobject

    9/34

    PROGRAM-6

    Write a program to implement ARGUMENT.

    Class Arg

    {

    Public static void main(String args[])

    {

    System.out.println(enterfollowing arguments);

    System.out.println(first argument : +args[0]);

    System.out.println(second argument :+args[1]);

    Char a=args[0].charAt[0];

    Char b=args[1].charAt[0];

    int l=args[0].length();

    int m=args[1].length();

    System.out.println(a);

    System.out.println(b);

    System.out.println(l);

    System.out.println(m);

    }

    }

    9

  • 7/27/2019 Program of Ootobject

    10/34

    Output :

    Java arg xyz abc

    Enter the following arguments

    First argument : xyz

    Second argument : abc

    X

    A

    3

    3

    10

  • 7/27/2019 Program of Ootobject

    11/34

    Program-7

    Write a program to implement CONSTRUCTOR.

    class Rectangle{

    int l, b;

    float p, q;

    public Rectangle(int x, int y){

    l = x;

    b = y;}

    public int first(){

    return(l * b);}

    public Rectangle(int x){

    l = x;

    b = x; }

    public int second(){

    return(l * b); }

    public Rectangle(float x){

    p = x;

    q = x; }

    public float third(){

    return(p * q); }

    public Rectangle(float x, float y){ p = x;q = y; }

    public float fourth(){

    return(p * q);

    }

    }

    11

  • 7/27/2019 Program of Ootobject

    12/34

    OUTPUT:-

    The area of a rectangle in first constructor is : 8

    The area of a rectangle in first constructor is : 25

    The area of a rectangle in first constructor is : 4.0

    The area of a rectangle in first constructor is : 6.0

    12

  • 7/27/2019 Program of Ootobject

    13/34

    PROGRAM-8

    Write a program to implement PARAMETERIZED CONSTRUCTOR.

    Class Box{

    double width;

    double height;

    double depth;

    Box(double w, double h, double d){

    System.out.println(constructing box);

    width = w;

    height =h;

    dephth=d;}

    double volume(){

    return width*height*depth;}}

    Class BoxDemo

    {

    double vol;

    public static void main(String args[])

    {

    Box mybox1=new Box(10,20,15);

    Vol =mybox1.volume();

    System.out.println(volume is=+vol);

    } }

    Output :

    Constructing Box

    Volume is =3000.0

    13

  • 7/27/2019 Program of Ootobject

    14/34

    PROGRAM-9

    Write a program to implement OVERLOADING.

    public class MethodOverloading

    {

    public void add(int a, int b)

    { System.out.println(a + b); }

    public void add(String x,String y)

    { System.out.println(x + y); }

    public void add( char c, char d)

    {

    System.out.println(c + d);

    }

    public static void main(String [] args)

    {

    Calculation c = new Calculation();

    c.add(7,8);

    c.add("he", "llo");

    c.add('n','k');

    }

    }

    OUTPUT:-

    a+b=15

    x+y=hello

    c+d=nk

    14

  • 7/27/2019 Program of Ootobject

    15/34

    PROGRAM-10

    Write a program to implement CALL BY VALUE.

    Class Test

    {

    Void meth(int I, int j)

    {

    i*=2;

    j/=2;

    }

    }

    Class call

    {

    Public static void main(String args[])

    {

    Test ob =new test();

    int a = 15,b=20;

    System.out.println(a and b before call : +a+ +b);

    Ob.meth(a,b);

    System.out.println(a abd b after call : +a+ +b);

    }

    }

    Output :

    a and b before call : 15 20

    a and b after call : 15 20

    15

  • 7/27/2019 Program of Ootobject

    16/34

    PROGRAM-11

    Write a program to implement CALL BY REFERENCE.

    import java.io.*;

    import java.awt.*;

    public class callByReference

    {

    public static void main(String[] args)

    {

    System.out.println("This is call by reference example");

    Rectangle rec = new Rectangle(20,30,200,50);

    System.out.println("Before move up massage");

    System.out.println("After move up massage display");

    moveUp(rec);

    System.out.println("r1 is now\n" + rec);

    }

    static void moveUp(Rectangle rect)

    {

    rect.translate(0,-20);

    }

    }

    16

  • 7/27/2019 Program of Ootobject

    17/34

    PROGRAM-12

    Write a program to implement ABSTRACT CLASS.

    abstract class Shape

    { abstract public double area();

    }

    public class AbstractClassExample extends Shape

    { static int l, b;

    public AbstractClassExample()

    {

    l = 5;

    b = 2;

    }

    public double area()

    {

    return l*b;

    }

    public void display()

    { System.out.println("Length: " + l + ",Breadth: " + b);

    System.out.println(area());

    }

    public static void main(String args[])

    {

    AbstractClassExample p = new AbstractClassExample();

    p.display();

    }}

    17

  • 7/27/2019 Program of Ootobject

    18/34

    PROGRAM-13

    Write a program to implement INHERITANCE.

    class A{

    int a;float b;

    void Show(){

    System.out.println("b in super class: " + b);

    }

    }

    class B extends A{

    int a;

    float b;B( int p, float q){

    a = p;

    super.b = q;

    }

    void Show(){

    super.Show();

    System.out.println("b in super class: " + super.b);

    System.out.println("a in sub class: " + a);

    }

    public static void main(String[] args){

    B subobj = new B(1, 5);

    subobj.Show();

    }

    }

    Output :

    b in super class: 5.0

    b in super class: 5.0

    a in sub class: 1

    18

  • 7/27/2019 Program of Ootobject

    19/34

    Program : 14

    WAP to implement interface call back.

    public class Main{

    public interface Visitor{

    int DoJob(int a, int b);}

    public static void main(String[] args){

    Visitor adder = new Visitor(){

    public int DoJob(int a, int b){

    return a + b;}

    };

    Visitor multiplier = new Visitor(){public int DoJob(int a, int b) {

    return a*b;}

    };

    System.out.println(adder.DoJob(10, 20));System.out.println(multiplier.DoJob(10, 20));

    }}

    19

  • 7/27/2019 Program of Ootobject

    20/34

    Program 15

    WAP to implement interface extends.

    interface Monster {

    void menace();

    }

    interface DangerousMonster extends Monster {

    void destroy();

    }

    interface Lethal {

    void kill();

    }

    class DragonZilla implements DangerousMonster {

    public void menace() {

    }

    public void destroy() {

    }

    }

    interface Vampire extends DangerousMonster, Lethal {

    void drinkBlood();

    }

    class VeryBadVampire implements Vampire {

    public void menace() {

    }

    public void destroy() {

    }

    20

  • 7/27/2019 Program of Ootobject

    21/34

    public void kill() {

    }

    public void drinkBlood() {

    }}

    public class HorrorShow {

    static void u(Monster b) {

    b.menace();

    }

    static void v(DangerousMonster d) {

    d.menace();

    d.destroy();

    }

    static void w(Lethal l) {

    l.kill();

    }

    public static void main(String[] args) {

    DangerousMonster barney = new DragonZilla();

    u(barney);

    v(barney);

    Vampire vlad = new VeryBadVampire();

    u(vlad);

    v(vlad);

    w(vlad);

    }

    }

    21

  • 7/27/2019 Program of Ootobject

    22/34

    Program : 16

    WAP to implement interface stack.

    interface stack{

    void push(int x);int pop();}

    class StcksizeFix implements stack

    {

    private int tos;

    private int stck[];

    StcksizeFix(int size)

    {

    stck=new int[size];tos=-1;

    }

    public void push(int item)

    {

    if(tos==stck.length-1)

    System.out.println(The stack has over flow);

    else

    stck[++tos]=item;

    }

    public int pop()

    {

    if(tos==-1)

    {

    System.out.println(There is nothing to PoP);

    return 0;

    }

    else

    return stck[tos--];

    }

    }

    class TestStack

    22

  • 7/27/2019 Program of Ootobject

    23/34

    {

    public static void main(String args[])

    {

    StcksizeFix Stack1= new StcksizeFix(5);

    StcksizeFix Stack2= new StcksizeFix(5);

    System.out.println(Start Push Objects in Stack);

    for(int i=0;i

  • 7/27/2019 Program of Ootobject

    24/34

    Program : 17

    WAP to implement access inheritance.

    interface Area{float compute(float x, float y);}

    class Rectangle implements Area{public float compute(float x, float y){return(x * y);}}

    class Triangle implements Area

    {public float compute(float x,float y){return(x * y/2);}}

    class InterfaceArea{public static void main(String args[]){

    Rectangle rect = new Rectangle();Triangle tri = new Triangle();Area area;area = rect;System.out.println("Area Of Rectangle = "+ area.compute(1,2));

    area = tri;System.out.println("Area Of Triangle = "+ area.compute(10,2));}}

    OUTPUTArea Of Rectangle = 2.0Area Of Triangle = 10.0

    24

  • 7/27/2019 Program of Ootobject

    25/34

    Program : 18

    WAP to implement stack interface.

    Class Fixed Stack implements Instack

    {

    Private int stck[];

    Private int tos;

    Fixed stack(int size)

    {

    Stck = new int [size];

    Tos=-1;

    }

    Public void push(int item)

    {If (tos = =stck.length-1)

    {

    System.out.println(stack is full);

    }

    else

    {

    Stck[++tos]=item;

    }Public int pop()

    {

    If( tos

  • 7/27/2019 Program of Ootobject

    26/34

    {

    public static void main(String args[])

    fixed stack MyStack1=new fixed stack(5);

    fixed stack MyStack2=new fixed stack(8);

    for(int i=0,i

  • 7/27/2019 Program of Ootobject

    27/34

    Program: 19

    WAP to implement package.

    package pack;

    public class First{static double amt,sim,comp,princ = 1000,rate = 0.09;static int n=2;

    public First(){System.out.println("\n...Welcome to First Class\n");}

    public static void DisplayData(){System.out.println("Principle Amount\t:"+princ+"\nRate OfInterest\t:"+rate*100+"%");System.out.println("Term\t\t\t:"+n);}public static void SimpleInterest(){System.out.println("\nDisplaying Simple Interest...");try{

    Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)}catch(InterruptedException e){e.printStackTrace();}sim = (princ*n*rate);System.out.println("\tInterest\t: "+sim);}

    public static void CompInterest()

    {System.out.println("\nDisplaying Compound Interest...");try{Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)}catch(InterruptedException e){

    27

  • 7/27/2019 Program of Ootobject

    28/34

    e.printStackTrace();}double ans = princ*(1+rate/n);comp = Math.pow(ans,(double)n*4.0);System.out.println("\tInterest\t: "+comp);

    }/* public static void main(String []args){First f= new First();f.DisplayData();f.SimpleInterest();f.CompInterest();}*/}

    /* The socond file imports the first file and calculates the interst*/

    /*Second.java*/

    import pack.First;

    class Second{public static void main(String[] args){System.out.println("Running Class Second..");First first = new First();first.DisplayData();first.SimpleInterest();first.CompInterest();}}

    OutputRunning Class Second..

    ...Welcome to First Class

    Principle Amount :1000.0Rate Of Interest :9.0%Term :2

    Displaying Simple Interest...Interest : 180.0Displaying Compound Interest...Interest : 1.4221006128366082E24

    28

  • 7/27/2019 Program of Ootobject

    29/34

    Program : 20

    WAP to implement My thread.

    public class MyThread extends Thread {public void run () {int count = 0;

    while (true) {

    System.out.println ("Thread 1 alive");

    // Print every 0.10sec for 2 secondstry {

    Thread.sleep (100);

    } catch (InterruptedException e) {}

    count++;if (count >= 20) break;

    }

    System.out.println ("Thread stopping");} // run

    } // class MyThread

    29

  • 7/27/2019 Program of Ootobject

    30/34

    Program : 21

    WAP to implement multi catch.

    public class Multi_Catch

    {public static void main (String args[]){

    int array[]={20,10,30};int num1=15,num2=0;int res=0;

    try{res = num1/num2;

    System.out.println("The result is" +res);

    for(int ct =2;ct >=0; ct--){System.out.println("The value of array are" +array[ct]);}

    }catch (ArrayIndexOutOfBoundsException e){System.out.println("Error?. Array is out of Bounds");

    }

    catch (ArithmeticException e){System.out.println ("Can't be divided by Zero");}

    }}

    output:

    Can't be divided by Zero

    30

  • 7/27/2019 Program of Ootobject

    31/34

    Program : 22

    WAP to implement throw exeption.

    class Throw_clause

    {

    public static void main (String args [ ])

    {

    try

    {

    throw new NullPointerException ( );

    } catch (NullPointerException e)

    {

    System.out.println ("Invalid reference use");

    }

    }

    }

    output :

    Invalid refrence use

    31

  • 7/27/2019 Program of Ootobject

    32/34

    Program : 23

    WAP to implement simple applet

    import java.awt.*;import java.applet.*;public class Checkerboard extends Applet {

    public void paint(Graphics g) {

    int row; // Row number, from 0 to 7int col; // Column number, from 0 to 7int x,y; // Top-left corner of square

    for ( row = 0; row < 8; row++ ) {

    for ( col = 0; col < 8; col++) {

    x = col * 20;y = row * 20;if ( (row % 2) == (col % 2) )

    g.setColor(Color.red);else

    g.setColor(Color.black);g.fillRect(x, y, 20, 20);

    }}

    }}

    Output :

    32

  • 7/27/2019 Program of Ootobject

    33/34

    Program:24

    WAP to implement frame.

    import java.awt.*;

    class MyFrame

    {

    Frame f;

    MyFrame(String s)

    {

    Frame f=new Frame(s);

    f.setSize(500,500);

    f.setVisible(true);

    }

    public static void main(String args[])

    {

    new MyFrame("ff");

    }

    }

    Output :

    33

  • 7/27/2019 Program of Ootobject

    34/34

    Program : 25

    WAP to implement panel.

    Import java.swing.*;

    Public class panel

    {

    Public static void main(String args[])

    {

    Jpanel p = new jpanel(panel);

    JTextField t = new jField(panel field);

    Jlevel l = new Jlevel(panel level);

    Jframe f = new Jframe(Swing frame);

    p.add(l);

    p.add(t);

    f.get content panel().add(p);

    f.set visible(true);

    f.set size(300,300);

    }

    }