Answer to Question #298712 in Java | JSP | JSF for Jackie

Question #298712

Write two subclasses of Shape called Circle and Rectangle, as shown in the class diagram.

a) 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().

· Override the inherited toString() method, 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.

b) 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().

· Override the inherited toString() method, 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.


1
Expert's answer
2022-02-16T16:32:43-0500


import java.util.*;


abstract class Shape {
	private String color;
	private boolean filled;


	public Shape() {
		this.color = "green";
		this.filled = true;
	}


	public Shape(String color, boolean filled) {
		this.color = color;
		this.filled = filled;
	}


	public abstract double getArea();


	public abstract double getPerimeter();


	public String toString() {
		String filledStr = "filled";
		if (!filled) {
			filledStr = "Not filled";
		}
		return "A Shape with color of " + color + " and " + filledStr;
	}


	/**
	 * @return the color
	 */
	public String getColor() {
		return color;
	}


	/**
	 * @param color the color to set
	 */
	public void setColor(String color) {
		this.color = color;
	}


	/**
	 * @return the filled
	 */
	public boolean isFilled() {
		return filled;
	}


	/**
	 * @param filled the filled to set
	 */
	public void setFilled(boolean filled) {
		this.filled = filled;
	}
}


class Circle extends Shape {
	private double radius;


	public Circle() {
		this.radius = 1.0;
	}


	public Circle(double radius) {
		this.radius = radius;
	}


	public Circle(double radius, String color, boolean filled) {
		super(color, filled);
		this.radius = radius;
	}


	@Override
	public double getArea() {
		return Math.PI * radius * radius;
	}


	@Override
	public double getPerimeter() {
		return 2 * Math.PI * radius;
	}


	public String toString() {
		return "A Circle with radius= " + radius + ", which is a subclass of " + super.toString();
	}


	/**
	 * @return the radius
	 */
	public double getRadius() {
		return radius;
	}


	/**
	 * @param radius the radius to set
	 */
	public void setRadius(double radius) {
		this.radius = radius;
	}


}


class Rectangle extends Shape {
	private double width;
	private double length;


	public Rectangle() {
		this.width = 1.0;
		this.length = 1.0;
	}


	public Rectangle(double width, double length) {
		this.width = width;
		this.length = length;
	}


	public Rectangle(double width, double length, String color, boolean filled) {
		super(color, filled);
		this.width = width;
		this.length = length;
	}


	@Override
	public double getArea() {
		return width * length;
	}


	@Override
	public double getPerimeter() {
		return 2 * (width + length);
	}


	public String toString() {
		return "A Rectangle with width=" + width + " and length=" + length + ", which is a subclass of "
				+ super.toString();
	}


	/**
	 * @return the width
	 */
	public double getWidth() {
		return width;
	}


	/**
	 * @param width the width to set
	 */
	public void setWidth(double width) {
		this.width = width;
	}


	/**
	 * @return the length
	 */
	public double getLength() {
		return length;
	}


	/**
	 * @param length the length to set
	 */
	public void setLength(double length) {
		this.length = length;
	}


}


class App {


	public static void main(String[] args) {
		Shape[] arr = new Shape[4];
		Circle c1 = new Circle(4.65);
		Circle c2 = new Circle(2.5, "Green", true);
		c1.setColor("Red");
		c1.setFilled(true);
		Rectangle r1 = new Rectangle();
		Rectangle r2 = new Rectangle(3, 3.5, "Green", true);
		r1.setWidth(4.25);
		r1.setLength(3);
		r1.setColor("Purple");
		r1.setFilled(true);
		arr[0] = c1;
		arr[1] = c2;
		arr[2] = r1;
		arr[3] = r2;


		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i].toString());


		}
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS