Está en la página 1de 9

Core Java Assignments

Day #2: Inheritance and Polymorphism


Estimated Time : 2.5 Hours

1. Create an abstract class Instrument which is having the abstract function play.
Create three more sub classes from Instrument which is Piano, Flute, Guitar.
Override the play method inside all three classes printing a message

“Piano is playing tan tan tan tan ” for Piano class


“Flute is playing toot toot toot toot” for Flute class
“Guitar is playing tin tin tin ” for Guitar class

You must not allow the user to declare an object of Instrument class.

Create an array of 10 Instruments.

Assign different type of instrument to Instrument reference.

Check for the polymorphic behavior of play method.

Use the instanceof operator to print that which object stored at which index of
instrument array.
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("Yes, Its Flute");
inst[i].Play();
}

if ( inst[i] instanceof Guitar )


{
System.out.println("Yes, Its Guitar");
inst[i].Play();
}
}
}
}
2. Create an abstract class Compartment to represent a rail coach. Provide an abstract
function notice in this class. Derive FirstClass, Ladies, General, Luggage classes from
the compartment class. Override the notice function in each of them to print notice
suitable to the type of the compartment.

Create a class TestCompartment. Write main function to do the following:

Declare an array of Compartment pointers of size 10.

Create a compartment of a type as decided by a randomly generated integer in the


range 1 to 4.

Check the polymorphic behavior of the notice method.


abstract class Compartment
{
abstract void notice();
}

class FirstClass extends Compartment


{
void notice()
{
System.out.println("Its FIRSTCLASS");
}
}

class Ladies extends Compartment


{
void notice()
{
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;
case 3: c[2] = new General();
c[2].notice();
break;

case 4: c[3] = new Luggage();


c[3].notice();
break;

default: System.out.println("Invalid Choice");


}
}
}
3. Create a class Medicine to represent a drug manufactured by a pharmaceutical
company. Provide a function displayLabel() in this class to print Name and address of
the company.

Derive Tablet, Syrup and Ointment classes from the Medicine class. Override the
displayLabel() function in each of these classes to print additional information
suitable to the type of medicine. For example, in case of tablets, it could be “store in a
cool dry place”, in case of ointments it could be “for external use only” etc.

Create a class TestMedicine . Write main function to do the following:


Declare an array of Medicine references of size 10
Create a medicine object of the type as decided by a randomly generated integer in
the range 1 to 3.

Refer Java API Documentation to find out random generation feature.

Check the polymorphic behavior of the displayLabel() method.


File1
public class Medicine
{
public void displayLabel()
{
System.out.println("Company : Globex Pharma");
System.out.println("Address : Bangalore");
}
}

class Tablet extends Medicine


{
public void displayLabel()
{
System.out.println("store in a cool dry place");
}
}

class Syrup extends Medicine


{
public void displayLabel()
{
System.out.println("Consumption as directed by the
physician");
}
}

class Ointment extends Medicine


{
public void displayLabel()
{
System.out.println("for external use only");
}
}

File2
public class TestMedicine
{
public static void main(String[] args)
{
Medicine m[] = new Medicine[10];
double i = Math.random()*4;
int j = (int) i;
System.out.println(j);

switch(j)
{
case 1: m[0] = new Medicine();
m[1] = new Tablet();
m[0].displayLabel();
m[1].displayLabel();
break;
case 2: m[2] = new Medicine();
m[3] = new Syrup();
m[2].displayLabel();
m[3].displayLabel();
break;

case 3: m[4] = new Medicine();


m[5] = new Ointment();
m[4].displayLabel();
m[5].displayLabel();
break;

default: System.out.println("Invalid Choice");


}
}
}

4. Write a program that accepts two numbers and a operator like (+,-,*, /) as command
line arguments and perform the appropriate operation indicated by operator.

If the user enters any other character the appropriate message will be displayed. The
output of the program should be displayed to the user.
public class Ans24
{
public static void main(String[] args)
{
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
double res;
String s = args[2];
System.out.println("s = "+s);

if ( s .equals("+") )
{
res = a + b;
System.out.println("Its Addition & Sum of "+a+" &
"+b+" is "+res);
}
else if ( s .equals("-") )
{
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");
}
}

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

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

También podría gustarte