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: Help fixing my first program

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help fixing my first program

    Hey all,

    I'm new to Java and am attempting to write my first program that will determine the circumference of a circle with a given radius. The user will enter only a 'double' number as there is no error handling in this program - it's mainly to get me comfortable using helper classes, objects and methods as well as variable handling. Both classes compile, but it doesn't appear as if the radius is being calculated inside the circumference formula. Here is the code I've written so far:

    import java.util.Scanner;
    /**
     * Write a description of class Circumerence here.
     *
     * @author me
     * @version 1.0
     */
    public class Circumerence
    {
        public static void main(String[] args) {
            double radiusInput = 0.0;
            Scanner keyInput = new Scanner(System.in);
            CircleFormula cf1 = new CircleFormula();
            System.out.print("Enter a radius: ");
            radiusInput = keyInput.nextDouble();
            System.out.println("The circumerence of a circle with a radius of " + radiusInput + " is " + cf1.getRadius());
        }
    }

    /**
     * Write a description of class CircleFormulas here.
     *
     * @author me
     * @version 1.0
     */
    public class CircleFormula
    {
        // radiusInput holds the radius
        private double userRadius;
        // circleOutput holds the circumference
        private double circumferenceOut;
     
       /**
         * Default constructor for CircleFormula sets the default
         * value of the userRadius object to 0.0.
         */
        public CircleFormula() {
            // initialise instance variables
            this(0.0);
        }
     
        /**
         * An example of a method - replace this comment with your own
         *
         * This formula will calculate the circumerence of a circle, given a radius.
         */
        public CircleFormula(double radius)
        {
            // put your code here
            setRadius(radius);
        }
     
        /**
         * The setRadius method sets the radius then calculates the circumerence
         * of the circle.
         */
        public void setRadius(double radius) {
            radius = userRadius;
            circumferenceOut = 2 * 3.14 * radius;
        }
     
        public double getRadius() {
            return circumferenceOut;
        }
     
    }


    I have a feeling I've confused variables and objects but I think I'm thoroughly confused now. If anyone has some pointers of where I might start looking to debug this, that'd be awesome. Not looking for someone to fix it completely, because I want to learn what I've done wrong. :-) Thank you!


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Help fixing my first program

    I've noticed a couple things.

    First of all, look at your main program. You collect the radius, but do you ever pass the radius to the CircleFormula object?

    Next, look at your setRadius method. It takes one parameter, radius. However, in your code, you do this:

    radius = userRadius;

    You tell Java to set the value of the parameter variable, radius, to the value of the instance variable, userRadius. That's pretty problematic, seeing as it doesn't change the instance variable's value at all!

    If you fix those things, I think your program will work.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    javadude (January 4th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help fixing my first program

    Excellent! I fixed it with your help. Here's what I did (for anyone looking at this in the future):

    1. I added cf1.setRadius(radiusInput); after the radiusInput key capture statement in the main class.
    2. I then reversed radius = userRadius to userRadius = radius;


    After that, the program compiled and worked as I would expect. So it looks like I just needed to pass the input radius into the object created by the helper class, then reverse the variable assignment.
    Last edited by javadude; January 1st, 2012 at 09:18 PM.

  5. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Help fixing my first program

    Yay! It works! I'm glad you got it finished!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help fixing my first program

    @javadude: Don't forget to add in reputation (by pressing star button) or saying thanks to the user who helped you.

  7. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help fixing my first program

    Done and done!

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Location
    Nigeria
    Posts
    12
    My Mood
    Cheerful
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Help fixing my first program

    can you pls put a solved also...so dat we dont come here agn
    I AM MONEY B-)

Similar Threads

  1. Fixing the scan input
    By Twoacross in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 14th, 2011, 07:42 AM
  2. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  3. Need help fixing Binary Search Tree code
    By fistpunch in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2010, 11:22 AM
  4. Need simple JAVA program fixing ASAP
    By theviper in forum Paid Java Projects
    Replies: 1
    Last Post: April 14th, 2010, 10:59 AM
  5. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM