Assignment - Day 3: Problems
Assignment - Day 3: Problems
Assignment - Day 3: Problems
JAVA
Assignment - Day 3
Problems:
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 TestMedicinewith the main method that declares an array of Medicine
references of size 5. Create a medicine object of the type Tablet/Syrup/Ointment as decided by
a randomly generated integer in the range 1 to 3. (Refer Java API Documentation to find out
random number generation)
Create a class Square that implements Polygon and has the following member:
side float
Create another class Rectangle that implements Polygon and has the following member:
length float
MAYANK RAJ 18SCSE1010336 SECTION-3
breadth float
In another packagecom.test, create a class that imports the above package and instantiates an object
of the Square class and an object of the Rectangle class.
Call the methods on each of the classes to calculate the area and perimeter given the side and the
length/breadth of the Square class and the Rectangle class respectively.
Answer 1.
File1
public class Medicine
{
String date;
Int P;
Public void getDetails(int P,String date)
{
System.out.println("Price");
System.out.println("Expiry date");
}
public void displayLabel()
{
System.out.println("Company : Globex Pharma");
System.out.println("Address : Bangalore");
}
}
class Tablet extends Medicine
{
MAYANK RAJ 18SCSE1010336 SECTION-3
Answer 2.
interface Polygon
{
void calcPeri();
void calcArea();
}
class Square implements Shape
MAYANK RAJ 18SCSE1010336 SECTION-3
{ double side = 5;
double ar=0;
@Override
public void calcPeri()
{
System.out.println("Perimeter of Square :"+4*side);
}
@Override
public void calcArea()
{
ar = side*side;
System.out.println("Area of Square :"+ar);
}
}
class Rectangle extends Circle
{
double l = 6.0;b = 4.0;
double ar;
public void calcPeri()
MAYANK RAJ 18SCSE1010336 SECTION-3
{
super.calcPeri();
System.out.println("Perimeter of rectangle:"+2(l+b));
}
public void calcArea()
{
super.calcArea();
ar = l * b;
System.out.println("Area of rectangle:"+ar);
}
}
public class Demo
{
public static void main(String[] args)
{
Rectangle obj = new Rectangle();
obj.calcPeri();
obj.calcArea();
}}