JAVA ONLY I have created an Oval class that demonstrates how to create a subclass from GeometricShape. I have also reco

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

JAVA ONLY I have created an Oval class that demonstrates how to create a subclass from GeometricShape. I have also reco

Post by answerhappygod »

JAVA ONLY
I have created an Oval class that demonstrates how to create a
subclass from GeometricShape. I have also recorded a demo
video explaining each piece of the class.
Using the supplied code as your guide, create a Trapezoid class
that has the following:
1. Setters for all private attributes.
2. Getters for all private attributes.
3. A constructor with no parameters. Unlike Oval, have it
call its own constructor with default parameters.
4. A constructor with parameters for all private attributes,
filled, and color.
5. A toString method that overrides GeometricObject’s.
6. A getArea method that returns the area of the
Trapezoid. The area of a trapezoid is .5 * h * (b1 + b2).
Modify the main so that:
7. It creates a Trapezoid object.
8. It adds that object to the shapes list.
9. It prints the Trapezoid and the area in the for
loop.
MAIN
import java.util.ArrayList;
public class Main {
public static void main(String[] args){
ArrayList<GeometricObject>
shapes = new ArrayList<GeometricObject>();
GeometricObject genericShape = new
GeometricObject();
Oval oval = new Oval(10, 15, "blue",
true);
//TODO: Add Trapezoid creation
here
shapes.add(genericShape);
//polymorphism: the type
GeometricObject (that the shapes list contains)
//can refer to a Oval because Oval
is a subclass of GeometricObject
shapes.add(oval);
//TODO: add Trapezoid to shapes
//"foreach element in shapes, call
it shape"
for(GeometricObject shape:
shapes){
System.out.println(shape.toString());
//GeometricShape does
not have a getArea(), so we have to check to
//see what subclass it
is, and then cast appropriately
if( shape instanceof
Oval ){
System.out.println(((Oval)shape).getArea());
}
//TODO: print
trapezoid getArea
System.out.println("======================");
}
}
}
GeometricObject.java
public class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new
java.util.Date();
}
/** Construct a geometric object with the
specified color and filled value */
public GeometricObject(String color, boolean
filled){
dateCreated = new
java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
its getter method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return string representation of this object
*/
public String toString() {
return "created on " + dateCreated +
"\ncolor: " + color + " and filled: " + filled;
}
}
Oval.java
ublic class Oval extends GeometricObject {
private double a, b;
public Oval() {
//super() is called here because we
don't specify a super constructor
a = 1;
b = 1;
}
public Oval(double a, double b, String color,
boolean filled) {
super(color, filled); //call super
constructor
this.a = a; //use "this." to
differentiate between parameter and attribute
this.b = b;
}
//getters
public double getA() {
return a;
}
public double getB() {
return b;
}
//setters
public void setA(double a) {
this.a = a;
}
public void setB(double b) {
this.b = b;
}
//methods
public double getArea(){
double area = (a / 2) * (b / 2) *
Math.PI;
return area;
}
//overrided methods
@Override
public String toString(){
//call inherited methods from
GeometricObject
return "Oval: " + a + " " + b + "\n"
+ getColor() + ", " + isFilled();
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply