Results 1 to 5 of 5
  1. #1
    BettyS is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Question Double vs Int as output in Java

    Hi
    I am in my first Java Programming class and I am having the followintg problem:

    I am constructing a program to show how many years and days are contained in 1 billion minutes.

    Here is my code:


    import java.util.Scanner;

    public class Ex02_07 {
    public static void main (String [] args) {
    Scanner input = new Scanner(System.in);
    //Prompt the user for input
    System.out.print("Enter a number of minutes ");
    double minutes = input.nextDouble();
    double years = minutes / 5256000.0;
    double remainder = years % 365.0;

    System.out.println("There are approximately " + years +
    " years and" + remainder + " days.");
    }
    }

    This produces the following output:


    C:\Users\Owner\Desktop\JavaPractice>javac Ex02_07.java

    C:\Users\Owner\Desktop\JavaPractice>java Ex02_07
    Enter a number of minutes 10000000000

    There are approximately 1902.5875190258753 years and 77.58751902587528 days.

    C:\Users\Owner\Desktop\JavaPractice>

    I need the decimal part in order to figure how many days are left after the number of years are computed, but I do not want the decimal part to show in the output statement in either the years portion nor the days portion. Also the days should be approximately 214: not 77.58751902...

    What am I missing? Can someone please point me in the right direction?

    Thanks,
    BettyS

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Double vs Int as output in Java

    • First off, you're using "magic" numbers and should avoid doing that. What does 5256000.0 mean? If it is the product of numbers, show the numbers in order to help make your code self-commenting.
    • Next, if you need to do int division, do int division and not double division.
    • Next, if you want to show the remainder of a division, the numerator and denominator for the division are usually the same first and 2nd numbers in a mod operation to calculate the remainder. Your numbers are different -- why?
    • Please use [code] [/code] tags when posting code on this forum so that we can read it more easily.

  3. #3
    BettyS is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Post Re: Double vs Int as output in Java

    I am in my first Java Programming class and I am having the followintg problem:

    I am constructing a program to show how many years and days are contained in 1 billion minutes.

    Here is my code:


    Java Code:
     import java.util.Scanner;
    
      public class Ex02_07 {
        public static void main (String [] args) {
          Scanner input = new Scanner(System.in);
          //Prompt the user for input
          System.out.print("Enter a number of minutes ");
          double minutes = input.nextDouble();
          double years = minutes / 525600.0; // number of minutes in years is 525600
          double remainder = years % 365.0; //number of days in year is 365
              
          System.out.println("There are approximately   " + years + 
            " years and" + remainder + "    days."); 
      }
    }
    This produces the following output:


    C:\Users\Owner\Desktop\JavaPractice>javac Ex02_07.java

    C:\Users\Owner\Desktop\JavaPractice>java Ex02_07
    Enter a number of minutes 10000000000

    There are approximately 1902.5875190258753 years and 77.58751902587528 days.

    C:\Users\Owner\Desktop\JavaPractice>

    I need the decimal part in order to figure how many days are left after the number of years are computed, but I do not want the decimal part to show in the output statement in either the years portion nor the days portion. Also the days should be approximately 214: not 77.58751902...

    What am I missing? Can someone please point me in the right direction?

    Thanks,
    BettyS

    Hi Fubarable:

    Thank you for answering.

    I hope my edited post above is easier to read and understand. :)

    BettyS

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Double vs Int as output in Java

    You still have magic numbers. I suggest that you use constants so that your calculations look something more like:

    Java Code:
          int minutes = 1000 * 1000 * 1000;
          System.out.println("billion = " + minutes);
          
          int hours = minutes / MINUTES_PER_HOUR;
    Where MINUTES_PER_HOUR is a constant defined in the program as
    Java Code:
    private static final int MINUTES_PER_HOUR = 60;
    With similar constants for the equivalent days and years calculations. Then calculate the remainder from *each* calculation of this type that you do. Again, the two function parameters (here the numerator and denominator) must be the same for division and for mod operation.

  5. #5
    BettyS is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Double vs Int as output in Java

    Quote Originally Posted by Fubarable View Post
    You still have magic numbers. I suggest that you use constants so that your calculations look something more like:

    Java Code:
          int minutes = 1000 * 1000 * 1000;
          System.out.println("billion = " + minutes);
          
          int hours = minutes / MINUTES_PER_HOUR;
    Where MINUTES_PER_HOUR is a constant defined in the program as
    Java Code:
    private static final int MINUTES_PER_HOUR = 60;
    With similar constants for the equivalent days and years calculations. Then calculate the remainder from *each* calculation of this type that you do. Again, the two function parameters (here the numerator and denominator) must be the same for division and for mod operation.
    Thanks Fubarable:

    I think that is what I needed. :)
    BettyS

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2013, 09:38 AM
  2. Replies: 4
    Last Post: 05-10-2012, 08:03 AM
  3. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Tags for this Thread

Posting Permissions

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