Please create a new Project called GeoObjArrayList. In this project, under the d
ID: 3572909 • Letter: P
Question
Please create a new Project called GeoObjArrayList. In this project, under the default package, you will have the following classes:
A super class called GeometricObjects. Use the same code as what you used in the previous exercise.
Two subclasses: Rectangle and Circle – use the same code as in the previous exercises.
Please do not include the interface called Shape in this example . Make sure you define your Circle class as: public class Circle extends Geometric Objects. Also make sure you define your Rectangle class as : public class Rectangle extends GeometricObjects
Tester class called TestGeoObjectsYourName that needs to be completed. Please follow the comments which gives you instructions to complete the code, as given below:
import java.util.ArrayList;
public class TestGeoObjectsYourName {
public static void main(String[] args){
//create an ArrayList of the type Rectangle as shown below:
ArrayList<Rectangle> myRectangleList= new ArrayList<Rectangle>();
//Add five Rectangles to the ArrayList. Two examples are given below.
// Use different types of constructors.
myRectangleList.add(new Rectangle(6,7));
myRectangleList.add(new Rectangle(8,10,”blue”,true));
//…add 3 more Rectangles to myRectangleList….
/*Using a for loop, go over each Rectangle object and print the string returned by the toString() for each arrayList element.*/
// Create another ArrayList called myCircleList of the type Circle as shown below:
ArrayList<Circle> myCircleList= new ArrayList<Circle>();
/*add five Circles to myCircleList. Use different types of constructors. Two examples have been worked out for you*/
myCircleList.add(new Circle(5));
myCircleList.add(new Circle(7, “red”, false));
//Create three more circle objects and add them to the list
/*Using a for loop, printout the string returned by the toString() for each element of myCircleList.*/
// Create a new ArrayList of GeometricObjects called myShapesList, as shown below
ArrayList<GeometricObjects> myShapesList = new ArrayList<GeometricObjects>();
/*add two Rectangle objects (use any Rectangle constructors) to myShapesList. See the code in previous exercise--- how we add Rectangle and circle objects to Shapes ArrayList*/
// add two Circle objects to myShapesList, using any Circle constructors.
// use a for loop to go over each element of myShapesList and for each element, use the toString()).
}
}
My code minus Shapes:
Explanation / Answer
HI, Please find my code.
Please let me know in case of any issue.
#####################
public abstract class GeometricObjects {
private String color;
private boolean filled;
public GeometricObjects(){
this.color = "white";
this.filled = false;
}
/*Construct Geometric Object with specified color and filled value*/
public GeometricObjects(String color, boolean filled){
this.color = color;
this.filled = filled;
}
/* Return Color*/
public String getColor(){
return color;
}
/*Return filled. since filled is boolean we name it isFilled*/
public boolean isFilled(){
return filled;
}
/*Set new color*/
public void setColor(String color) {
this.color = color;
}
/*Set new filled*/
public void setFilled(boolean filled){
this.filled = filled;
}
// abstract methods
public abstract double getArea();
public abstract double getPerimeter();
/* toString method that returns the string representation of object*/
public String toString(){
return "Object color is: " + color + " object filled is: " + filled ;
}
}
############################
class Circle extends GeometricObjects {
private double radius;
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() {
double area = 3.14 * radius * radius;
return area;
}
@Override
public double getPerimeter() {
double perimeter = 2 * 3.14 * radius;
return perimeter;
}
@Override
public String toString() {
return super.toString()+ "; Circle Area is: " + getArea() + "; Perimeter is: " + getPerimeter() ;
}
}
#############################
class Rectangle extends GeometricObjects {
private double lenngth;
private double width;
public Rectangle(double lenngth, double width) {
this.lenngth = lenngth;
this.width = width;
}
public Rectangle(double lenngth, double width, String color, boolean filled) {
super(color, filled);
this.lenngth = lenngth;
this.width = width;
}
@Override
public double getArea() {
return lenngth*width;
}
@Override
public double getPerimeter() {
return 2*(lenngth+width);
}
@Override
public String toString() {
return super.toString()+ "; Rectangle Area is: " + getArea() + "; Perimeter is: " + getPerimeter() ;
}
}
##########################################
import java.util.ArrayList;
public class TestGeoObjectsYourName {
public static void main(String[] args){
//create an ArrayList of the type Rectangle as shown below:
ArrayList<Rectangle> myRectangleList= new ArrayList<Rectangle>();
//Add five Rectangles to the ArrayList. Two examples are given below.
// Use different types of constructors.
myRectangleList.add(new Rectangle(6,7));
myRectangleList.add(new Rectangle(8,10,"blue",true));
//…add 3 more Rectangles to myRectangleList….
myRectangleList.add(new Rectangle(2,2,"green",true));
myRectangleList.add(new Rectangle(3,4,"yellow",false));
myRectangleList.add(new Rectangle(5,6));
/*Using a for loop, go over each Rectangle object and print the string returned by the
* toString() for each arrayList element.*/
for(Rectangle r : myRectangleList){
System.out.println(r.toString());
}
// Create another ArrayList called myCircleList of the type Circle as shown below:
ArrayList<Circle> myCircleList= new ArrayList<Circle>();
/*add five Circles to myCircleList. Use different types of constructors. Two examples have been worked out for you*/
myCircleList.add(new Circle(5));
myCircleList.add(new Circle(7, "red", false));
//Create three more circle objects and add them to the list
myCircleList.add(new Circle(5, "blue", false));
myCircleList.add(new Circle(4, "red", true));
myCircleList.add(new Circle(8, "red", true));
/*Using a for loop, printout the string returned by the toString() for each element of myCircleList.*/
for(Circle c : myCircleList){
System.out.println(c.toString());
}
// Create a new ArrayList of GeometricObjects called myShapesList, as shown below
ArrayList<GeometricObjects> myShapesList = new ArrayList<GeometricObjects>();
/*add two Rectangle objects (use any Rectangle constructors) to myShapesList. See the code in previous exercise--- how we add Rectangle and circle objects to Shapes ArrayList*/
myShapesList.add(new Rectangle(4, 5));
myShapesList.add(new Rectangle(3,4,"yellow",false));
// add two Circle objects to myShapesList, using any Circle constructors.
myShapesList.add(new Circle(5, "blue", false));
myShapesList.add(new Circle(5));
// use a for loop to go over each element of myShapesList and for each element, use the toString()).
for(GeometricObjects g : myShapesList){
System.out.println(g.toString());
}
}
}
/*
Sample run:
Object color is: white object filled is: false; Rectangle Area is: 42.0; Perimeter is: 26.0
Object color is: blue object filled is: true; Rectangle Area is: 80.0; Perimeter is: 36.0
Object color is: green object filled is: true; Rectangle Area is: 4.0; Perimeter is: 8.0
Object color is: yellow object filled is: false; Rectangle Area is: 12.0; Perimeter is: 14.0
Object color is: white object filled is: false; Rectangle Area is: 30.0; Perimeter is: 22.0
Object color is: white object filled is: false; Circle Area is: 78.5; Perimeter is: 31.400000000000002
Object color is: red object filled is: false; Circle Area is: 153.86; Perimeter is: 43.96
Object color is: blue object filled is: false; Circle Area is: 78.5; Perimeter is: 31.400000000000002
Object color is: red object filled is: true; Circle Area is: 50.24; Perimeter is: 25.12
Object color is: red object filled is: true; Circle Area is: 200.96; Perimeter is: 50.24
Object color is: white object filled is: false; Rectangle Area is: 20.0; Perimeter is: 18.0
Object color is: yellow object filled is: false; Rectangle Area is: 12.0; Perimeter is: 14.0
Object color is: blue object filled is: false; Circle Area is: 78.5; Perimeter is: 31.400000000000002
Object color is: white object filled is: false; Circle Area is: 78.5; Perimeter is: 31.400000000000002
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.