-1

I am doing exercise 2.3 on page 23 of Think Java (http://www.greenteapress.com/thinkapjava/thinkapjava.pdf).

The program converts an arbitrary time stored in variables to seconds, then calculates the amount of seconds remaining in the day, then how much of the day has elapsed in percentage.

Here's the working program:

public class Time {
    public static void main(String[] args) {

    double hour = 21.0; //Represents 9 PM
    double minute = 5.0; //Represents 9:05 PM
    double second = 33.0; //Represents 9:05:33 PM

    final double SEC_IN_MIN = 60.0; //Made final because the amount of seconds in a minute does not change
    final double SEC_IN_HOUR = 3600.0; //Made final for the same reason above.

    final double SEC_SINCE_MN = (SEC_IN_HOUR * hour) + (SEC_IN_MIN * minute) + second; //Calculates the seconds since midnight, or 00:00
    final double SEC_IN_DAY = 24.0 * SEC_IN_HOUR; //Calculates the amount of seconds in a day; 24 stands for the hours in a day

    System.out.printf("The number of seconds since midnight is: %.0f\n", SEC_SINCE_MN);
    System.out.printf("The number of seconds remaining in the day is: %.0f\n", SEC_IN_DAY - SEC_SINCE_MN);
    System.out.printf("The percentage of the day that has passed is: %.0f%%", (100 * SEC_SINCE_MN) / SEC_IN_DAY); // Escape percent sign is %% 100 * to remove decimal value

}

}

I know there is a better way with more advanced code but this is what the assignment required based on what we have learned so far. However, I am not sure double is the best way to represent the variables as I have to use a format specifier to trim the decimal. I asked my professor about it and he said I could change all the variables to int and change the computation in the last print statement to:

System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) * 1.0 / SEC_IN_DAY); 

This does not work because I get a d != java.lang.Double error for the last print statement. If I change the 1.0 to 1, I get no errors put the last output is incorrect.

It says 87% instead of 88% which is the correct output because the decimal value for the last print statement output is 0.08788.

I think it's my computation that needs to be changed for it to work with int.

Any ideas on how to edit the program for int instead of double?

EDIT 1: Code that doesn't work as per my professor's suggestions (returns java.lang.double error for last print statement)

public class Time {

public static void main(String[] args) {

    int hour = 21; //Represents 9 PM
    int minute = 5; //Represents 9:05 PM
    int second = 33; //Represents 9:05:33 PM

    final int SEC_IN_MIN = 60; //Made final because the amount of seconds in a minute does not change
    final int SEC_IN_HOUR = 3600; //Made final for the same reason above.

    final int SEC_SINCE_MN = (SEC_IN_HOUR * hour) + (SEC_IN_MIN * minute) + second; //Calculates the seconds since midnight, or 00:00
    final int SEC_IN_DAY = 24 * SEC_IN_HOUR; //Calculates the amount of seconds in a day; 24 stands for the hours in a day

    System.out.printf("The number of seconds since midnight is: %d\n", SEC_SINCE_MN);
    System.out.printf("The number of seconds remaining in the day is: %d\n", SEC_IN_DAY - SEC_SINCE_MN);
    System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) * 1.0 / SEC_IN_DAY); // Escape percent sign is %%. 100 * to remove decimal value

}

}

EDIT 2: Code that works but doesn't give the correct output of 88% for the last print statement

public class Time {

public static void main(String[] args) {

    int hour = 21; //Represents 9 PM
    int minute = 5; //Represents 9:05 PM
    int second = 33; //Represents 9:05:33 PM

    final int SEC_IN_MIN = 60; //Made final because the amount of seconds in a minute does not change
    final int SEC_IN_HOUR = 3600; //Made final for the same reason above.

    final int SEC_SINCE_MN = (SEC_IN_HOUR * hour) + (SEC_IN_MIN * minute) + second; //Calculates the seconds since midnight, or 00:00
    final int SEC_IN_DAY = 24 * SEC_IN_HOUR; //Calculates the amount of seconds in a day; 24 stands for the hours in a day

    System.out.printf("The number of seconds since midnight is: %d\n", SEC_SINCE_MN);
    System.out.printf("The number of seconds remaining in the day is: %d\n", SEC_IN_DAY - SEC_SINCE_MN);
    System.out.printf("The percentage of the day that has passed is: %d%%", (100 * SEC_SINCE_MN) / SEC_IN_DAY); // Escape percent sign is %%. 100 * to remove decimal value

}

}

EDIT 3: This is not a dupe question. My question was how to convert the variables to int from doubles, so the percentage was calculated correctly in the last statement. I was not asking about rounding although it did play a part in the question/answer.

srocode
  • 21
  • 5
  • So, you're asking how to round a double, is that right? https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double- – JB Nizet Sep 17 '16 at 18:41
  • @JB Nizet, no. My program as is gives me the correct outputs. For all intents and purposes, I have completed the assignment successfully using double variables instead of int. But, the variables should be int instead but I don't know how to make the last statement work when the variables are int. – srocode Sep 17 '16 at 18:47
  • How about showing us the code that doesn't work, rather that the code that does work? Tell us precisely what you expect to happen, and what happens instead. But, `(100 * SEC_SINCE_MN) * 1.0 / SEC_IN_DAY` is a double, and you want to round it. – JB Nizet Sep 17 '16 at 18:52
  • @JBNizet, Okay, will do in a few. – srocode Sep 17 '16 at 18:55

2 Answers2

0

The int throws what's after the period. You may want to duplicate in 10 more. Get the first numeral to check if it greater than 5 add one to your percent. Sorry for not writing code. I'm on iOS now and my java rusty... Good luck

Yitzchak
  • 3,303
  • 3
  • 30
  • 50
-1
(100 * SEC_SINCE_MN) / SEC_IN_DAY

is multiplying the integer SEC_SINCE_MN by the integer 100, and then divides it by the integer SEC_IN_DAY. So that's an integer division. It just truncates the decimal part of the result.

What you want is to compute the accurate percentage, with the decimals, and then round the result. So you need a floating point division:

(100.0 * SEC_SINCE_MN) / SEC_IN_DAY

That will produce a double value (87.88541666666667), that you then need to round:

Math.round((100.0 * SEC_SINCE_MN) / SEC_IN_DAY)
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you, this gives me the answer I am looking for (and will be very useful for later), however, it doesn't satisfy the assignment because it involves using Math.round which hasn't been covered in my class yet. – srocode Sep 17 '16 at 19:09
  • I will add this as an alternate version and look into Math.round so I could explain how and why it works. If you have any idea how to accomplish this without Math.round, please let me know. – srocode Sep 17 '16 at 19:10
  • Then keep `%.0f%%`, and use `(100.0 * SEC_SINCE_MN) / SEC_IN_DAY`. All the computation part is done using integers, except the last one which needs to be a double. That said, a very, very, very large part of being a developer consists in knowing nothing about a subject, read documentation to learn, and try things you never learnt in class. Your teacher should be happy that you manage to be autonomous and use classes you've never seen in class. – JB Nizet Sep 17 '16 at 19:10
  • Ah yes, thank you! This works perfectly as well and is exactly what I needed. Now, just for my understanding, the 100.0 can be added to convert the output to double so it prints the decimal value and %.0f allows it to round and truncate to the nearest whole number to 1, correct? – srocode Sep 17 '16 at 19:16
  • to round, not truncate. It's one or the other. But yes, you got the idea. – JB Nizet Sep 17 '16 at 19:19
  • Got it. Thank you! – srocode Sep 17 '16 at 19:20
  • The part about about *100/100 is [fallacious](http://stackoverflow.com/a/12684082/207421). It cannot possibly reduce the number of decimal places in the double to 2, as doubles dint have decimal places in the first, err, place. – user207421 Sep 18 '16 at 00:21
  • I don't know where you're seeing *100/100, @EJP. – srocode Sep 18 '16 at 11:35
  • Also, @JBNizet, I don't know why your answer was downvoted. You helped me with exactly what I needed. – srocode Sep 18 '16 at 11:38
  • Don't worry about that. I'm happy to have helped. – JB Nizet Sep 18 '16 at 11:40
  • @JBNizet, I almost missed your statement about being a developer. I have skipped ahead in chapters and used things not in chapter 2 but my prof. prefers we stick to the script. :/ It's an unfortunate part of in-class learning, bending to the rules of someone else. I am teaching myself Java outside of the course too to cover knowledge gaps and am happy I am able to learn enough to stay ahead of the class. Again, thank you so much for your help! – srocode Sep 18 '16 at 11:53