CoreJava Day2Assignments
CoreJava Day2Assignments
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
You must not allow the user to declare an object of Instrument class.
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");
}
}
switch(x)
{
case 1: c[0] = new FirstClass();
c[0].notice();
break;
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.
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;
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;