Question: Read all of the following instructions before starting this assignment. Use the attached GeometricObject.java, Circle.java, and ShapeBucket.java files to implement a Rectangle class based on
Read all of the following instructions before starting this assignment.
Use the attached GeometricObject.java, Circle.java, and ShapeBucket.java files to implement a Rectangle class based on the following UML class diagram (the Circle.java file is included only to allow the ShapeBucket program to run with the Circles that are already being added).
Implement your Rectangle class in a file named "Rectangle.java"
The formula for the area of a rectangle is width * height
The formula for the perimeter of a rectangle is 2 * (width + height)
Add a "public String toString()" method to the Rectangle class (not shown in the UML) which returns a String containing the width and height field values followed by the results of calling super.toString() as you did with the Circle class in Lab 7.
Modify the main method in the ShapeBucket.java file to add two rectangles with varying width and height in addition to the circles already being added to the ArrayList.
Add code to the ShapeBucket's for-each loop to print each Rectangle object, followed by the Rectangle's perimeter. There is already an "if" condition for the Circle objects, use an "else-if" for the Rectangle (do not use a separate "if" or a separate for-each loop).
Attach and submit your Rectangle.java file and your modified "ShapeBucket.java" file to this assignment. Do not submit the GeometricObject.java file.
Here is sample output you should use to implement your code (you can use your own values for the height and width of the rectangles):
Circle with radius 2.0 created Mon Jul 18 07:44:49 EDT 2016, color green, not filled Area of circle is 12.566370614359172 Circle with radius 3.0 created Mon Jul 18 07:44:49 EDT 2016, color blue, filled Area of circle is 28.274333882308138 Rectangle with width 4.0 height 5.0 created Mon Jul 18 07:44:49 EDT 2016, color black, not filled Perimeter of rectangle is 18.0 Rectangle with width 6.0 height 7.0 created Mon Jul 18 07:44:49 EDT 2016, color white, filled Perimeter of rectangle is 26.0
CIRCLE.JAVA
// Circle.java // Implements a circle class which inherits from GeometricObject public class Circle extends GeometricObject { private double radius = 0.0; public Circle() { super(); } 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 this.radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return this.radius * this.radius * Math.PI; } public double getPerimeter() { return 2 * this.radius * Math.PI; } public double getDiameter() { return 2 * this.radius; } public String toString() { return "Circle with radius " + radius + " " + super.toString(); } } GEOMETRIC.JAVA
// GeometricObject.java // Implements a Geometric Object class import java.util.Date; public class GeometricObject { private String color = "no color"; private boolean filled; private Date dateCreated; public GeometricObject() { dateCreated = new Date(); } public GeometricObject(String color, boolean filled) { this(); this.color = color; this.filled = filled; } public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return this.filled; } public void setFilled(boolean filled) { this.filled = filled; } public Date getDateCreated() { return this.dateCreated; } public String toString() { return "created " + dateCreated + ", color " + color + (filled ? ", filled " : ", not filled "); } } SHAPEBUCKET.JAVA
public class ShapeBucket { public static void main(String[] args) { // create an ArrayList named shapeList that stores GeometricObject objects ArrayList shapeList = new ArrayList(); // add 2 Circle objects to the shapeList container. shapeList.add(new Circle(2, "green", false)); shapeList.add(new Circle(3, "blue", true)); // use a foreach loop to iterate through each object in the shapeList container // and print the properties of the geometric object plus the area of the circle for (GeometricObject g: shapeList) { if (g instanceof Circle) { System.out.println(g + "Area of circle is " + ((Circle)g).getArea()); } } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
