0

I'm doing a homework assignment in my Java class. I got most of it except for this one part about elapsed time. We have to use methods. Here's the assignment and the code I have.

"You have just been hired by a company to do it’s weekly payroll. One of the functions you must perform daily is to check the employee time cards and compute the elapsed time between the time they “punch in” and “punch out”. You also have to sometimes convert hours to minutes, days to hours, minutes to hours and hours to days. Since you’ve just finished your first programming class you decide to write a program that will help you do your job.

You decide to structure your program the following way. The main function will just be a menu that the user can select from to get the information they want. Each option on the menu will call a specific method(s) to solve the task and/or output the answer.

You may assume for this program that all elapsed times will be in a single day but the others may span much further. Be sure to provide sufficient test data to demonstrate that your solutions are correct. (show at least one output for each conversion [probably several for option #5]). "

import java.util.*;
public class Prog671A
{
    public static void hoursToMinutes()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter hour(s): ");
        int hours = input.nextInt();
        System.out.println(hours + " * 60 = " + (hours * 60) + " minutes.");
        System.out.println("");
    }
    
    public static void daysToHours()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter day(s): ");
        int days = input.nextInt();
        System.out.println(days + " * 24 = " + (days * 24) + " hours.");
        System.out.println("");
    }
    public static void minutesToHours()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter minute(s): ");
        int minutes = input.nextInt();
        System.out.println(minutes + " / 60 = " + ((double)minutes / 60) + " hours.");
        System.out.println("");
    }
    public static void hoursToDays()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter hour(s): ");
        int hours = input.nextInt();
        System.out.println(hours + " / 24 = " + ((double)hours / 24) + " days.");
        System.out.println("");
    }
    public static void elapsedTime()
    {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter the beginning hour: ");
        int startingHour = input.nextInt();
        System.out.print("Enter the beginning minute(s): ");
        int startingMinutes = input.nextInt();
        System.out.print("Enter AM/PM: ");
        String startingAmOrPm = input.nextLine();
        System.out.print("Enter the ending hour: ");
        int endingHour = input.nextInt();
        System.out.print("Enter the ending minute(s): ");
        int endingMinutes = input.nextInt();
        System.out.print("Enter AM/PM: ");
        String endingAmOrPm = input.nextLine();
        System.out.println("");
        System.out.println("The elapsed time is: " + );
    }
    public static void main (String args [])
    {
        int x = 1;
        while (x == 1) {
        Scanner input = new Scanner (System.in);
        System.out.println("Conversion Tasks");
        System.out.println("\t1. Hours -> Minutes");
        System.out.println("\t2. Days -> Hours");
        System.out.println("\t3. Minutes -> Hours");
        System.out.println("\t4. Hours -> Days");
        System.out.println("\t5. Elapsed time between two times");
        System.out.println("\t6. Exit");
        System.out.print("Enter a number: ");
        int menu = input.nextInt();
        System.out.println("");
        if (menu == 1)
            hoursToMinutes();
        if (menu == 2)
            daysToHours();
        if (menu == 3)
            minutesToHours();
        if (menu == 4)
            hoursToDays();
        if (menu == 5)
            elapsedTime();
        if (menu == 6)
            x = 0;
        }
    }
}

I just need help here

public static void elapsedTime()
{
    Scanner input = new Scanner (System.in);
    System.out.print("Enter the beginning hour: ");
    int startingHour = input.nextInt();
    System.out.print("Enter the beginning minute(s): ");
    int startingMinutes = input.nextInt();
    System.out.print("Enter AM/PM: ");
    String startingAmOrPm = input.nextLine();
    System.out.print("Enter the ending hour: ");
    int endingHour = input.nextInt();
    System.out.print("Enter the ending minute(s): ");
    int endingMinutes = input.nextInt();
    System.out.print("Enter AM/PM: ");
    String endingAmOrPm = input.nextLine();
    System.out.println("");
    System.out.println("The elapsed time is: " + );
}
Community
  • 1
  • 1
Garrett Outlaw
  • 215
  • 3
  • 6
  • 16
  • 3
    Are you looking to subtract the starting and ending times and then print the result? – hologram Dec 26 '12 at 21:05
  • Why are you having them enter hour and minutes separately? And if you want to compute elapsed time, it will make your life easier to just convert all values to 24 hour time (e.g. add 12 to anything greater than 12 p.m.) – Max Dec 26 '12 at 21:09
  • I think so. The teacher wasn't very specific. He only gave us this as an example output. Elapsed Time Conversion Enter the beginning hour: 8 Enter the beginning minutes: 14 Enter am/pm: am Enter the ending hour: 2 Enter the ending minutes: 47 Enter am/pm: pm The elapsed time is: ? hrs ?? min – Garrett Outlaw Dec 26 '12 at 21:09
  • The "normal" way to subtract times is to convert everything to seconds, subtract and convert back. Of course you _can_ compute in 60-ary, but then you'll have to carry manually. – John Dvorak Dec 26 '12 at 21:12
  • To get the elapse time you need two dates, not one. The simplest appoarch is to read one date in a single line and another date in a single line and parse them with SimpleDateFormat and take the difference. – Peter Lawrey Dec 26 '12 at 21:12
  • @PeterLawrey He stated it was homework. I believe his teacher is attempting to have them solve the issue logically, not using the date libraries. – Max Dec 26 '12 at 21:15
  • 1
    @Max You could be right. In which case he can convert to the smallest unit (minutes) and take the difference. – Peter Lawrey Dec 26 '12 at 21:16
  • @GarrettOutlaw I have one question. How you going to manage AM and PM timing rollover? – Smit Dec 26 '12 at 21:31
  • @smit - From the assignment: _"You may assume for this program that all elapsed times will be in a single day"_. – Ted Hopp Dec 26 '12 at 21:46

2 Answers2

3

Just convert the start and end times to minutes since the start of the day (at midnight). For p.m., just add 12 to the hour before converting to minutes. Then subtract and convert back to hours and minutes for the elapsed time.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
-2

why do you want to take the hours etc stuff manually?? Read the system date when the User logs in System.currentTimeMillis() store this time somewhere

When the User exists do the same get the System Time. Subtract it from the time you stored initially when the user logged in.

I hope this is what ur looking for ?

Shalabh
  • 687
  • 1
  • 6
  • 11
  • 1
    That's not consistent with the assignment. – Ted Hopp Dec 26 '12 at 21:14
  • 2
    This is homework. Where user will enter some numbers. What you are telling him is completely different. – Smit Dec 26 '12 at 21:14
  • 1
    That's how we were told to do it. Also, that's not exactly what I was looking for. It's not asking for the elapsed time for a user login. It's just elapsed time between two different hours that you type in. – Garrett Outlaw Dec 26 '12 at 21:16