Re: Math.Floor
- From: Jeffrey Schwab <jeff@xxxxxxxxxxxxxxxx>
- Date: Sat, 14 Oct 2006 16:55:45 GMT
Tukewl4u wrote:
Can someone direct me in the right direction on this? I just don't understand what it's asking.
An Application of method.floor is rounding a value to the nearest integer.
I think it means: "One thing you can do with the Math.floor() method is to round a number to the nearest integer."
Math.floor(d) returns the biggest (closest to positive infinity) number that is less than d. See the documentation:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#floor(double)
The statement y = math.floor( x + 0.5 ); will round the number x to the nearest integer and assign the result to y.
This says you can round a floating-point number to the nearest integer by adding 0.5 (i.e. 1/2), then calling Math.floor.
Math.floor( 5.2 + 0.5); => 5.0
Math.floor( 5.5 + 0.5); => 6.0
Math.floor(-2.7 + 0.5); => -3.0
Write an application that reads double value and uses the preceding statement to round each of the numbers to the nearest integer.
It sounds like you're supposed to write a program that reads double-precision numbers from standard input and rounds each of them using "Math.floor(number + 0.5)". Getting the numbers out of the input stream will probably be the hard part. My suggestion would be:
1. Wrap a BufferedReader around System.in.
2. Write a regular expression to represent whitespace, e.g:
Pattern whitespace = Pattern.compile("\\s+");
3. Read one line of input text at a time, using the BufferedReader's readLine() method. Keep reading lines until readLine() returns null. Each time you read a line, split it into tokens, e.g:
String[] tokens = whitespace.split(line);
Hopefully, each token you get will represent a number you're supposed to round. You can get the numbers out of the tokens using Double.valueOf(token).
For each number proceeded, display both the original number and rounded number.
Easy stuff now. Just use the technique from the original problem statement to round each number, and output it however you want. My preference would be to wrap a java.io.PrintWriter around System.out and use the printf method. Here is some code that includes nice formatting, and which you can use on each token in turn.
private void showRound(String s) {
out.printf("%16s %16f\n", s,
Math.floor(Double.valueOf(s) + 0.5));
}
One piece of advice if you are new to Java, or to programming in general: Start with a very small program that you can compile and run without problems, even if the program doesn't do anything. For example:
public class Main {
public static void main(String[] args) {
}
}
As you add to your program, compile and run it often. This will help you catch little problems before they become big problems.
If you have trouble, post your code here and the eagle-eye veterans will help you find it.
.
- Follow-Ups:
- Re: Math.Floor
- From: Tukewl4u
- Re: Math.Floor
- References:
- Math.Floor
- From: Tukewl4u
- Math.Floor
- Prev by Date: Re: Hotkey Manager
- Next by Date: Re: Latency In Displaying Dialog
- Previous by thread: Re: Math.Floor
- Next by thread: Re: Math.Floor
- Index(es):
Relevant Pages
|