Here's what I have so far. Am I on the right direction? Thanks
import java.util.Scanner;
public class RoundFinder
{
public void determineRound()
{
Scanner input = new Scanner( System.in );
System.out.print("Enter three values seperated by spaces: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double result = round( number1, number2, number3 );
System.out.println( "Rounded Value is : " + result);
}
public double round ( double x, double y, double z)
{
double roundValue = Math.round( x + 0.5);
if ( y > roundValue )
roundValue = y;
//if ( z > maximumValue )
// maximumValue = z;
return roundValue;
}
}
> An Application of method.floor is rounding a value to the nearest
> integer. The statement y = math.floor( x + 0.5 ); will round the
> number x to the nearest integer and assign the result to y. Write an
> application that reads double value and uses the preceding statement
> to round each of the numbers to the nearest integer. For each number
> proceeded, display both the original number and rounded number.
> Here's what I have so far. Am I on the right direction? Thanks
You're off to a good start. There are some issues already, though. The
RoundFinder class cannot be run as a stand-alone program, for example,
since it has no main method.
> import java.util.Scanner;
>
[quoted text clipped - 3 lines]
> {
> Scanner input = new Scanner( System.in );
Good idea using a Scanner. It is probably the easiest way to get the
doubles. The reason I don't care to use it is that you lose the
formatting of the original input, but preserving the input formatting is
not one of the requirements of your assignment, anyway.
> System.out.print("Enter three values seperated by spaces: ");
"Separated," not "seperated."
> double number1 = input.nextDouble();
> double number2 = input.nextDouble();
[quoted text clipped - 3 lines]
>
> System.out.println( "Rounded Value is : " + result);
Don't forget to output the original value, too. That was part of the
assignment.
> }
>
> public double round ( double x, double y, double z)
>
> {
> double roundValue = Math.round( x + 0.5);
That should be Math.floor, not Math.round.
> if ( y > roundValue )
> roundValue = y;
>
> //if ( z > maximumValue )
> // maximumValue = z;
Are y and z supposed to be the minimum and maximum permissible values?
If so, you probably want to give them more descriptive names and
comments. It also might be better to do the checking on the original,
input values if possible, rather than the rounded values, but that will
depend on what you want to achieve.
> return roundValue;
> }
I like to define the main function last.
public static void main(String[] args) {
new RoundFinder().determineRound();
}
> }
Lew - 15 Oct 2006 05:21 GMT
> > An Application of method.floor is rounding a value to the nearest
> > integer. The statement y = math.floor( x + 0.5 ); will round the
> > number x to the nearest integer and assign the result to y.
One wrinkle is that Math.floor( x + 0.5 ) (not math.floor()) and Math.round()
will not perform standard IEEE rounding. The standard specifies the behavior
of Math.rint() - "If two double values that are mathematical integers are
equally close, the result is the integer value that is even."
In other words, if a double value x were exactly equal to 6.5, then
Math.rint(x) would return 6.0, but Math.round(x) would return 7L.
- Lew
Tukewl4u - 15 Oct 2006 16:45 GMT
How's this?
import java.util.Scanner;
import java.util.*;
public class RoundFinder
{
public void determineRound()
{
Scanner input = new Scanner( System.in );
System.out.print("Enter three values separated by spaces: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double result = round( number1, number2, number3 );
System.out.printf("(number1)= %f\n" , number1 );
System.out.printf("(number2)= %f\n" , number2 );
System.out.printf("(number3)= %f\n" , number3 );
System.out.println( "Rounded Value is : " + result);
}
public double round ( double x, double y, double z)
{
double roundValue = Math.floor(x + 0.5);
if ( y > roundValue )
roundValue = y;
if ( z > roundValue )
roundValue = z;
return roundValue;
}
}
OUTPUT:
Enter three values separated by spaces: 33.6
33.8
22.9
(number1)= 33.600000
(number2)= 33.800000
(number3)= 22.900000
Rounded Value is : 34.0
> > An Application of method.floor is rounding a value to the nearest
> > integer. The statement y = math.floor( x + 0.5 ); will round the
[quoted text clipped - 68 lines]
>
>> }
Patricia Shanahan - 15 Oct 2006 16:58 GMT
> How's this?
...
> public double round ( double x, double y, double z)
>
[quoted text clipped - 10 lines]
> }
> }
...
>>> An Application of method.floor is rounding a value to the nearest
>>> integer. The statement y = math.floor( x + 0.5 ); will round the
>>> number x to the nearest integer and assign the result to y. Write an
>>> application that reads double value and uses the preceding statement
>>> to round each of the numbers to the nearest integer. For each number
>>> proceeded, display both the original number and rounded number.
...
What you wrote does not seem to match the problem you said you were
trying to solve. Did the specification change?
Patricia
Tukewl4u - 15 Oct 2006 18:50 GMT
No.
>> How's this?
> ...
[quoted text clipped - 25 lines]
>
> Patricia