Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Math.Floor

Thread view: 
Tukewl4u - 14 Oct 2006 04:42 GMT
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.
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.
Manish Pandit - 14 Oct 2006 04:53 GMT
public static void testFloor(ArrayList<Double> list){
    for(double d: list){
         System.out.println("Original number is " + d + ", which is
floored to " + Math.floor(d+0.5));
    }
}

-cheers,
Manish
Matt Humphrey - 14 Oct 2006 14:44 GMT
> 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.
> 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.

Your homework assignment is asking you to write a program that reads a
double value and uses the given math statement to round each number to the
nearest integer.  For each number you read in, print it and the rounded
number.

There really is no simpler way to put this--you'll have to talk to your
teacher.

http://home.earthlink.net/~patricia_shanahan/beginner.html

Do you know which compiler you're using?  How to compile and run a program?
How to type in and edit source code?  These are things your teacher or tutor
is best at helping you with.

Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
Jeffrey Schwab - 14 Oct 2006 17:55 GMT
> 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.
Tukewl4u - 14 Oct 2006 18:28 GMT
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;
}
}
>> Can someone direct me in the right direction on this? I just don't
>> understand what it's asking.
[quoted text clipped - 72 lines]
> If you have trouble, post your code here and the eagle-eye veterans will
> help you find it.
Jeffrey Schwab - 14 Oct 2006 20:04 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. 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.