Professional Documents
Culture Documents
Core Java Assignments: Day #2: Inheritance and Polymorphism
Core Java Assignments: Day #2: Inheritance and Polymorphism
File1
public abstract class Instrument
{
public abstract void Play();
}
File2
class Piano extends Instrument
{
public void Play()
{
System.out.println("Piano is playing tan tan tan tan");
}
}
class Flute extends Instrument
{
public void Play()
{
System.out.println("Flute is playing toot toot toot toot");
}
}
class Guitar extends Instrument
{
public void Play()
{
System.out.println("Guitar is playing tin tin tin ");
}
}
public class Ans21
{
public static void main(String[] args)
{
Instrument inst[] = new Instrument[10];
inst[0] = new Piano();
inst[1] = new Flute();
inst[2] = new Guitar();
inst[3] = new Piano();
inst[4] = new Flute();
inst[5] = new Guitar();
inst[6] = new Piano();
inst[7] = new Flute();
inst[8] = new Guitar();
inst[9] = new Piano();
for ( int i = 0 ; i < inst.length ; i++ )
{
if ( inst[i] instanceof Piano )
{
System.out.println("Yes, Its Piano");
inst[i].Play();
}
if ( inst[i] instanceof Flute )
{
{
System.out.println("Its LADIES Compartment");
}
}
class General extends Compartment
{
void notice()
{
System.out.println("Its GENERAL Compartment");
}
}
class Luggage extends Compartment
{
void notice()
{
System.out.println("Its LUGGAGE");
}
}
public class Ans22
{
public static void main(String[] args)
{
Compartment c[] = new Compartment[10];
double i = Math.random()*5;
int x = (int)i;
System.out.println(x);
switch(x)
{
case 1: c[0] = new FirstClass();
c[0].notice();
break;
case 2: c[1] = new Ladies();
c[1].notice();
break;
File1
public class Medicine
{
public void displayLabel()
{
System.out.println("Company : Globex Pharma");
System.out.println("Address : Bangalore");
}
}
class Tablet extends Medicine
{
res = a - b;
System.out.println("Its Subtraction & Difference between
"+a+" & "+b+" is "+res);
}
else if ( s .equals("*") )
{
res = a * b;
System.out.println("Its Multiplication & Product of
"+a+" & "+b+" is "+res);
}
else if ( s .equals("/") )
{
if ( b == 0 )
{
System.out.println("Division error");
}
else
{
res = a / b;
System.out.println("Its Division & Quotient of
"+a+" & "+b+" is "+res);
}
}
else
System.out.println("Invalid choice");
}
}
Create a class Car which contains members speed, noOfGear. The class has a
method drive() which is responsible to provide starting speed and noOfGears to a Car.
Implement display() method which will display all attributes of Car class.
5.
The class SportCar is derived from the class Car which adds new features
AirBallonType. When this method is invoked, initial speed and gear status must be
displayed on console. Override the display method which display all attribute of the
SportCar. Make use of super class display() method.
class Car
{
private int speed, noOfGear;
public void drive()
{
speed = 60;
noOfGear = 4;
}
public void display()
{
System.out.println("Speed is "+speed+"\nNo. of gears are
"+noOfGear);
}
}
class SportCar extends Car
{
private String s = "Air Ballon Type";
public void display()
{
super.display();
System.out.println("Spl Feature : "+s);
}
}
public class Ans25
{
public static void main(String[] args)
{
SportCar sc = new SportCar();
sc.drive();
sc.display();
}
}