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 13 of 13

Thread: I keep getting a returned value of 0

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    Texas
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I keep getting a returned value of 0

    Ok first of all I am a new programmer self teaching myself Java using online tutorials on the Oracle site as well as a Learn Java in 21 days book. I have created the bicycle class from the online tutorials and I am trying to manipulate it and see what I can do with it. The following code assumes that speed is in mph and circumference of the tire is in inches. The formula is designed to get the cadence of the tire based upon the circumference and total speed. At the end of the code it is meant to print out the variables as labeled but keeps giving me 0.0 for cadence. Please let me know if a copy of the bicycle class is needed as well. I could also use an explanation on how to get the origin to print out in the (x, y) format as opposed to some strange code it keeps spitting out at me. Please help!!!
    import java.util.Scanner;
    public class Main {
     
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner scn = new Scanner(System.in);
    		Point origin = new Point (0, 0);
    		Rectangle rectOne = new Rectangle (origin, 100, 50);
    		rectOne.move(5, 10);
     
    		System.out.println ("This is a default rectangle, its origin is " + origin + " and its area is "+ rectOne.getArea());
     
    		Bicycle myBike = new Bicycle (0, 0, 1);
    	    System.out.println ("It is time to go for a bike ride to check out your bikes functionality."
    	    		+ " First enter the circumference of your wheel: ");
    	    int circumference = scn.nextInt();
     
    	    		System.out.println ("The circumference of your wheel is "+ circumference);
    	    		System.out.println ("Now enter how much you would like to speed up: ");
     
    	    	    int i = scn.nextInt();
    	    	    myBike.speedUp(i);
    	    	    int speed = myBike.getSpeed();
    	    	    double cadence = ((speed/60)*63360)/circumference;
    	    	    Math.round(cadence);
     
    	    	    System.out.println ("Your current speed is now: " + speed + " and your current cadence is: "+ cadence);
    	    	    }
    Last edited by Tim_Tow; June 8th, 2012 at 04:07 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I keep getting a returned value of 0

    Do you know about integer arithmetic 50/60 = 0
    Try changing variables to double vs int

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Location
    Texas
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I keep getting a returned value of 0

    Thanks I will try that and see how it works. I was not aware of int arithmetic but I will read up on it now.

  4. #4
    Junior Member
    Join Date
    May 2012
    Location
    Texas
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I keep getting a returned value of 0

    Ok it worked I really appreciate the help on the code as well as the forums. Would still like to know if anybody can tell me how to print out a point in (x, y) format

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I keep getting a returned value of 0

    Build the String to print using the + operator.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    May 2012
    Location
    Texas
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I keep getting a returned value of 0

    What do you mean, like I said I am pretty new to this stuff so I dont understand all of it I am just trying different things.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I keep getting a returned value of 0

    You can build a String to print by using the + operator:
    String aStr = "some " + 4 + " parts";
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    May 2012
    Location
    Texas
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I keep getting a returned value of 0

    ok I think I understand ill try it out

  9. #9
    Junior Member
    Join Date
    May 2012
    Location
    Texas
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I keep getting a returned value of 0

    So far I have not been able to do this successfully once I use the move() method from my Rectangle class. To avoid using your time to teach me the entire Java language I am going to read up some more and learn a bit more on my own. I appreciate all your help so far.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I keep getting a returned value of 0

    You'll have to post the program's output and explain what is wrong with it and also post the code that generates it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: I keep getting a returned value of 0

    A Point object contains method for getting the x and y positions. Use those to print it out rather than just the Point object.
    So it would be something like:

    System.out.println("X coordinate: " + origin.getX() + "Y coordinate: " + origin.gety());

  12. #12
    Junior Member
    Join Date
    May 2012
    Location
    Texas
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I keep getting a returned value of 0

    Input:

    import java.util.Scanner;
    public class Main {
     
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner scn = new Scanner(System.in);
    		int a = 0;
    		int b = 0;
    		Point origin = new Point (a, b);
     
    		Rectangle rectOne = new Rectangle (origin, 100, 50);
    		rectOne.move(5, 10);
     
    		System.out.println ("This is a default rectangle, its origin is " + origin + " and its area is "+ rectOne.getArea());

    Output:
    This is a default rectangle, its origin is Point@284f2189 and its area is 5000

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I keep getting a returned value of 0

    Point@284f2189
    That is the default String returned by an object's toString() method: the classname followed by @ and the hashcode for the object. You will have to call some of the object's methods to get its contents for printing. See the API doc for what methods to use.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Method works, value not returned.
    By SPACE MONKEY in forum Object Oriented Programming
    Replies: 2
    Last Post: April 29th, 2012, 09:23 AM
  2. Help understanding this syntax -- new object returned
    By wy125 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 5th, 2011, 12:18 PM
  3. Ok, Notepad class has returned!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 1st, 2011, 09:50 PM
  4. Error returned
    By elie winter in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2010, 11:49 PM
  5. how to reuse method returned string ??
    By zeeshanmirza in forum Collections and Generics
    Replies: 7
    Last Post: September 4th, 2009, 03:54 PM