Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a toString method for this class. The method should return a string con

ID: 2247312 • Letter: 1

Question

1. Write a toString method for this class. The method should return a string containing the radius and area of the circle.

2. Write an equals method for this class. The method should accept a Circle object as an argument. It should return true if the argument object contains the same data as the calling object, or false otherwise.

3. Write a greaterThan method for this class. The method should accept a Circle object as an argument. It should return true if the argument object has an area that is greater than the area of the calling object, or false otherwise.

This is what I have but I have ONE error that I can't figure out how to fix: The error is line 3, where it says private double radius. When I use the suggested fix on Netbeans (which changes it to private final double radius) I just another error saying illegal use of expression or something. Can someone help???

public class Circle {
{
private double radius;

public Circle(double r)
{
radius = r;
}

public double getArea()
{
return Math.PI * radius * radius;
}

public double getRadius()
{
return radius;
}

public String toString()
{
String str;
str = "Radius: " + radius +
"Area: " + getArea();
return str;
}

public boolean equals(Circle c)
{
boolean status;

if(c.getRadius() == radius)
status = true;
else
status = false;

return status;
}

public boolean greaterThan(Circle c)
{
boolean status;

if(c.getArea() > getArea())
status = true;
else
status = false;

return status;
}
}

Explanation / Answer

There is no issue in your program. Just the issue is that you have added one opening bracket in class declaration before declaring radius.

The working class is as below: -

Circle.java

public class Circle {

private double radius;

public Circle(double r) {

radius = r;

}

public double getArea() {

return Math.PI * radius * radius;

}

public double getRadius() {

return radius;

}

public String toString() {

String str;

str = "Radius: " + radius + "Area: " + getArea();

return str;

}

public boolean equals(Circle c) {

boolean status;

if (c.getRadius() == radius)

status = true;

else

status = false;

return status;

}

public boolean greaterThan(Circle c) {

boolean status;

if (c.getArea() > getArea())

status = true;

else

status = false;

return status;

}

}

I have created a class also to test this class below is the class: -

MainCircle.java

public class MainCircle {

public static void main(String args[]) {

Circle c = new Circle(2.5);

Circle c1 = new Circle(2.5);

Circle c2 = new Circle(4.5);

System.out.println(c);

System.out.println(c1);

System.out.println(c2);

System.out.println("Is c and c1 equal : "+c.equals(c1));

System.out.println("Is c2 greater than c1 : "+c1.greaterThan(c2));

}

}

Output: -

Radius: 2.5Area: 19.634954084936208
Radius: 2.5Area: 19.634954084936208
Radius: 4.5Area: 63.61725123519331
Is c and c1 equal : true
Is c2 greater than c1 : true

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote