Question

In: Computer Science

Create an ElapsedTimeCalculator application that prompts the user for a starting hour, whether it is am...

Create an ElapsedTimeCalculator application that prompts the user for a starting hour, whether it is am or pm, and the number of elapsed hours. The application then displays the time after that many hours have passed. (Java)

Solutions

Expert Solution

/*********************************ElapsedTimeCalculator.java*********************************/

import java.util.Scanner;

public class ElapsedTimeCalculator {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the starting hour: ");
       int startHour = scan.nextInt();
       System.out.println("AM or PM?");
       String amOrPm = scan.next();
       System.out.println("How many hour elapsed: ");
       int elapsedHour = scan.nextInt();
       if (elapsedHour>12&&elapsedHour<24&&amOrPm.equalsIgnoreCase("AM")) {
          
           amOrPm = "PM";
           elapsedHour = elapsedHour%12;
       }
       else if (elapsedHour>12&&elapsedHour<24&&amOrPm.equalsIgnoreCase("PM")) {
          
           amOrPm = "AM";
           elapsedHour = elapsedHour%12;
       }
       else if (elapsedHour>24) {
      
           elapsedHour = elapsedHour%12;
       }

       //condition check for valid time
       if (startHour / 12 <= 1 && amOrPm.equalsIgnoreCase("AM")) {
           //condition if total time is greater than 12
           if ((startHour + elapsedHour) / 12 < 1 && startHour != 12) {

               System.out.println("Time is: " + (startHour + elapsedHour) + "AM");
           } else if (startHour == 12 && (startHour + elapsedHour) / 12 < 2) {
               System.out.println("Time is: " + (elapsedHour) + "AM");
           } else {
               System.out.println("Time is: " + Math.abs((12 - (elapsedHour + startHour))) + "PM");
           }
       } else if (startHour / 12 <= 1 && amOrPm.equalsIgnoreCase("PM")) {

           if ((startHour + elapsedHour) / 12 < 1 && startHour != 12) {

               System.out.println("Time is: " + (startHour + elapsedHour) + "PM");
           } else if (startHour == 12 && (startHour + elapsedHour) / 12 < 2) {
               System.out.println("Time is: " + (elapsedHour) + "PM");
           } else {

               System.out.println("Time is: " + Math.abs((12 - (startHour + elapsedHour))) + "AM");
           }

       }

       else {

           System.out.println("Please enter valid time");
       }

   }
}

/****************************ouput**************************/

Enter the starting hour:
12
AM or PM?
PM
How many hour elapsed:
10
Time is: 10PM

Enter the starting hour:
1
AM or PM?
AM
How many hour elapsed:
26
Time is: 3AM

Thanks a lot, Please let me know if you have any problem,,


Related Solutions

Create a CubesSum application that prompts the user for a non-negative integer and then displays the...
Create a CubesSum application that prompts the user for a non-negative integer and then displays the sum of the cubes of the digits.   b) Modify the application to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits.(Java Programming )
C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression,...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a...
Create a class (Shapes) that prompts the user to select a shape to be drawn on...
Create a class (Shapes) that prompts the user to select a shape to be drawn on the screen. Objects: Circle, X, box, box with an x inside, or any other object of your choice. Depending on the user’s choice, you will then ask for the number of rows, columns or any requirements needed to draw the shape. Finally, you will display the shape selected by the user to the screen. Your class should have at least one instance variable. Your...
Write an application named MultiplicationTable that prompts the user for an integer value, for example 7....
Write an application named MultiplicationTable that prompts the user for an integer value, for example 7. Then display the product of every integer from 1 through 10 when multiplied by the entered value. For example, the first three lines of the table might read 1×7=7 , 2×7=14 , and 3×7=21 using a Loop other than While if
create a Python script that prompts the user for a title, description, and filename of your...
create a Python script that prompts the user for a title, description, and filename of your Python program and add the following to the bottom of the existing homepage: Add the post title with an emphasis Add the post description beneath the post title Create a hyperlink to the Python file using the filename input Create another hyperlink to the page you will create in the Web Showcase assignment
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Create an application that makes the user guess a number. The user must be allowed tries....
Create an application that makes the user guess a number. The user must be allowed tries. You must have a loop, user input (Scanner), and a constructor. (JAVA)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT