Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Output not coming through properly

  1. #1
    Junior Member
    Join Date
    May 2011
    Location
    Connecticut
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default [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!

    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()));
     
    					}
     
    }




    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;
    }
     
     
     
    }
    Last edited by Camiot; May 16th, 2011 at 09:24 PM. Reason: Solved


  2. #2
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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.

    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();
    Last edited by vanDarg; May 16th, 2011 at 08:45 PM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  3. #3
    Junior Member
    Join Date
    May 2011
    Location
    Connecticut
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by Camiot; May 16th, 2011 at 09:00 PM.

  4. #4
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Output not coming through properly

    Quote Originally Posted by Camiot View Post
    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.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  5. #5
    Junior Member
    Join Date
    May 2011
    Location
    Connecticut
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!

  6. #6
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Output not coming through properly

    Great! Hope I helped.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  7. The Following User Says Thank You to vanDarg For This Useful Post:

    Camiot (May 16th, 2011)

  8. #7
    Junior Member
    Join Date
    May 2011
    Location
    Connecticut
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Output not coming through properly

    Yes you did thanks a bunch!

Similar Threads

  1. my FileReader(i think) isn't performing properly..
    By weej25aya in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 21st, 2011, 10:06 PM
  2. Code doesn't execute properly
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 12:36 PM
  3. Null value coming from Database
    By kosuri.dilip in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 1st, 2011, 03:03 PM
  4. JFrames not coming up?
    By scooty199 in forum AWT / Java Swing
    Replies: 13
    Last Post: October 30th, 2010, 02:54 AM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM