Answer to Question #241926 in Java | JSP | JSF for doeboy

Question #241926
Your application must make use of concepts such as arrays, loops, inheritance, constructors
and information hiding. Output must be shown in the form of a report using the console.
In your solution, make use of as many concepts, and components dealt with in this course, but
pay special attention to the following learning units:
• Learning Unit 1: Advanced arrays.
• Learning Unit 2: Introduction to inheritance
1
Expert's answer
2021-09-25T17:48:26-0400

DESCRIPTION:

INHERITANCE:

Superclass - Shape

Subclasses - Circle and Rectangle are subclasses of Shape superclass

ARRAY

To store an array of shapes in the Tester class

CONSTRUCTORS:

Each class contains the default and parameterized constructor

DATA HIDING:

Each instance variable is declared as private ie accessed only by the getters and setters method.

Loop:

each instance in an array is accessed by for loop.


// Shape.java
/**
 * 
 * The class super Shape
 *
 */
public class Shape {
    private String color;
    private boolean fill;
    /**
     * parameterized constructor
     * 
     * @param color
     * @param fill
     */
    public Shape(String color, boolean fill) {
        this.color = color;
        this.fill = fill;
    }
    /**
     * default constructor
     */
    public Shape() {
    }
    /**
     * @return the color
     */
    public String getColor() {
        return color;
    }
    /**
     * @param color the color to set
     */
    public void setColor(String color) {
        this.color = color;
    }
    /**
     * @return the fill
     */
    public boolean isFill() {
        return fill;
    }
    /**
     * @param fill the fill to set
     */
    public void setFill(boolean fill) {
        this.fill = fill;
    }
    // This method return perimeter of the shape
    public double perimeter() {
        return 0;
    }
    // This method return area of the shape
    public double area() {
        return 0;
    }
    // This method print shape details
    public void info() {
        System.out.println("Shape Color: " + getColor());
        System.out.println("Filled status: " + isFill());
    }
}


// Circle.java
public class Circle extends Shape {
    // instance variable
    private double radius;
    /**
     * @param color
     * @param fill
     * @param radius
     */
    public Circle(String color, boolean fill, double radius) {
        super(color, fill);
        this.radius = radius;
    }
    /**
     * default constructor
     */
    public Circle() {
    }
    /**
     * @return the radius
     */
    public double getRadius() {
        return radius;
    }
    /**
     * @param radius the radius to set
     */
    public void setRadius(double radius) {
        this.radius = radius;
    }
    // This method return perimeter of the shape circle
    @Override
    public double perimeter() {
        return 2 * Math.PI * radius;
    }
    // This method return area of the shape circle
    @Override
    public double area() {
        return Math.PI * radius * radius;
    }
    // This method print shape details Circle
    @Override
    public void info() {
        System.out.println("Circle Color: " + getColor());
        System.out.println("Filled status: " + isFill());
        System.out.println("Circle Area: " + area());
        System.out.println("Circle Perimeter: " + perimeter());

    }

}


// Rectangle.java
public class Rectangle extends Shape {
    // instance variables
    private double width;
    private double height;
    /**
     * @param color
     * @param fill
     * @param width
     * @param height
     */
    public Rectangle(String color, boolean fill, double width, double height) {
        super(color, fill);
        this.width = width;
        this.height = height;
    }
    /**
     * default constructor
     */
    public Rectangle() {
    }
    /**
     * @return the width
     */
    public double getWidth() {
        return width;
    }
    /**
     * @param width the width to set
     */
    public void setWidth(double width) {
        this.width = width;
    }
    /**
     * @return the height
     */
    public double getHeight() {
        return height;
    }
    /**
     * @param height the height to set
     */
    public void setHeight(double height) {
        this.height = height;
    }
    // This method return perimeter of the shape Rectangle
    @Override
    public double perimeter() {
        return 2 * (width + height);
    }
    // This method return area of the shape Rectangle
    @Override
    public double area() {
        return width * height;
    }
    // This method print shape details Rectangle
    @Override
    public void info() {
        System.out.println("Rectangle Color: " + getColor());
        System.out.println("Filled status: " + isFill());
        System.out.println("Rectangle Area: " + area());
        System.out.println("Rectangle Perimeter: " + perimeter());

    }
}


// Test.java
public class Test {

    public static void main(String[] args) {
        // Create an Shape array
        Shape[] shapes = new Shape[2];
        // Create an object of Circle
        Circle circle = new Circle("RED", true, 5);
        // Create an object of Rectangle
        Rectangle rectangle = new Rectangle("Blue", false, 3, 2);
        // add instances to an array
        shapes[0] = circle;
        shapes[1] = rectangle;

        // loop array and print details of each shape
        for (int i = 0; i < shapes.length; i++) {
            shapes[i].info();
            System.out.println();
        }
    }
}


*******************
OUTPUT
*********************

Circle Color: RED
Filled status: true
Circle Area: 78.53981633974483
Circle Perimeter: 31.41592653589793

Rectangle Color: Blue
Filled status: false
Rectangle Area: 6.0
Rectangle Perimeter: 10.0

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