This is a java program. I need to create the superclass (shape) and two subclass
ID: 659175 • Letter: T
Question
This is a java program. I need to create the superclass (shape) and two subclasses (circle, rectangle) then create a square class as a subclass for Rectangle class based on the methods in this UML diagram and the question guidelines:
Write a superclass called Shape (as shown in the class diagram), which contains Two instance variables color (String) and filled (boolean), two constructors: a no-arg (no-argument) constructor that initializes the color to "green" and filled to true, and a constructor that initializes the color and filled to the given values, Getter and setter for all the instance variables. By convention, the getter for a boolean variable xxx is called isXXX() (instead of getXxx() for all the other types), and a toString() method that returns "A Shape with color of xxx and filled/Not filled".
Write two subclasses of Shape called Circle and Rectangle, as shown in the UML diagram.
The Circle class contains:
An instance variable radius (double), Three constructors as shown. The no-arg constructor initializes the radius to 1.0, Getter and setter for the instance variable radius, Methods getArea() and getPerimeter(), and Override the toString() method inherited, to return "A Circle with radius=xxx, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.
The Rectangle class contains:
Two instance variables width (double) and length (double), Three constructors as shown. The no-arg constructor initializes the width and length to 1.0, Getter and setter for all the instance variables, methods getArea() and getPerimeter(), and override the toString() method inherited, to return "A Rectangle with width=xxx and length=zzz, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.
Write a class called Square, as a subclass of Rectangle. Convince yourself that Square can be modeled as a subclass of Rectangle. Square has no instance variable, but inherits the instance variables width and length from its superclass Rectangle.
Explanation / Answer
Shape.java
public class Shape {
private String color;
private boolean filled;
public Shape() {
this.color = "green";
this.filled = true;
}
public Shape(String color, boolean filled) {
super();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
@Override
public String toString() {
return "Shape [color=" + color + ", filled=" + filled + "]";
}
}
Circle.java
public class Circle extends Shape {
private double radius;
public Circle() {
this.radius = 1.0;
}
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "] which is subclass of " + super.toString();
}
}
Rectanagle.java
public class Rectangle extends Shape {
protected double length;
protected double width;
public Rectangle() {
this.length = 1.0;
this.width = 1.0;
}
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
public Rectangle(double length, double width, String color, boolean filled) {
super(color, filled);
this.length = length;
this.width = width;
}
public Rectangle(String color, boolean filled) {
super(color, filled);
}
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;
}
public double getArea() {
return length * width;
}
public double getPerimeter() {
return 2 * (length + width);
}
@Override
public String toString() {
return "Rectangle [length=" + length + ", width=" + width + "] which is subclass of " + super.toString();
}
}
Square.java
public class Square extends Rectangle {
public Square() {
super();
}
public Square(double side) {
super.length = side;
super.width = side;
}
public Square(double side, String color, boolean filled) {
super(color, filled);
super.length = side;
super.width = side;
}
public double getSide() {
return this.length;
}
public void setSide(double length) {
this.length = length;
}
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public String toString() {
return "Square []";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.