92.9k views
0 votes
The Circle and CircleTester have been created, but they have errors. The public and private settings for variables and methods are not all correct.

Your job is to go through and fix them. You will need to make edits in both files to get them working correctly, but once complete, your output should match the output below.
Sample Output:
Circle with a radius of 5.0
The diameter is 10.0
The perimeter is 31.41592653589793
CIRCLE.JAVA
public class Circle {
public double radius;
private Circle(double myRadius) {
radius = myRadius;
private void setRadius(int myRadius){
radius = myRadius;
}
private double getDiameter() {
return radius*2;
}
public double getRadius() {
return radius;
}
private double getPerimeter() {
return Math.PI*getDiameter();
}
private String toString() {
return "Circle with a radius of " + radius;
}
}
CIRCLE TESTER.JAVA
public class CircleTester {
public static void main(String[] args) {
Circle circ = new Circle(10);
circ.radius = 5;
System.out.println(circ);
System.out.println("The diameter is " + circ.getDiameter());
System.out.println("The perimeter is " + circ.getPerimeter())
}
}

User Wowo Ot
by
3.7k points

1 Answer

4 votes

Answer:

CIRCLE.JAVA

public class Circle {

private double radius;

public Circle(double myRadius) {

radius = myRadius;

private void setRadius(int myRadius){

radius = myRadius;

}

public double getDiameter() {

return radius*2;

}

public double getRadius() {

return radius;

}

public double getPerimeter() {

return Math.PI*getDiameter();

}

public String toString() {

return "Circle with a radius of " + radius;

}

}

CIRCLE TESTER.JAVA

public class CircleTester {

public static void main(String[] args) {

Circle circ = new Circle(10);

circ.radius = 5;

System.out.println(circ);

System.out.println("The diameter is " + circ.getDiameter());

System.out.println("The perimeter is " + circ.getPerimeter())

}

}

Step-by-step explanation:

public class Circle {

//This could be made private or public.

//Making it private is better

private double radius;

//This is a constructor. It should be made public

//since it would most likely be used in another class

//to create an object of this class.

//Making it private means no other external class can create

//an object of this class.

//Since the tester class (CIRCLETESTER.java), as shown on line 3,

// needs to create

//an object of this class, this should be made public

public Circle(double myRadius) {

radius = myRadius;

private void setRadius(int myRadius){

radius = myRadius;

}

//This should be made public since it will be

// used in another class (CIRCLETESTER.java in this case)

public double getDiameter() {

return radius*2;

}

public double getRadius() {

return radius;

}

//This should be made public since it will be

//used in another class (CIRCLETESTER.java)

public double getPerimeter() {

return Math.PI*getDiameter();

}

//The toString() method is the string representation

//of an object and is called when there is an attempt to

//print the object. It should be made public since it will

//be used in another class (CIRCLETESTER.java)

public String toString() {

return "Circle with a radius of " + radius;

}

}

CIRCLE TESTER.JAVA

public class CircleTester {

public static void main(String[] args) {

Circle circ = new Circle(10);

circ.radius = 5;

System.out.println(circ);

System.out.println("The diameter is " + circ.getDiameter());

System.out.println("The perimeter is " + circ.getPerimeter())

}

}

User Nathan Prometheus
by
4.2k points