Question

Define an abstract class GeometricShape with a color (string), filled (boolean), and dateCreated (java.util.Date) GeometricShape has...

Define an abstract class GeometricShape with a color (string), filled (boolean), and dateCreated (java.util.Date)

  1. GeometricShape has getter/setter methods for each attribute, along with a getArea, a getPerimeter, and toString method that returns the shape information in a formatted string including class name, color, filled, dimensions (if available), area, perimeter, and date created
  2. Additionally GeometricShape implements the Java Comparable interface and defines a static method max used to find the larger of two GeometricShapes based on their area
0 0
Add a comment Improve this question Transcribed image text
Answer #1
abstract class GeometricShape implements Comparable<GeometricShape> {
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    protected GeometricShape() {
        dateCreated = new java.util.Date();
    }

    protected GeometricShape(String color, boolean filled) {
        dateCreated = new java.util.Date();
        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;
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public String toString() {
        return "created on " + dateCreated + "\ncolor: " + color +
                " and filled: " + filled;
    }

    @Override
    public int compareTo(GeometricShape o) {
        return Double.compare(getArea(), o.getArea());
    }

    public static GeometricShape max(GeometricShape obj1, GeometricShape obj2) {
        if (obj1.compareTo(obj2) > 0) {
            return obj1;
        } else {
            return obj2;
        }
    }

    /**
     * Abstract method getSurfaceArea
     */
    public abstract double getArea();

    /**
     * Abstract method getPerimeter
     */
    public abstract double getPerimeter();
}

class TestGeometricObject {

    public static void main(String[] args) {
        GeometricShape obj1 = new GeometricShape() {
            @Override
            public double getArea() {
                return 10;
            }

            @Override
            public double getPerimeter() {
                return 5;
            }
        };

        GeometricShape obj2 = new GeometricShape() {
            @Override
            public double getArea() {
                return 7;
            }

            @Override
            public double getPerimeter() {
                return 3;
            }
        };

        GeometricShape max = GeometricShape.max(obj1, obj2);
        System.out.println("Max getArea is " + max.getArea());
    }
}

Add a comment
Know the answer?
Add Answer to:
Define an abstract class GeometricShape with a color (string), filled (boolean), and dateCreated (java.util.Date) GeometricShape has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

  • Can the folllowing be done in Java, can code for all classes and code for the...

    Can the folllowing be done in Java, can code for all classes and code for the interface be shown please. Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometriObject class for finding the larger of two GeometricObject objects. Write a test program that uses the max method to find the larger of two circles, the larger of two rectangles. The GeometricObject class is provided below: public abstract class GeometricObject { private...

  • The software I use is Eclipse, please show how to write it, thanks. GeometricObject class public...

    The software I use is Eclipse, please show how to write it, thanks. GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; protected GeometricObject() { dateCreated = new java.util.Date(); } protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); 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...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • Write the definition of an abstract class shape that define two abstract methods and get Area()...

    Write the definition of an abstract class shape that define two abstract methods and get Area() and getPerlmeter() returning, respectively the area and the perimeter of that shape. Then write the definition and implementation of a derived classes class shape: class Rectangle. Rectangle class defines two private side integer fields and it overrides methods getArea() and getPerimeter() of the shape class.

  • Define a public method that is called isPrime() that returns a boolean and implements the Sieve...

    Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public method that is called numberOfPrimes() that returns the number of prime numbers between 2 and the private attribute value. Show this object in a main method that allows the user to interact with all the public methods of your class. Make a class called MyNumber with an integer private attribute. Make a constructor that defines an integer parameter...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT