Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am working on this Java program and am having issues getting my code to run, but am not getting a compiler error. Where is

I am working on this Java program and am having issues getting my code to run, but am not getting a compiler error. Where is my mistake?

image text in transcribed

public class Test {

public static void main(String[] args)

{

//Circle Objects

Circle circle1 = new Circle(20, "purple", true);

Circle circle2 = new Circle(10, "green", false);

//Output circle1

System.out.println(" Circle 1 = ");

print(circle1);

//Output circle2

System.out.println(" Circle 2 = ");

print(circle2);

//Output the larger circle

print(" The larger circle is: ");

print(Circle.max(circle1, circle2));

//Rectangle Objects

Rectangle rectangle1 = new Rectangle(3, 5, "blue", true);

Rectangle rectangle2 = new Rectangle(4, 7, "yellow", true);

//Output rectangle1

System.out.println(" Rectangle 1 = ");

print(circle1);

//Output rectangle2

System.out.println(" Rectangle 2 = ");

print(circle2);

//Output the larger rectangle

print(" The larger rectangle is: ");

print(Rectangle.max(rectangle1, rectangle2));

}

//Displays the String

public static void print(String s)

{

System.out.println(s);

}

//Displays the GeometricObject

public static void print(GeometricObject o)

{

System.out.println(o);

}

}

public abstract class GeometricObject implements Comparable {

private String color = "white";

private boolean filled;

private java.util.Date dateCreated;

//Construct a default GeometricObject

protected GeometricObject()

{

dateCreated = new java.util.Date();

}

protected GeometricObject(String color, boolean filled)

{

dateCreated = new java.util.Date();

this.color = color;

this.filled = filled;

}

//Return color

public String getColor()

{

return color;

}

//Setting a new color

public void setColor(String color)

{

this.color = color;

}

//Return filled

public boolean isFilled()

{

return filled;

}

//Setting a new filled

public void setFilled(boolean filled)

{

this.filled = filled;

}

//Getting dateCreated

public java.util.Date getDateCreated()

{

return dateCreated;

}

@Override

public String toString()

{

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

" and filled: " + filled;

}

//Compares the size of the GeometricObjects

@Override

public int compareTo(GeometricObject o)

{

if(this.getArea() > o.getArea())

{

return 1;

}

else if(this.getArea()

{

return -1;

}

else

{

return 0;

}

}

public static GeometricObject max(GeometricObject o1, GeometricObject o2)

{

return o1.compareTo(o2) == 1 ? o1 : o2;

}

public abstract double getArea();

public abstract double getPerimeter();

}

public class Rectangle extends GeometricObject {

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 the width

public double getWidth()

{

return width;

}

//Setting a new width

public void setWidth(double width)

{

this.width = width;

}

//Return the height

public double getHeight()

{

return height;

}

public void setHeight(double height)

{

this.height = height;

}

@Override

public double getArea()

{

return width * height;

}

@Override

public double getPerimeter()

{

return (width + height) * 2;

}

//Return the String of the Rectangle Object

@Override

public String toString()

{

return super.toString() + " Width: " + width + " Height: " + height +

" Area: " + getArea() + " Perimeter: " + getPerimeter();

}

}

public class Test {

public static void main(String[] args)

{

//Circle Objects

Circle circle1 = new Circle(20, "purple", true);

Circle circle2 = new Circle(10, "green", false);

//Output circle1

System.out.println(" Circle 1 = ");

print(circle1);

//Output circle2

System.out.println(" Circle 2 = ");

print(circle2);

//Output the larger circle

print(" The larger circle is: ");

print(Circle.max(circle1, circle2));

//Rectangle Objects

Rectangle rectangle1 = new Rectangle(3, 5, "blue", true);

Rectangle rectangle2 = new Rectangle(4, 7, "yellow", true);

//Output rectangle1

System.out.println(" Rectangle 1 = ");

print(circle1);

//Output rectangle2

System.out.println(" Rectangle 2 = ");

print(circle2);

//Output the larger rectangle

print(" The larger rectangle is: ");

print(Rectangle.max(rectangle1, rectangle2));

}

//Displays the String

public static void print(String s)

{

System.out.println(s);

}

//Displays the GeometricObject

public static void print(GeometricObject o)

{

System.out.println(o);

}

}

13.5 (Enable Geometricobject comparable) Modify the Geometricobject class to implement the Comparable interface, and define a static max method in the Geometricobject class for finding the larger of two Geometricobject objects. Draw the UML diagram and implement the new Geometricobject class. Write a test program that uses the max method to find the larger of two circles and the larger of two rectangles

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Theory And Applications 27th Australasian Database Conference Adc 20 Sydney Nsw September 28 29 20 Proceedings Lncs 9877

Authors: Muhammad Aamir Cheema ,Wenjie Zhang ,Lijun Chang

1st Edition

3319469215, 978-3319469218

More Books

Students also viewed these Databases questions

Question

When is it appropriate to use a root cause analysis

Answered: 1 week ago