Results 1 to 13 of 13

Thread: Java Question

  1. #1

    ZeroPaid Regular

    Join Date
    Feb 2004
    Posts
    91

    Java Question

    I need help from any person that knows Java

    I have writen a program and I am almost done expect stuck on one line of code, I am trying to write a command to make the program always round down

    example 103.7 to 103

    please anyone that can help would be appreciated

    thanx

  2. #2

    N/A

    Join Date
    Sep 2002
    Posts
    2,274
    I dont know in java, but in c++ (which java is based off of) you could do somthing like this:

    int a; (a is an integer variable)
    double b; (b is a real number variable)

    ... input getting data for variable b...

    a = b;

    a will then become a truncated form of b, rounding down regardless of what lies beyond the decimal point.

  3. #3
    muffenme's Avatar

    Registered Moonatic

    Join Date
    May 2003
    Location
    The Moon
    Posts
    732
    It is like c++ but you forgot (int)

    eg

    int a;
    double b;
    ...
    a = (int)b;

  4. #4

    N/A

    Join Date
    Sep 2002
    Posts
    2,274
    Quote Originally Posted by muffenme
    It is like c++ but you forgot (int)

    eg

    int a;
    double b;
    ...
    a = (int)b;
    if you are referring to how it should be in c++, I compiled a program that worked exactly as I described before (including the a = b statement that didn't do any type casting) and it worked perfectly.

  5. #5
    shawners's Avatar

    Hurt no more my son.

    Join Date
    Dec 2002
    Location
    An angel in Heaven and on Earth
    Posts
    7,899
    dont round up if higher then 5?

  6. #6
    zpman's Avatar

    Zeropaid Noob

    Join Date
    Aug 2005
    Posts
    441
    please show me your code so far.

    declaration and method for starters.

    If you want help, I will help you with your code, but I won't write it for you. I was a tutor for many things, including Java.

    I will tell you if you are getting close, or horribly off. That's how I learned it, and will not contribute to cheating. But I will always contribute to teaching/tutoring and learning.

  7. #7

    ZeroPaid Regular

    Join Date
    Apr 2002
    Location
    Stuttgart, Germany
    Posts
    14
    The easiest and savest way:

    int result = (int) Math.floor( value );
    Download Phex

  8. #8
    zpman's Avatar

    Zeropaid Noob

    Join Date
    Aug 2005
    Posts
    441
    Quote Originally Posted by GregorK
    The easiest way:

    int result = Math.floor( value );
    This is correct where:

    roundToInteger( number )

    I just wanted our friend to try a bit harder to figure it out is all, heh.

    so an example of this method could be:

    y = Math.floor(x + .5 );

  9. #9

    ZeroPaid Regular

    Join Date
    Feb 2004
    Posts
    91
    thanx guys for the help I gave up and just turned it in, I will submit the code for you guys to see what I was missing

    what I round up doing to finish the code was cast and make it into an integer but that would knock off one whole number

  10. #10
    zpman's Avatar

    Zeropaid Noob

    Join Date
    Aug 2005
    Posts
    441
    Quote Originally Posted by Herr.Wenz
    thanx guys for the help I gave up and just turned it in, I will submit the code for you guys to see what I was missing

    what I round up doing to finish the code was cast and make it into an integer but that would knock off one whole number
    using float or double?

  11. #11

    ZeroPaid Regular

    Join Date
    Feb 2004
    Posts
    91
    //Here it is boys, see what you can find that I missed and again thanx for the help

    import java.util.Scanner;
    import java.text.DecimalFormat;

    public class Party
    {
    public static void main(String[] args)
    {
    int radius, height,guests;

    double volume,gallons;

    Scanner scan= new Scanner (System.in);

    System.out.println("Enter the radius of the trash can, in inches::");
    radius=scan.nextInt();


    System.out.println("Enter the height of the liquid in the trash can, in inches:");
    height=scan.nextInt();

    volume=Math.PI*Math.pow(radius,2)*height;

    gallons=volume/231.0;

    guests=(int)gallons*3+1; // this is what was giving me the trouble, I added the one back that the casting took off but it seems not all inputs were accurate


    DecimalFormat fmt= new DecimalFormat ("0.#");

    DecimalFormat fmt1=new DecimalFormat ("0");

    System.out.println("Your trash can will hold " + fmt.format(gallons)+" gallons of punch.");

    System.out.println("Invite " + fmt1.format(guests) +" guests over!");


    }
    }

  12. #12
    muffenme's Avatar

    Registered Moonatic

    Join Date
    May 2003
    Location
    The Moon
    Posts
    732
    This another way to do it in java.

    int a;
    double b;

    ...
    a = mod(b,1);

    I don't normally use java so I'm not sure

  13. #13
    ducttapeBigSexy's Avatar

    w00t!

    Join Date
    Oct 2003
    Location
    Over by those boxes
    Posts
    1,018
    Assuming you're free to declare things as you want (as in, this isn't an assignment, etc.), here's what I'd do:

    double guests;
    DecimalFormat df = new DecimalFormat("0");
    // ...
    guests = gallons * 3;
    // ...
    System.out.println("Guests: " + df.format(guests) );


    Otherwise, if guests has to be an integer, just do this:
    guests = Math.floor(gallons * 3);


    If you're still stuck, try looking through Sun's Java Doc: http://java.sun.com/j2se/1.5.0/docs/api/index.html

    P.S. If you want to get really anal, you technically didn't close the scanner (scan.close() ), and the end of your main method should have a System.exit(0); - sometimes, without it, it won't end the program when it's done executing (but, that's mostly if you're dealing with multiple threads, such as when you're making a GUI). Also, the first line in the method should be System.out.println("ducttapeBigSexy is awesome!"); - ok, jk about that last one ;)

Similar Threads

  1. Java Launcher 2.4 released
    By mark9876 in forum News
    Replies: 1
    Last Post: January 21st, 2005, 12:16 AM
  2. question mark>> ? causing download problem??!
    By comical ali in forum BitTorrent
    Replies: 0
    Last Post: December 11th, 2004, 12:21 AM
  3. java download not working on website
    By mp3MaStA88 in forum Problems & Questions
    Replies: 8
    Last Post: November 27th, 2004, 01:30 PM
  4. Replies: 3
    Last Post: October 17th, 2004, 07:24 PM
  5. Q - Java files installed. they needed?
    By skyl3r in forum Windows
    Replies: 5
    Last Post: October 12th, 2004, 04:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •