[Solved]Output not coming through properly
Hello everybody,
Its great to see there's a community of people who are willing to help out. If anyone could solve my dilemma that would be awesome because my Computer Science professor is a worthless poop head. With that said, I feel like most of this is right yet the output is not showing up correctly. The main problem being that the radius outputs properly but none of the calculations of area, circumference, or diameter do.
Thank you in advance!
Code :
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Circle {
public static void main(String[] args) {
CircleRadius radius = new CircleRadius();
CircleRadius area = new CircleRadius();
CircleRadius circumference = new CircleRadius ();
CircleRadius diameter = new CircleRadius();
String input;
double r;
DecimalFormat twodigits = new DecimalFormat("#.00");
input = JOptionPane.showInputDialog("Please enter the radius of the circle");
r = Double.parseDouble(input);
radius.set_radius(r);
area.get_Area();
diameter.get_Diameter();
circumference.get_Circumference();
JOptionPane.showMessageDialog(null, "The radius of the circle is " + twodigits.format(radius.get_radius()));
JOptionPane.showMessageDialog(null, "The area of the circle is " + twodigits.format(area.get_Area()));
JOptionPane.showMessageDialog(null, "The diameter of the circle is " + twodigits.format(diameter.get_Diameter()));
JOptionPane.showMessageDialog(null, "The circumference of the circle is " + twodigits.format(circumference.get_Circumference()));
}
}
Code :
public class CircleRadius {
private double radius;
public double PI = 3.14159;
public CircleRadius()
{
radius = 0.0;
}
public CircleRadius(double r)
{
radius = r;
}
public double get_radius()
{
return radius;
}
public void set_radius(double r){
radius = r;
}
public double get_Area()
{
return PI * radius * radius;
}
public double get_Diameter()
{
return radius * 2;
}
public double get_Circumference()
{
return 2 * PI * radius;
}
}
Re: Output not coming through properly
I think the reason you are having the incorrect output is because you are creating too many objects. You don't need a separate object for each calculation. The radius determines the rest of the calculations. You need to take the radius that is entered by the user, and then create just one CircleRadius object. You then use the radius to calculate the rest.
Code java:
CircleRadius radius = new CircleRadius();
String input;
double r;
DecimalFormat twodigits = new DecimalFormat("#.00");
input = JOptionPane.showInputDialog("Please enter the radius of the circle");
r = Double.parseDouble(input);
radius.set_radius(r);
someVariable = theObject.get_Area();
someVariable = theObject.get_Diameter();
someVariable = theObject.get_Circumference();
Re: Output not coming through properly
Assignment requires me to use all the different methods so I cannot really get around doing it the easy way.
What do you mean by the object in there. I cannot put anything for "theObject" there without declaring it
Re: Output not coming through properly
Quote:
Originally Posted by
Camiot
Assignment requires me to use all the different methods so I cannot really get around doing it the easy way.
I'm not sure what you mean. Instantiating only one object will still allow you to use all methods defined within the CircleRadius class.
Re: Output not coming through properly
I just got it my variables had to be the same
radius.set_radius(r);
radius.get_Area();
radius.get_Diameter();
radius.get_Circumference();
JOptionPane.showMessageDialog(null, "The radius of the circle is " + twodigits.format(radius.get_radius()));
JOptionPane.showMessageDialog(null, "The area of the circle is " + twodigits.format(radius.get_Area()));
JOptionPane.showMessageDialog(null, "The diameter of the circle is " + twodigits.format(radius.get_Diameter()));
JOptionPane.showMessageDialog(null, "The circumference of the circle is " + twodigits.format(radius.get_Circumference()));
works!
Re: Output not coming through properly
Re: Output not coming through properly
Yes you did thanks a bunch!