Vous êtes sur la page 1sur 6

MAYANK RAJ 18SCSE1010336 SECTION-3

JAVA
Assignment - Day 3

Concept: Abstraction &Polymorphism

Objective: At the end of the assignment, participants will be able to:

 Create Abstract classes & Interfaces


 Use packages

Problems:

1. Create an abstract class Medicine to represent a drug manufactured by a pharmaceutical


company with attributes price and expiry date and a method getDetails() to print them.

Also include an abstract function displayLabel() in the Medicine class.

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)

Check the polymorphic behavior of the displayLabel() method.

2. Create a package com.shape containing the following classes and interfaces.


An interface Polygon containing the members as given below:

void calcArea( ); Method to calculate area

void calcPeri( ); Method to calculate perimeter

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

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[5];
double i = Math.random()*4;
int j = (int) i;
System.out.println(j);
switch(j)
{
case 1: m[0] = new Medicine();
MAYANK RAJ 18SCSE1010336 SECTION-3

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

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

Vous aimerez peut-être aussi