Question

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: " + );
}
Was it helpful?

Solution

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.

OTHER TIPS

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 ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top