My program is supposed to print out a table like this:

  Time          Fraction Since Midnight
     12:00 AM       0.0000
      1:00 AM       0.0417
      2:00 AM       0.0833
      3:00 AM       0.1250
      4:00 AM       0.1667
      5:00 AM       0.2083
      6:00 AM       0.2500
      7:00 AM       0.2917
      8:00 AM       0.3333
      9:00 AM       0.3750
     10:00 AM       0.4167
     11:00 AM       0.4583
     12:00 PM       0.5000
      1:00 PM       0.5417
      2:00 PM       0.5833
      3:00 PM       0.6250
      4:00 PM       0.6667
      5:00 PM       0.7083
      6:00 PM       0.7500
      7:00 PM       0.7917
      8:00 PM       0.8333
      9:00 PM       0.8750
     10:00 PM       0.9167
     11:00 PM       0.9583

It even compiles, but when I run it I get an error. I don't know how to fix it. Here's what I have:

public class FractionOfDay {
    static double frac = 0;
    static double timeSecs = 0;
    static int time = 0;

    public static void fractionOfDay( int hour, int minute, int second, char 
    half) {


    if (hour == 12 && minute == 0 && second == 0 && half == 'A') 
    {
      frac = 0.0000;
      System.out.printf("%8.4f", frac);

     }

        else if (half == 'A') {
          timeSecs = ((hour * 3600) + (minute * 60) + second);
          frac = (timeSecs / 86400);
          System.out.printf("%8.4f", frac);

    }
    else if (half == 'P') {
     timeSecs = ((hour * 3600) + (minute * 60) + second + (12 * 3600));
     frac = (timeSecs / 86400);
    System.out.printf("%f", frac);

    }

}

public static void main (String [] args) {
    int h = 12;
    double f12 = 0.0000;
    System.out.println("        Time     Fraction Since Midnight");
    System.out.printf("%8d:00 AM\n", h);
    for (int i = 1; i <= 11; i++) {
     System.out.printf("%8d:00 AM\n", i);
    }
    System.out.printf("%8d:00 PM\n", h);
    for (int i = 1; i <= 11; i++) {
     System.out.printf("%8d:00 PM\n", i);
    }

    System.out.printf("%8d:00 PM\n", f12);

     for (int i = 1; i <= 11; i++) {
     fractionOfDay(i, 0, 0, 'A');
    }

    for (int i = 1; i <= 11; i++) {
     fractionOfDay(i, 0, 0, 'P');
    }

}

}
4

There are 4 best solutions below

0
On

In your main method,you declared double f12 = 0.0000; it,then you invoke System.out.printf("%8d:00 PM\n", f12); which will cause the exception.

In order to solve it,you can change the type from double to int since %d in System.out.printf("%8d:00 PM\n", f12); means int

0
On
 System.out.printf("%8d:00 PM\n", f12); 

%d is used for Decimals. But your argument is of type double. Please have a look at formatting

Change %d to %s to fix this -> System.out.printf("%8s:00 PM\n", f12);

0
On
System.out.printf("%8d:00 PM\n", f12);

f12 is a double, so it can represent a non-integer number of hours. What does this mean, logically? You probably wouldn't want to print

3.141592:00 PM

Change the type of f12 to int.

0
On

You can modify code to this -

System.out.printf("%.4f:00\n", f12);

Also declare f12 as below -

float f12 = 0.0000f;