0% found this document useful (0 votes)
9K views

09 Java Lab Exercise Polymorphism Solution 1

This document discusses a Java programming lab exercise on polymorphism. It defines classes for Figure, Rectangle, and Triangle. Figure is the base class with dimensions and an area method. Rectangle and Triangle extend Figure and override the area method to return the correct area calculation for each shape. The FindAreas class creates objects of each shape type and uses polymorphism by storing them in a Figure reference variable to call their area methods.

Uploaded by

khairiyah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9K views

09 Java Lab Exercise Polymorphism Solution 1

This document discusses a Java programming lab exercise on polymorphism. It defines classes for Figure, Rectangle, and Triangle. Figure is the base class with dimensions and an area method. Rectangle and Triangle extend Figure and override the area method to return the correct area calculation for each shape. The FindAreas class creates objects of each shape type and uses polymorphism by storing them in a Figure reference variable to call their area methods.

Uploaded by

khairiyah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Computer Programming-2 (CS2301) Polymorphism Lab Session-#09

Java Lab Exercise #09 Polymorphism Evaluation:

Student Name & Number


Instructor: Date:

Q1. Predict the output of the following program: ( final & instanceof )
(i):
Program Output

ERROR! Can't override.

(ii):
Program Output

ob now refers to d
ob is instance of D

ob now refers to c
ob cannot be cast to D
ob can be cast to A

a may be cast to Object


c may be cast to Object
d may be cast to Object

Q2. Write a program (Polymorphism)


Define an class named Figure containing:
- Two instance variable named dim1, dim2 of type double.
1
This study source was downloaded by 100000761285818 from CourseHero.com on 10-03-2022 22:22:57 GMT -05:00

https://www.coursehero.com/file/34709614/09-Java-Lab-Exercise-Polymorphism-Solution-1docx/
Computer Programming-2 (CS2301) Polymorphism Lab Session-#09

- A full parameterized constructor which initiaizes the instance variables.


- A method named area which returns double and also prints a message as “Area for Figure is
undefined”.
Define another class named Rectangle which is extended from class Figure containing:
- A two parameter constructor which initializes all the instance variables of its super class (by
calling its constructor).
- An overridden method named Area which returns the calculated area of the rectangle: (L*W).
Define another class named Triangle which is extended from class Figure containing:
- A two parameter constructor which initializes all the instance variables of its super class (by
calling its constructor).
- An overridden method named Area which returns the calculated area of the triangle: (L*W)/2.

Define a third class named FindArea contains a main method that test the functionality of the
above classes by using super class reference..

Solution:

// Using run-time polymorphism.


class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}

2
This study source was downloaded by 100000761285818 from CourseHero.com on 10-03-2022 22:22:57 GMT -05:00

https://www.coursehero.com/file/34709614/09-Java-Lab-Exercise-Polymorphism-Solution-1docx/
Computer Programming-2 (CS2301) Polymorphism Lab Session-#09

class FindAreas {
public static void main(String args[]) {
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);

Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area());
}}

3
This study source was downloaded by 100000761285818 from CourseHero.com on 10-03-2022 22:22:57 GMT -05:00

https://www.coursehero.com/file/34709614/09-Java-Lab-Exercise-Polymorphism-Solution-1docx/
Powered by TCPDF (www.tcpdf.org)

You might also like