Re: Math.Floor
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
"Jeffrey Schwab" <jeff@schwabcenter.com> wrote in message
news:gxaYg.29716$fy.22124@tornado.southeast.rr.com...
Tukewl4u wrote:
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;
public class RoundFinder
{
public void determineRound()
{
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();
double number3 = input.nextDouble();
double result = round( number1, number2, number3 );
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();
}
}