Question

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;

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,

* the get 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;

}

@Override

public String toString() {

return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;

}

/** Abstract method getArea */

public abstract double getArea();

/** Abstract method getPerimeter */

public abstract double getPerimeter();

}

Assume that all eight sides of the octagon are of equal length. The area can be computed using the following formula:

area = (2 + 4 / sqrt(2)) * side * side

Draw the UML diagram that involves Octagon, GeometricObject, Comparable, and Cloneable.

Write a test program (OctagonTest.java) that creates an Octagon object with side value 5.00 and displays its area and perimeter. Create a new object using the clone method and compare the two objects using the compareTo method.

The output should look exactly like the following:

Area of the octagon with side value 5.00 is 120.71

Perimeter of the octagon with side value 5.00 is 40.00

Comparison result between an octagon and its clone: 0

Submit the UML diagram file OctagonUML.docx and all three source code files - GeometricObject.java, Octagon.java, and OctagonTest.java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//output

//copyable code

//driver.java

public class driver {

    

     public static void main(String[] args) throws CloneNotSupportedException {

          // object

          Octagon oc1 = new Octagon(5);

          // Display

          System.out.println("\nOctagon:\nArea: " + oc1.get_Area() +

              "\nPerimeter: " + oc1.get_Perimeter());

          // method

          //System.out.println("Cloning Octagon...");

          Octagon octagon2 = (Octagon)oc1.clone();

          // method

          int output = (oc1.compareTo(octagon2));

          if (output == 1)

              System.out.println("Octagon is higher than its clone.");

          else if (output == -1)

              System.out.println("Octagon is lower than its clone.");

          else

              System.out.println("Comparison result between an octagon and its clone: 0");

     }

}

// Octagon.java

public class Octagon extends Geometric_Object

          implements Cloneable, Comparable<Octagon> {

     private double side;

     public Octagon() {

     }

     /** Constructor */

     public Octagon(double side) {

          this.side = side;

     }

    

     public void setSide(double side) {

          this.side = side;

     }

    

     public double getSide() {

          return side;

     }

     @Override

     public double get_Area() {

          return (2 + 4 / Math.sqrt(2)) * side * side;

     }

     @Override

     public double get_Perimeter() {

          return 8 * side;

     }

     @Override

     public int compareTo(Octagon o) {

          if (get_Area() > o.get_Area())

              return 1;

          else if (get_Area() < o.get_Area())

              return -1;

          else

              return 0;

     }

     @Override

     public Object clone() throws CloneNotSupportedException {

          return super.clone();

     }

     @Override

     public String toString() {

          return super.toString() + "\nArea: " + get_Area() +

              "\nPerimeter: " + get_Perimeter();

     }

}

// Geometric_Object.java

public abstract class Geometric_Object {

     private String color = "while";

     private boolean fill;

     private java.util.Date date_Created;

     protected Geometric_Object() {

          date_Created = new java.util.Date();

     }

     protected Geometric_Object(String color, boolean fill) {

          date_Created = new java.util.Date();

          this.color = color;

          this.fill = fill;

     }

    

     public String get_Color() {

          return color;

     }

    

     public void set_Color(String color) {

          this.color = color;

     }

    

     public boolean isfill() {

          return fill;

     }

    

     public void setfill(boolean fill) {

          this.fill = fill;

     }

    

     public java.util.Date getdate_Created() {

          return date_Created;

     }

     @Override

     public String toString() {

          return "created on " + date_Created + "\ncolor: " + color +

              " and fill: " + fill;

     }

    

     public abstract double get_Area();

    

     public abstract double get_Perimeter();

}

//UML Diagram

Add a comment
Answer #1

//output

//copyable code

//driver.java

public class driver {

    

     public static void main(String[] args) throws CloneNotSupportedException {

          // object

          Octagon oc1 = new Octagon(5);

          // Display

          System.out.println("\nOctagon:\nArea: " + oc1.get_Area() +

              "\nPerimeter: " + oc1.get_Perimeter());

          // method

          //System.out.println("Cloning Octagon...");

          Octagon octagon2 = (Octagon)oc1.clone();

          // method

          int output = (oc1.compareTo(octagon2));

          if (output == 1)

              System.out.println("Octagon is higher than its clone.");

          else if (output == -1)

              System.out.println("Octagon is lower than its clone.");

          else

              System.out.println("Comparison result between an octagon and its clone: 0");

     }

}

// Octagon.java

public class Octagon extends Geometric_Object

          implements Cloneable, Comparable<Octagon> {

     private double side;

     public Octagon() {

     }

     /** Constructor */

     public Octagon(double side) {

          this.side = side;

     }

    

     public void setSide(double side) {

          this.side = side;

     }

    

     public double getSide() {

          return side;

     }

     @Override

     public double get_Area() {

          return (2 + 4 / Math.sqrt(2)) * side * side;

     }

     @Override

     public double get_Perimeter() {

          return 8 * side;

     }

     @Override

     public int compareTo(Octagon o) {

          if (get_Area() > o.get_Area())

              return 1;

          else if (get_Area() < o.get_Area())

              return -1;

          else

              return 0;

     }

     @Override

     public Object clone() throws CloneNotSupportedException {

          return super.clone();

     }

     @Override

     public String toString() {

          return super.toString() + "\nArea: " + get_Area() +

              "\nPerimeter: " + get_Perimeter();

     }

}

// Geometric_Object.java

public abstract class Geometric_Object {

     private String color = "while";

     private boolean fill;

     private java.util.Date date_Created;

     protected Geometric_Object() {

          date_Created = new java.util.Date();

     }

     protected Geometric_Object(String color, boolean fill) {

          date_Created = new java.util.Date();

          this.color = color;

          this.fill = fill;

     }

    

     public String get_Color() {

          return color;

     }

    

     public void set_Color(String color) {

          this.color = color;

     }

    

     public boolean isfill() {

          return fill;

     }

    

     public void setfill(boolean fill) {

          this.fill = fill;

     }

    

     public java.util.Date getdate_Created() {

          return date_Created;

     }

     @Override

     public String toString() {

          return "created on " + date_Created + "\ncolor: " + color +

              " and fill: " + fill;

     }

    

     public abstract double get_Area();

    

     public abstract double get_Perimeter();

}

//UML Diagram

Add a comment
Know the answer?
Add Answer to:
Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...
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
  • 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...

  • 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...

  • 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)...

  • 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...

  • 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) 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 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

  • 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...

  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class....

    Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input....

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

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