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

Question #298709

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

· A toString() method that returns "A Shape with color of xxx and filled/Not filled".

Write a test program to test all the methods defined in Shape.


1
Expert's answer
2022-02-16T14:54:56-0500
public class Shape {
    private String color;
    private boolean filled;

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

    public Shape(String color, boolean filled) {
        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 "A Shape with color of " + color + " and " + (filled ? "" : "Not ") + "filled.";
    }
}


public class Main {
    public static void main(String[] args) {
        Shape aShape = new Shape();
        System.out.println(aShape);
        System.out.println(aShape.getColor());
        System.out.println(aShape.isFilled());
        aShape.setColor("orange");
        aShape.setFilled(false);
        System.out.println(aShape);
        Shape bShape = new Shape("red", false);
        System.out.println(bShape);
    }
}

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