Saturday 3 June 2017

JAVA Tutorial-5

1. The abstract Vegetable class has three subclasses named Potato, Brinjal and Tomato.
Write an application that demonstrates how to establish this class hierarchy. Declare one instance variable of type String that indicates the color of a vegetable.
Create and display instances of these objects. Override the toString() method of Object to return a string with the name of the vegetable and its color

 
public abstract class Vegetable
{
 String color;
 public abstract String toString();
 public static void main(String a[])
 {
  Vegetable v=new Potato();
  System.out.println(v.toString());
  Vegetable v1=new Brinjal();
  System.out.println(v1.toString());
  Vegetable v2=new Tomato();
  System.out.println(v2);
 }
}
class Potato extends Vegetable
{
 Potato()
 {
  color="white Potato";
 }
 public String toString()
 {
  return("Color of Potato: "+this.color);
 }
}
class Brinjal extends Vegetable
{
 Brinjal()
 {
  color="dark purple";
 }
 public String toString()
 {
  return("Color of Brinjal: "+this.color);
 }
}
class Tomato extends Vegetable
{
 Tomato()
 {
  color="Red";
 }
 public String toString()
 {
  return("Color of Tomato: "+this.color);
 }
}

Output

2. The Transport interface declares a deliver() method.
The abstract class Animal is the superclass of the Tiger, Camel, Deer and Donkey classes.
The Transport interface is implemented by the Camel and Donkey classes.
Write a test program that initialize an array of four Animal objects.
If the object implements the Transport interface, the deliver() method is invoked

 
interface Transport
{
 public void deliver();
}
abstract class Animal
{
 public void show()
 {
  
 }
}
class Tiger extends Animal
{
 public void show()
 {
  System.out.println("I am in show method of Animal class overrided in Tiger class ");
 }
}
class Camel extends Animal implements Transport
{
 public void deliver()
 {
   System.out.println("I am in deliver method of Transport Interface overrided in Camel class ");
 }
}
class Deer extends Animal
{
 public void show()
 {
   System.out.println("I am in show method of Animal class overrided in Deer class ");
 }
 
}
class Donkey extends Animal implements Transport
{
 public void deliver()
 {
   System.out.println("I am in deliver method of Transport Interface overrided in Donkey class ");
 }
 
}
class AnimalDemo
{
   public static void main(String args[])
   {
 Animal A[]=new Animal[4];
 A[0]=new Tiger();
 A[1]=new Camel();
 A[2]=new Deer();
 A[3]=new Donkey();
 if(A[0] instanceof Transport)
 {
  ((Transport)A[0]).deliver();
 }
 else
 {
  A[0].show();
 }
 if(A[1] instanceof Transport)
 {
  ((Transport)A[1]).deliver();
 }
 else
 {
  A[1].show();
 }
 if(A[2] instanceof Transport)
 {
  ((Transport)A[2]).deliver();
 }
 else
 {
  A[2].show();
 }
 if(A[3] instanceof Transport)
 {
  ((Transport)A[3]).deliver();
 }
 else
 {
  A[3].show();
 }
   }
}

Output

3. Describe abstract class called Shape which has three subclasses say Triangle,Rectangle,Circle.
Define one method area() in the abstract class and override this area() in these three subclasses to calculate for specific object
i.e. area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle.


public abstract class Shape
{
 public void area()
 {
 }
 public static void main(String a[])
 {
  Shape s;
  s=new Triangle(30,20);
  s.area();
  s=new Rectangle(10,10);
  s.area();
  s=new Circle(20);
  s.area();
 }
}
class Triangle extends Shape
{
 double h,b;
 Triangle(double he,double ba)
 {
  h=he;
  b=ba;
 }
 public void area()
 {
  System.out.println("Area of Triangle is:"+(h*b)/2);
 }
}
class Rectangle extends Shape
{
 int h,l;
 Rectangle(int h,int l)
 {
  this.h=h;
  this.l=l;
 }
 public void area()
 {
  System.out.println("Area of Recangle is:"+(h*l));
 }
}
class Circle extends Shape
{
 double r;
 Circle(double r)
 {
  this.r=r;
 }
 public void area()
 {
  System.out.println("Area of Circle is:"+(3.14*r*r));
 }
}

Output

4. Write a program that illustrates interface inheritance. Interface P is extended by P1 and P2.
Interface P12 inherits from both P1 and P2. Each interface declares one constant and one method. Class Q implements P12.
Instantiate Q and invoke each of its methods. Each method displays one of the constants.


interface p
{
 public int p=10;
 public void p();
}
interface p1 extends p
{
 public int p1=20;
 public void p1();
}
interface p2 extends p
{
 public int p2=30;
 public void p2();
}
interface p12 extends p1,p2
{
 public int p12=40;
 public void p12();
}
public class Q implements p12
{
 public void p()
 {
  System.out.println("Value of P is: "+p);
 }
 public void p1()
 {
  System.out.println("Value of P1 is: "+p1);
 }
 public void p2()
 {
  System.out.println("Value of P2 is: "+p2);
 }
 public void p12()
 {
  System.out.println("Value of P12 is: "+p12);
 }
 public static void main(String a[])
 {
  Q obj=new Q();
  obj.p();
  obj.p1();
  obj.p2();
  obj.p12();
 }
}

Output

5. Declare a class called employee having employee_id and employee_name as members.
Extend class employee to have a subclass called salary having designation and monthly_salary as members. Define following:
  • Required constructors
  • A method to find and display all details of employees drawing salary more than Rs. 20000/-
  • Method main for creating an array for storing these details given as command line arguments and showing usage of above methods.


import java.util.Scanner;
class employee
{
 int emp_id;
 String emp_name;
 employee(int id,String name)
 {
  emp_id=id;
  emp_name=name;
 }
}
class Salary extends employee
{
 String designation;
 double emp_salary;
 Salary(int id,String name,String deg,double sal)
 {
  super(id,name);
  designation=deg;
  emp_salary=sal;
 }
 void display()
 {
  System.out.println("Employee Id: "+this.emp_id);
  System.out.println("Employee Name: "+this.emp_name);
  System.out.println("Employee Designation: "+this.designation);
  System.out.println("Employee Salary: "+this.emp_salary);
 }
 public static void main(String a[])
 {
  int n,i,id;
  String name,deg;
  double sal;
  Scanner sc=new Scanner(System.in);
  n=Integer.parseInt(a[0]);
  Salary s[]=new Salary[n];
  System.out.println("Enter "+n+" employee entry");
  for(i=0;i<n;i++)
  {
   System.out.print("\nEnter employee name: " );
   name=sc.next();
   System.out.print("\nEnter employee Designation: ");
   deg=sc.next();
   System.out.print("\nEnter employee Salary: ");
   sal=sc.nextDouble();
   System.out.print("\nEnter employee id: ");
   id=sc.nextInt();
   s[i]=new Salary(id,name,deg,sal);
   System.out.println("");
  }
  for(i=0;i<n;i++)
  {
   if(s[i].emp_salary>20000)
   {
    s[i].display();
   }
  }
 }
}

Output

6. Declare a class called book having author_name as private data member.
Extend book class to have two sub classes called book_publication & paper_publication.
Each of these classes have private member called title.
Write a complete program to show usage of dynamic method dispatch (dynamic polymorphism) to display book or paper publications of given author.
Use command line arguments for inputting data.


class book
{
 private String author_name;
 book(String author)
 {
  author_name=author;
 }
 void display()
 {
  System.out.println("Author name: "+this.author_name);
 }
}
class book_publication extends book
{
 private String book_title;
 book_publication(String name,String title)
 {
  super(name);
  book_title=title;
 }
 void display()
 {
  super.display();
  System.out.println("Book name: "+book_title);
 }
}

class paper_publication extends book
{
 private String paper_title;
 paper_publication(String name,String title)
 {
  super(name);
  paper_title=title;
 }
 void display()
 {
  super.display();
  System.out.println("paper name: "+paper_title);
 }
}
class bookdemo
{
 public static void main(String a[])
 {
  book b;
  String ba,book,pa,p;
  ba=a[0];
  book=a[1];
  pa=a[2];
  p=a[3];
  book_publication bo=new book_publication(a[0],a[1]);
  paper_publication po=new paper_publication(a[2],a[3]);
  b=bo;
  b.display();
  b=po;
  b.display();
 }
}

Output

Share:

3 comments:

  1. A mind blowing article is provided here. Private tutor Bethesda And it is written with great skill and the words directly explain the thought of author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete