Sunday, 13 December 2015

Java Lab@home 5

1.Write a program to create a class that contains a method that accepts compass directions and displays the same. In addition, you need to ensure that the method must accept any one of the directions: NORTH, SOUTH, EAST, WEST, NORTHEAST, SOUTHEAST, SOUTHWEST, or NORTHWEST.

Ans.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);

        }

    };


2.Take an existing application and attempt to recognize the declaration and use of various types of nested classes.

Ans.enum CompassDirections
 {
    NORTH, SOUTH, EAST, WEST, NORTHEAST, SOUTHEAST, SOUTHWEST, NORTHWEST;
 }
     class TestCompassDirections
 {


  public void displayDirection(CompassDirections cd)
  {
   System.out.println(cd);
  }
    public static void main(String[] args)
     {
   TestCompassDirections tcd = new TestCompassDirections();
   tcd.displayDirection(CompassDirections.EAST);
  }
 }


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

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

4.• • Mark has been assigned a task of developing a game console. In this game console, he needs to ensure that the following functionalities are implemented: Each game in the game console must offer functionalities, such as play game, compute score, and display score. The display score functionality will remain the same for all the games. However, the play game and compute score functionalities will be different for each game. In the initial phase, you do not need to implement the logic of the functionalities. However, display appropriate messages, such as the play game functionality will display the message, Starting the game. Now, help Mark to achieve the preceding requirements.

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

5.Furniture and Fittings Company (FFC) manufactures several furniture items, such as chairs and bookshelves. The furniture items in the company have some common characteristics, such as price, width, and height. However, some furniture items have some specific details. For example, a bookshelf has a number of shelves. Now, write a Java program that uses the concept of abstract class to store and display the details of furniture items.

Ans.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();

  }


No comments:

Post a Comment