Jump to content

simple math.round method help

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
I've made a fahrenheit to celsius conversion table with the following source code:
public class HelloWorld {

	public static void main(String[] args) {

		System.out.println("Far-Cel Converter");

		

		int lower, upper, step;

		double far, cel;

		

		lower = 0;

		upper = 300;

		step = 20;

		

		far = lower;

		while (far <= upper){

			cel = (far - 32.0) * 5.0/9.0;

			Math.round(far);

			Math.round(cel);

			System.out.print(far);

			System.out.println("-");

			System.out.println(cel);

			far = far + step;

		}

	}

}

once I initialize the value of variable cel, I call on the math.round method to round the value of cel to the nearest number, but it doesn't for some reason. Does anyone know why?

Thanks :)

#2
Directive

Directive

    Newbie

  • Members
  • Pip
  • 5 posts
Even though you called round on far and cel, you did not update the variables, so when you call print you still get the values before round was called.

If you add an assignment, the problem is fixed.
public class HelloWorld {

	public static void main(String[] args) {


		System.out.println("Far-Cel Converter");

	

		int lower, upper, step;

		double far, cel;

		

		lower = 0;

		upper = 300;

		step = 20;

		

		far = lower;

		while (far <= upper) {

			cel = (far - 32.0) * 5.0/9.0;

                        //update variables

			far =  Math.round(far);

			cel = Math.round(cel);

			System.out.print(far);

			System.out.println("-");

			System.out.println(cel);

			far = far + step;

		}

	}

}


#3
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
ahh ok, thanks! I didn't know that I had to initialize the new value of cel in order to get the return value from math.round.

i get it now, thanks a lot :)

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Strings and primitives (byte, int, double, float, boolean etc. - anything starting with a small letter) will never 'magically' change value like that, just because they can't. A new initialization will always be required.

More classes require this kind of initialization all the time, but in theory they could have done without.
Like:

BigInteger integer = new BigInteger("11");

integer = integer.add(new BigInteger("2"));

In this case you also have to do it, but the Java programmers could have actually done it without needing to do it.
I think they just wanted you to give the option to keep the original value.


Note that if you have something like:

far = lower;

while (far <= upper) {

    ......

    far = far + step;

}

You can replace it by:

for(far = lower ; far<=upper ; far += step){

    ......

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users