Results 1 to 5 of 5
Thread: Double vs Int as output in Java
- 02-17-2013, 03:40 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
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
-
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.
- 02-17-2013, 04:27 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
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:
This produces the following output: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."); } }
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
-
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:
Where MINUTES_PER_HOUR is a constant defined in the program asJava Code:int minutes = 1000 * 1000 * 1000; System.out.println("billion = " + minutes); int hours = minutes / MINUTES_PER_HOUR;
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.Java Code:private static final int MINUTES_PER_HOUR = 60;
- 02-17-2013, 05:04 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How do I output messages that are in a public static double? Please help!
By xabre1200 in forum New To JavaReplies: 3Last Post: 02-07-2013, 08:38 AM -
Should you always use Double.compare( double d1, double d2 ) for >= and <=?
By stchman in forum New To JavaReplies: 4Last Post: 05-10-2012, 07:03 AM -
double a * double b = weird output
By GPB in forum New To JavaReplies: 3Last Post: 03-26-2010, 10:40 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks