Objective:
- Concept of Polymorphism
- Abstraction in Java
- Abstract classes and methods
- Use of Interface
Activity 1:
Task 1:
- Create an abstract class shape and also create classes circle, square, triangle and rectangle as sub classes of class shape
- Each sub class of shape should override a calculate area of class shape
- Your classes should have overloaded constructors that will take the member variables as input
Task 2:
What if we wanted to use the definition of the sub class method? To accomplish this, we can add the keyword abstract to the declaration of the area() method in the shape class.
Task 3:
Another advantage of polymorphism is to keep the object of different sub types of same parent class in one array. The array will be on parent class type. As an exercise create take 5 shapes as input from user. Store these in one array. In one loop print the area of these shapes.
Solution:
import java.util.Scanner;
abstract class Shape {
public String color;
public Shape(String color) {
this.color = color;
}
public abstract double area();
}
class Circle extends Shape {
private double radius;
public Circle(double radius, String color) {
super(color);
this.radius = radius;
}
public double area() {
return Math.PI * Math.pow(radius, 2);
}
}
class Square extends Shape {
private double side;
public Square(double side, String color) {
super(color);
this.side = side;
}
public double area() {
return Math.pow(side, 2);
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width, String color) {
super(color);
this.length = length;
this.width = width;
}
public double area() {
return length * width;
}
}
class Triangle extends Shape {
private double base;
private double height;
public Triangle(double base, double height, String color) {
super(color);
this.base = base;
this.height = height;
}
public double area() {
return 0.5 * base * height;
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Triangle t1 = new Triangle(1.0, 9.0, "Red");
Circle c1 = new Circle(2, "Blue");
Rectangle r1 = new Rectangle(6, 2, "Orange");
System.out.println(t1.area());
System.out.println(t1.color);
System.out.println(c1.area());
System.out.println(r1.area());
int count = 5;
Shape[] shapesArray = new Shape[count];
String color;
for (int i = 0; i < count; i++) {
System.out.println("Press 1 for a triangle, 2 for rectangle, 3 for a circle.");
switch (scanner.nextInt()) {
case 1:
System.out.println("Enter base: ");
double baseVal = scanner.nextDouble();
System.out.println("Enter height: ");
double height = scanner.nextDouble();
System.out.println("Enter color: ");
color = scanner.next();
shapesArray[i] = new Triangle(baseVal, height, color);
break;
case 2:
System.out.println("Enter length: ");
double length = scanner.nextDouble();
System.out.println("Enter width: ");
double width = scanner.nextDouble();
System.out.println("Enter color: ");
color = scanner.next();
shapesArray[i] = new Rectangle(length, width, color);
break;
case 3:
System.out.println("Enter radius: ");
double radius = scanner.nextDouble();
System.out.println("Enter color: ");
color = scanner.next();
shapesArray[i] = new Circle(radius, color);
break;
default:
System.out.println("Invalid input. Enter again.");
i--;
break;
}
}
System.out.println("\nPrinting areas of shapes:");
for (int i = 0; i < count; i++) {
System.out.println("Shape " + (i + 1) + " has area: " + shapesArray[i].area());
}
scanner.close();
}
}
Activity 2:
Define a pure abstract parent interface called IShape. The IShape interface should have the following methods:
double GetArea();
void CalcArea();
Next, define a class named Circle. It should be derived from the IShape interface. It should have the following members:
Private Member Variables:
centerX, a long integer used to hold the x coordinate of the circle’s center.
centerY, a long integer used to hold the y coordinate of the circle’s center.
radius, a double used to hold the circle's radius.
area, a double used to hold the circle's area.
Public Member Functions: constructor—accepts values for centerX, centerY, and radius and set area to zero.
Should call the overridden calcArea function described below.
getCenterX—returns the value in centerX.
getCenterY—returns the value in centerY.
calcArea—calculates the area of the circle(area = 3.14159 * radius * radius) and stores the result in the inherited member area.
Next, define a class named Rectangle. It should be derived from the IShape interface. It should have the following members:
Private Member Variables:
width, a long integer used to hold the width of the rectangle.
length,a long integer used to hold the length of the rectangle.
area, a double used to hold the rectangle's area.
Public Member Functions:
constructor—accepts values for width and length. Should call the overridden calcArea function described below.
getWidth—returns the value in width.
getLength—returns the value in length.
calcArea—calculates the area of the rectangle (area = length * width) and stores the result in the inherited member area.
After you have created these classes, create a driver program that defines a Circle object and a Rectangle object. Demonstrate that each object properly calculates and reports its area.
Solution:
interface IShape {
double getArea();
void calcArea();
}
class Circle implements IShape {
private int centerX;
private int centerY;
private double radius;
private double area;
public Circle(int x, int y, double r) {
centerX = x;
centerY = y;
radius = r;
area = 0;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public double getArea() {
return area;
}
public void calcArea() {
area = Math.PI * radius * radius;
}
}
class Rectangle implements IShape {
private int width;
private int length;
private double area;
public Rectangle(int w, int l) {
width = w;
length = l;
area = 0;
}
public int getWidth() {
return width;
}
public int getLength() {
return length;
}
public double getArea() {
return area;
}
public void calcArea() {
area = length * width;
}
}
class Main {
public static void main(String[] args) {
Circle circle = new Circle(2, 3, 4.5);
circle.calcArea();
System.out.println("Circle Area: " + circle.getArea());
System.out.println("Center: (" + circle.getCenterX() + ", " + circle.getCenterY() + ")");
Rectangle rectangle = new Rectangle(5, 7);
rectangle.calcArea();
System.out.println("Rectangle Area: " + rectangle.getArea());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Length: " + rectangle.getLength());
}
}