Topic
  • 1 reply
  • Latest Post - ‏2007-05-15T16:49:19Z by SystemAdmin
SystemAdmin
SystemAdmin
210 Posts

Pinned topic MathFloor

‏2007-05-10T00:05:08Z |
Please help... I am trying to write an applet that has the following four methods for rounding a number x in various ways:
1. roundToInteger( double number ) double y = Math.floor ( X )
2. roundToTenths( float number ) float y = Math.floor ( X ) / 10
3. roundToHundredths( double number ) double y = Math.floor ( X ) / 100
4. roundToThousandths( double number ) double y = Math.floor ( X ) / 1000

if I am populating the code via user input I will have to use the parse function, for example:

String InputasString = JOptionPane.showInputDialog("Message");
int InputasInteger = Integer.parseInt (Input);

I believe this works for any data type...wouldn't it?

but how do i write an applet with error conditions
I though of error conditions as error conditions when the user inputs something that can't be parsed as an int?

String inputAsString = JOptionPane.showInputDialog("Message");
try {
int inputAsInteger = Integer.parseInt (inputAsString);
// inputAsInteger will be a proper int here
// ...
} catch(NumberFormatException nfe) {
// Here you do whatever you want when the user
// has entered bad data
}



how do i write an applet?
Updated on 2007-05-15T16:49:19Z at 2007-05-15T16:49:19Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    210 Posts

    Re: MathFloor

    ‏2007-05-15T16:49:19Z  
    It seems to me that you have missed a step in your code. Here's what I believe you intended:

    codepublic double roundToInteger(double number){
    return Math.floor(number); // note that this is just Math.floor()
    }[/code]
    codepublic float roundToTenths(float number) {
    return (float) (Math.floor(number * 10.0F)) / 10.0F;
    // number implicitly promoted to double
    // note the needed parens around the Math.floor() invocation
    }[/code]
    codepublic double roundToHundredths(double number) {
    return (Math.floor(number * 100.0)) / 100.0;
    // note the needed parens around the Math.floor() invocation
    }[/code]
    codepublic double roundToThousandths(double number) {
    return (Math.floor(number * 1000.0)) / 1000.0;
    // note the needed parens around the Math.floor() invocation
    }[/code]

    You also want to modify your user input:

    codepublic double acceptUserInput() {
    String inputAsString = JOptionPane.showInputDialog("Some appropriate message");
    boolean digitsOnly = true;
    int decimalsFound = 0;
    for (int i = 0; i < inputAsString.length(); i++) {
    if (!Character.isDigit(inputAsString.charAt(i))) {
    if ('.' == inputAsString.charAt(i)) {
    decimalsFound++;
    } else {
    digitsOnly = false;
    break;
    }
    }
    }
    if (digitsOnly) {
    if (decimalsFound < 2) {
    return Double.parseDouble(inputAsString);
    } else {
    return Double.NaN;
    }
    } else {
    return Double.NaN;
    }
    }[/code]

    Does this help?

    Mark Francis Jaeger
    Knowledge Transfer Manager
    Michigan MMIS Project

    mark.jaeger@cns-inc.com
    (517) 321-2071
    fax: (517) 321-1518

    6465 MILLENNIUM STE 150
    LANSING MI 48917-6879

    There is no trick at all to learning something new - as long as it is something extra - something to supplement what we already know.
    The problem with learning comes when we have to replace something we already know with something new.

    • Bill Waddell

    Mark Francis Jaeger
    Knowledge Transfer Manager
    Michigan MMIS Project

    mark.jaeger@cns-inc.com
    (517) 321-2071
    fax: (517) 321-1518

    6465 MILLENNIUM STE 150
    LANSING MI 48917-6879

    There is no trick at all to learning something new - as long as it is something extra - something to supplement what we already know.
    The problem with learning comes when we have to replace something we already know with something new.

    • Bill Waddell

    Mark Francis Jaeger
    Knowledge Transfer Manager
    Michigan MMIS Project

    mark.jaeger@cns-inc.com
    (517) 321-2071
    fax: (517) 321-1518

    6465 MILLENNIUM STE 150
    LANSING MI 48917-6879

    There is no trick at all to learning something new - as long as it is something extra - something to supplement what we already know.
    The problem with learning comes when we have to replace something we already know with something new.

    • Bill Waddell

    Mark Francis Jaeger
    Knowledge Transfer Manager
    Michigan MMIS Project

    mark.jaeger@cns-inc.com
    (517) 321-2071
    fax: (517) 321-1518

    6465 MILLENNIUM STE 150
    LANSING MI 48917-6879

    There is no trick at all to learning something new - as long as it is something extra - something to supplement what we already know.
    The problem with learning comes when we have to replace something we already know with something new.

    • Bill Waddell

    Mark Francis Jaeger
    Knowledge Transfer Manager
    Michigan MMIS Project

    mark.jaeger@cns-inc.com
    (517) 321-2071
    fax: (517) 321-1518

    6465 MILLENNIUM STE 150
    LANSING MI 48917-6879

    There is no trick at all to learning something new - as long as it is something extra - something to supplement what we already know.
    The problem with learning comes when we have to replace something we already know with something new.

    • Bill Waddell