Question

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 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 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;
  }
  /** Abstract method getArea */
  public abstract double getArea();
  /** Abstract method getPerimeter */
  public abstract double getPerimeter();
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

GeometricObject.java

public abstract class GeometricObject<E> implements Comparable<E> {

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 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;

}

/** Return a string representation of this object */

public String toString() {

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

" and filled: " + filled;

}

/** Abstract Method to get the Area of the Geometric Object */

public abstract double getArea();

public abstract double getPerimeter();

/** Static Method to find the Larger among two Same Kind of Geometric Object*/

public static <T extends GeometricObject> T max(T obj1, T obj2){

if(obj1.getArea() > obj2.getArea()){

return obj1;

}else if(obj1.getArea() == obj2.getArea()){

return null; // Return null if both the objects are of same size

}else{

return obj2;

}

}

}

____________________

Circle.java

public class Circle extends GeometricObject<Circle> {

private double radius;

public Circle() {

}

public Circle(double radius) {

this.radius = radius;

}

public Circle(double radius, String color, boolean filled) {

this.radius = radius;

setColor(color);

setFilled(filled);

}

/** Return radius */

public double getRadius() {

return radius;

}

/** Set a new radius */

public void setRadius(double radius) {

this.radius = radius;

}

/** Return area */

public double getArea() {

return radius * radius * Math.PI;

}

/** Return diameter */

public double getDiameter() {

return 2.0 * radius;

}

/** Return perimeter */

public double getPerimeter() {

return 2.0 * radius * Math.PI;

}

/* Print the circle info */

public void printCircle() {

System.out.println("The circle is created " + getDateCreated() +

" and the radius is " + radius);

}

@Override

public int compareTo(Circle circle){

if(this.radius == circle.getRadius()){

return 0;

}else if(this.radius > circle.getRadius()){

return 1;

}else{

return -1;

}

}

}

__________________

Rectangle.java

public class Rectangle extends GeometricObject<Rectangle> {

private double width;

private double height;

public Rectangle() {

}

public Rectangle(double width, double height) {

this.width = width;

this.height = height;

}

public Rectangle(double width, double height, String color,

boolean filled) {

this.width = width;

this.height = height;

setColor(color);

setFilled(filled);

}

/** Return width */

public double getWidth() {

return width;

}

/** Set a new width */

public void setWidth(double width) {

this.width = width;

}

/** Return height */

public double getHeight() {

return height;

}

/** Set a new height */

public void setHeight(double height) {

this.height = height;

}

/** Return area */

public double getArea() {

return width * height;

}

/** Return perimeter */

public double getPerimeter() {

return 2 * (width + height);

}

@Override

public int compareTo(Rectangle rectangle){

if(getArea() == rectangle.getArea()){

return 0;

}else if(getArea() > rectangle.getArea()){

return 1;

}else{

return -1;

}

}

}

_________________

Test.java

public class Test {

public static void main(String[] args) {

  

Circle circle1 = new Circle(10.0);

Circle circle2 = new Circle(5.0);

Circle largerCircle;

Rectangle rectangle1 = new Rectangle(3.0, 4.0);

Rectangle rectangle2 = new Rectangle(4.0, 2.0);

Rectangle largerRectangle;

System.out.println("Two Circles circle1 and circle2 with radius 10.0 and 5.0 respectively");

largerCircle = GeometricObject.max(circle1, circle2);

System.out.println("The larger among the two circle is the one with radius : " + largerCircle.getRadius());

System.out.println(

"Two Rectangles rectangle1 and rectangle2 with (width, height) (3.0,4.0) and (4.0,2.0) respectively");

largerRectangle = GeometricObject.max(rectangle1, rectangle2);

System.out.println("The larger among the two rectangle is the one with (width, height) : ("

+ largerRectangle.getWidth() + "," + largerRectangle.getHeight() + ")");

}

}

__________________

Output:

Two Circles circle1 and circle2 with radius 10.0 and 5.0 respectively
The larger among the two circle is the one with radius : 10.0
Two Rectangles rectangle1 and rectangle2 with (width, height) (3.0,4.0) and (4.0,2.0) respectively
The larger among the two rectangle is the one with (width, height) : (3.0,4.0)

______________Thank You

Add a comment
Know the answer?
Add Answer to:
Can the folllowing be done in Java, can code for all classes and code for 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...

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

  • 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

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • This is a question about Java abstract classes. The correct answer is E and I do...

    This is a question about Java abstract classes. The correct answer is E and I do not understand why. Please be specific! I am a beginner in Java abstract classes and methods... 13.6 What is the output of running class Test? public class Test {   public static void main(String[] args) {     new Circle9();   } } public abstract class GeometricObject {   protected GeometricObject() {     System.out.print("A");   }   protected GeometricObject(String color, boolean filled) {     System.out.print("B");   } } public class Circle9 extends GeometricObject {...

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

  • All Codes are in java, I need the answers asap my assignment is almost due, thanks....

    All Codes are in java, I need the answers asap my assignment is almost due, thanks. I'll give thumbs up. 5. (20 pts)Given the Abstract class below, Shape, draw the UML diagram for a class Square that inherits Shape. Square needs to contain a private variable called side which is a double. It should have a default no argument constructor and an overloaded constructor that takes color, filled and side. Shape -color: String -filled: boolean +Shape() +Shape(color: String, filled: boolean)...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

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