Reputation: 13
I have been looking at my source code and can't figure out what is wrong with it. The problem I think is with the Circle class. When I call the mutators and accessors from the DriverCircle class it's giving me the wrong output. For getDiameter it's just printing out 0s.
public class Circle{
private double radius;
private double pi;
private double diameter;
private double circumference;
private double area;
public Circle(){
pi = Math.PI;
radius = 0;
}
public Circle(double radius){
this.radius = radius;
}
public void setDiameter(){
diameter = (2 * radius);
}
public double getDiameter(){
//diameter = 2 * radius;
return diameter;
}
public void setCircumference(){
circumference = (2 * pi * radius);
}
public double getCircumference(){
//circumference = 2 * pi * radius;
return circumference;
}
public double getArea(){
//area = pi * Math.pow(radius, 2);
return area;
}
public void setArea(){
area = (pi * Math.pow(radius, 2));
}
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return radius;
}
public String toString(){
return "The radius is " + radius;
}
}
(The tester)...
import java.util.Scanner;
public class CircleDriver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the radius: ");
Circle[] circles = new Circle[10];
Circle objectCircle = new Circle();
objectCircle.setRadius(input.nextDouble());
circles[1] = new Circle();
circles[2] = new Circle(2.0);
circles[3] = new Circle(3.5);
circles[4] = new Circle(5.0);
circles[5] = new Circle(0.0);
circles[6] = new Circle(15);
circles[7] = new Circle(25);
circles[8] = new Circle(-7);
circles[9] = new Circle(-10.0);
System.out.println("Initial call to toString():");
for (Circle c : circles)
{System.out.println("\t" + c);}
System.out.println("Call to getRadius (should be same as above):");
for (Circle r : circles)
{if (r != null)
{System.out.println("\t" + r.getRadius());}}
System.out.println("Call to getDiameter (should be twice the value shown above):");
for (Circle d : circles)
{if (d != null)
{System.out.println("\t" + d.getDiameter());}}
System.out.println("Calls to getCircumference:");
System.out.println("\tShould be 2 * PI: " + circles[1].getCircumference());
System.out.println("\tShould be 0.0: " + circles[5].getCircumference());
System.out.println("\nCall to getArea:");
System.out.println("\tShould be PI: " + circles[1].getArea());
System.out.println("\tShould be 0.0: " + circles[5].getArea());
System.out.println("Testing out the setRadius method:");
for (int i = 0; i < circles.length / 2; i++)
{if (circles[i] != null)
{circles[i].setRadius(i);}}
System.out.println("Call to toString after setting the first half of the objects:");
for (Circle c : circles)
{System.out.println("\t" + c);}
}
}
Upvotes: 1
Views: 4801
Reputation: 1
Just setting the radius does not update every function that uses radius; you have to call those functions explicitly or else they remain at Java's default of 0. I would also do any error checking at the Circle(radius) method; that way no negative numbers make it into the set of radii.
Upvotes: 0
Reputation: 106430
I see two problems.
First, and foremost, you don't set the value of pi
if you use the double constructor. You should also use the no-args constructor as well as set the value of your radius:
public Circle(double radius){
this(); // calls the no-args constructor
this.radius = radius;
}
Second, there's no explicit call to setDiameter()
. Funnily enough, that method is a misnomer - it should be calculated whenever the radius is nonzero. setArea
is the same way - you're not passing anything in to that method call. Here's what I would recommend:
setDiameter
to calculateDiameter
to make its intention clear. The reason for the rename is to have this class follow JavaBean conventions with respect to set and get.public Circle(double radius)
, call calculateDiameter()
immediately afterwards.setRadius()
is called, either immediately follow it with a call to calculateDiameter
or violate JavaBean conventions and call it immediately after the value is set.getArea
, but I'll leave that portion as an exercise to the reader.In code, a brief example:
public Circle(double radius){
this(); // calls the no-args constructor
this.radius = radius;
calculateDiameter();
}
public void setRadius(double r) {
radius = r;
calculateDiameter();
}
Upvotes: 0
Reputation: 78683
You constructed a Circle object, which sets the radius, but you never set the diameter. Should probably call setDiameter() from the constructor.
Better yet, delete the setDiameter() method as it's completely unnecessary. Simply make getDiameter() return 2*radius.
Upvotes: 0
Reputation: 285405
Your setter methods should have parameters and use the parameters to set fields. Else they are not in fact setter methods. Your current setter methods should all be discarded, except perhaps setRadius(...)
, and most of the calculations be done in the respective getter methods.
i.e., not
public void setCircumference(){
circumference = (2 * pi * radius);
}
public double getCircumference(){
//circumference = 2 * pi * radius;
return circumference;
}
but rather
public double getCircumference(){
return 2 * Math.PI * radius;
}
Upvotes: 2
Reputation: 31710
You never call setDiameter()
. Your constructor sets the value of the radius, but it doesn't do anything about setting the diameter variable!
You may just want to rewrite getDiameter()
:
public double getDiameter() {
return 2.0 * radius;
}
Upvotes: 1
Reputation: 4087
pi isn't initialized when you provide a value in constructor. Further, storing pi as an instance member is a bit weird. Just user Math.PI in your calculations.
Upvotes: 1