1) a) Write an abstract class called Shape that has only one function called are
ID: 3731736 • Letter: 1
Question
1)
a) Write an abstract class called Shape that has only one function called area (returning type double) and a string member variable called color which is public only for classes that inherits Shape. Write two concrete classes called Rectangle and Circle which both inherit the above Shape class. Rectangle has two double member variables width and length. Rectangle has an additional member function called perimeter which returns a double value. Circle has one double member variable called radius.
b) Write the main program to show how polymorphism can be used using a loop to display the area of 5 different objects of type Circle or Rectangle. Show how downcasting can be used in the above polymorphic code to call the perimeter function of Rectangle.
Explanation / Answer
Please find my answer.
//******************************************************************************
//Shape.java
//
//******************************************************************************
public abstract class Shape
{
private String color;
abstract public double area();
public Shape(String color) {
this.color = color;
}
public Shape() {
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
###########
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
super("");
this.length = length;
this.width = width;
}
public Rectangle(double length, double width, String color) {
super(color);
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double area() {
return length*width;
}
public double perimeter() {
return 2*(length+width);
}
@Override
public String toString() {
return super.toString()+ "; Rectangle Area is: " + area() + "; Perimeter is: " + perimeter() ;
}
}
###########
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius, String color) {
super(color);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double area() {
double area = 3.14 * radius * radius;
return area;
}
@Override
public String toString() {
return super.toString()+ "; Circle Area is: " + area() ;
}
}
##########
public class ShapeTest {
public static void main(String[] args) {
Shape[] shapes = {
new Rectangle(2.3, 4, "Blue"),
new Rectangle(5.3, 6),
new Circle(4.5),
new Rectangle(2.3, 4, "Yellow"),
new Circle(6, "Red")
};
for(Shape s : shapes) {
System.out.println("Area: "+s.area());
// downcasting
if(s instanceof Rectangle) {
Rectangle r = (Rectangle)s;
System.out.println("Perimeter: "+r.perimeter());
}
System.out.println();
}
}
}
/*
Sampel run:
Area: 9.2
Perimeter: 12.6
Area: 31.799999999999997
Perimeter: 22.6
Area: 63.585
Area: 9.2
Perimeter: 12.6
Area: 113.03999999999999
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.