Tuesday 27 September 2016

CORE JAVA LAB@HOME 5 ~ GNIITHELP

Question-1 Calculate Area of triangle.

Answer:-

abstract class GameConsole  {


   int score;

    void displayScore ()   {

   System.out.println("The displayScore method.");

  }

  

  abstract void computeScore();

  abstract void playGame();

 }


 class Badminton extends GameConsole   {


  void playGame()   {

   System.out.println("Starting the Badmintion Game...");

  }


  void computeScore()  {

   System.out.println("Calculating Score for the Badmintion Game...");

  }

 }

  class TableTennis extends GameConsole  {


  void playGame()   {

   System.out.println("Starting the TableTennis Game...);

  }

  void computeScore()  {

   System.out.println("Calculating Score for the TableTennis Game...");

  }

 }


 public class GameDemo   {

  

  public static void main(String args[])   {

   Badminton obj1 = new Badminton ();

   obj1.playGame();

   obj1.computeScore();

   obj1.displayScore();


   TableTennis obj2 = new TableTennis();

   obj2.playGame();

   obj2.computeScore();

   obj2.displayScore();

  }

 }


Question-2 Compass Direction.

Answer:-

public class OuterClass {


public int x = 42;


  

public void method1() {



        class LocalClass {



            public void localPrint() {


                System.out.println("In local class");


                System.out.println(x);


            }


       }


        LocalClass lc = new LocalClass();


        lc.localPrint();

    }



    public void method2() {


        Runnable r = new Runnable() {



            @Override

            public void run() {



                System.out.println("In an anonymous local class method");


                System.out.println(x);


            }


        };


        r.run();

    }

    public Runnable r = new Runnable() {



        @Override


        public void run() {


            System.out.println("In an anonymous class method");


            System.out.println(x);


        }


    };


Question-3 Query on furniture Items.

Answer:-

public adstract class Furniture {


  protected String color;

  protected int width;

  protected int height;

  public adstract void accept();

  public adstract void display();

 }

     class chair extends Furniture {

  private int numOf_legs;


  public void accept() {


  colour = "Brown"

  width = 36;

  height = 48;

  numOf_legs = 4;

 }

    public void display()    {

  System.out.println("DISPLAYING VALUE FOR CHAIR");

  system.out.println

 ("==================================");

  System.out.println("Color is" + color);

  System.out.println("Width is" + width);

  System.out.println("Height is" + height);

  System.out.println("Number of legs is" + numOf_legs);

  System.out.println(" ");

  }

 }


 class Bookshelf extends Furniture {


  private int numOf_shelves;

  

  public void accept()  {

  

   colour ="Black";

   width = 72;

   height = 84;

   numOf_shelves = 4;

  }

  public void display ()  {

   System.out.println("DISPLAYING VALUES FOR BOOKSHELF")

   System.out.println

  ("===================================");


  System.out.println("Color is" + color);

  System.out.println("Width is" + width);

  System.out.println("Height is" + height);

  System.out.println("Number of shelves is" + numOf_shelves);

  System.out.println(" ");

  }

 }


 class FurnitureDemo  {

  public static void main(String[] args)   {

   bookShelf b1 = new BookShelf();

   b1.accept();

   b1.display();

   


   Chair c1 = new Chair ();

   c1.accept();

   c1.display();


  }

Question-4 Query on Game Console.

Answer:-

abstract class Area  {

  double dim1, dim2, result;


  abstract void calArea();


  double disArea()    {


   return result;

  }

 }

     class Circle extends Area   {

  double pie = 3.14;


  public Circle(double rad)   {

   dim1 = rad;


  }


  void calArea()  {

   result= pie*dim1*dim1;

  }

 }


    class Rectangle extends Area   {

  public Rectangle(double ln, double br)   {

   dim1 = ln;

   dim2 = br;

  }


  void calArea()   {

   result = dim1 * dim2;

  }

 }


   claSS Square extand Area   {

  public Square(double side)   {

   dim1 = side;

  }

  

  void calArea()   {

   result =dim1* dim1;

  }

 }


   class Triangle extends Area  {

   public Triangle (double bas, double high)   {

    dim1 = bas;

    dim2 = high;

   }


   void calArea() {

    result = (dim1 * dim2 ) /2;


   }

 }

    public class Area calculator   {

  public static void main (String[] args)  {

   Square sobj = new Square (44.5);

   sobj.calArea();

   System.out.println("The area of square is " + sobj.disArea());

   

   Rectangle robj = new Rectangle (23.4, 12);

   robj.calArea();

   System.out.println("The area of rectangle is " + robj.disArea());



   Circle cobj = new Circle (5.7);

   cobj.calArea();

   System.out.println("The area of circle is " + cobj.disArea());



   Triangle tobj = new Triangle (20, 20);

   tobj.calArea();

   System.out.println("The area of triangle is " + tobj.disArea());

  }


Question-5 Create a Nested Class.

.Write a program to calculate the area of triangle, circle, square, and rectangle. However, you need to ensure that the preceding set of functionalities must be achieved by implementing abstraction.

Answer:-

Ans.abstract class GameConsole  {


   int score;

    void displayScore ()   {

   System.out.println("The displayScore method.");

  }

  

  abstract void computeScore();

  abstract void playGame();

 }


 class Badminton extends GameConsole   {


  void playGame()   {

   System.out.println("Starting the Badmintion Game...");

  }


  void computeScore()  {

   System.out.println("Calculating Score for the Badmintion Game...");

  }

 }

  class TableTennis extends GameConsole  {


  void playGame()   {

   System.out.println("Starting the TableTennis Game...);

  }

  void computeScore()  {

   System.out.println("Calculating Score for the TableTennis Game...");

  }

 }


 public class GameDemo   {

  

  public static void main(String args[])   {

   Badminton obj1 = new Badminton ();

   obj1.playGame();

   obj1.computeScore();

   obj1.displayScore();


   TableTennis obj2 = new TableTennis();

   obj2.playGame();

   obj2.computeScore();

   obj2.displayScore();

  }

 }

No comments:

Post a Comment