231k views
1 vote
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;
}
}

1 Answer

1 vote

Answer:

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;

}

}

Step-by-step explanation:

There is nothing with the logic of your code it work fine the problem is with the structure of your code you added an extra curly brace before declaring (r)

****************************** Am talking about this section **********************

public class Circle {

{

private double radius;

**************************** It should be *******************************************

public class Circle {

private double radius;

User AnthoPak
by
4.3k points