Write a program that fulfills the following criteria: The program should have a
ID: 2246638 • Letter: W
Question
Write a program that fulfills the following criteria:
The program should have a driver class and three levels of inheritance:
Level 1: a class called Shape
Level 2: a class called Rectangle and a class called Circle
Level 3: a class called filledRectangle and class called filledCircle
(You need to create 6 classes total)
The driver class should create two instances of each level 2 class and two instances of each level 3 class with different parameters.
Every shape has a color and an area. The area of a rectangle is calculated differently from the area of a circle.
Each rectangle has a width and a height. Those are initialized by passing them from the driver class when using the Rectangle constructor.
Each circle has a radius. This is initialized by passing it from the driver class when using the circle constructor.
The filledRectangle and filledCircle classes differ from their respective superclass by having a fillColor property and a method that returns the fill color.
The driver class should call the appropriate methods of the objects it creates to print out the following:
The color and area of each rectangle object and each circle object.
If the object is a filledRectangle or a filledCircle, it should print out the fill color as well.
Explanation / Answer
Here I am writing the java code for this question.
abstract class shape{
double area;
String color;
abstract double getArea();
abstract String getColor();
}
class Rectangle extends Shape{
double length,breadth;
public Rectangle(double length,double breadth,String color){
this.length=length;
this.breadth=breadth;
this.color=color;
this.area=length * breadth;
}
double getArea(){
return area;
}
String getColor(){
return color;
}
}
class Circle extends Shape{
double radius;
public Circle(double radius,String color){
this.radius=radius;
this.color=color;
this.area=3.14*radius*radius;
}
double getArea(){
return area;
}
String getColor(){
return color;
}
}
class FilledRectangle extends Rectangle{
String fillcolor;
public FilledRectangle(double length,double breadth,String color,String fillcolor){
super(length,breadth,color);
this.fillcolor=fillcolor;
String getFillcolor(){
return fillcolor;
}
}
class FilledCircle extends Circle{
String fillcolor;
public FilledCircle(double radius,String color,String fillcolor){
super(radius,color);
this.fillcolor=fillcolor;
}
}
public class Tester {
public static void main(String args[]){
Rectangle rect1 = new Rectangle(11,12,"Black");
Rectangle rect2 = new Rectangle(16.5,23.4,"Red");
System.out.println("Rectangle1 = color:"+rect1.getColor()+" area: "+rect1.getArea());
System.out.println("Rectangle2 = color:"+rect2.getColor()+" area: "+rect2.getArea());
Circle c1 = new Circle(4,"Black");
Circle c2 = new Circle(5.5,"Red");
System.out.println("Circle1= color:"+c1.getColor()+" area: "+c1.getArea());
System.out.println("Circle 2= color:"+c2.getColor()+" area: "+c2.getArea());
FilledRectangle fr1 = new FilledRectangle(12,14,"Black","White");
FIlledRectangle fr2 = new FilledRectangle(13.5,25.6,"Red","Blue");
System.out.println("Filledrectangle1= color:"+fr1.getColor()+" area: "+fr1.getArea()+"fillcolor: "+fr1.getFillcolor());
System.out.println("Filledrectangle2 = color:"+fr2.getColor()+" area: "+fr2.getArea()+"fillcolor: "+fr2.getFillcolor());
FilledCircle fc1 = new FilledCircle(12,"Black","White");
FilledCircle fc2 = new FilledCircle(15.8,"Red","Blue");
System.out.println("Filledcircle1= color:"+fc1.getColor()+" area: "+fc1.getArea()+"fillcolor: "+fc1.getFillcolor());
System.out.println("Filledcircle2= color:"+fc2.getColor()+" area: "+fc2.getArea()+"fillcolor: "+fc2.getFillcolor());
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.