[go: up one dir, main page]

0% found this document useful (0 votes)
78 views2 pages

Assignment NP

The document discusses two programming problems. The first involves creating abstract and derived classes for different types of medicines and demonstrating polymorphism. The second creates packages and interfaces for shapes, with classes for a square and rectangle that implement a polygon interface and calculate area and perimeter based on their attributes.

Uploaded by

Nipoorva Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views2 pages

Assignment NP

The document discusses two programming problems. The first involves creating abstract and derived classes for different types of medicines and demonstrating polymorphism. The second creates packages and interfaces for shapes, with classes for a square and rectangle that implement a polygon interface and calculate area and perimeter based on their attributes.

Uploaded by

Nipoorva Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

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.

You might also like