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...
programming language is c#. Create a method that prompts a user of your console application to...
programming language is c#. Create a method that prompts a user of your console application to input the information for a student: static void GetStudentInfo() { Console.WriteLine("Enter the student's first name: "); string firstName = Console.ReadLine(); Console.WriteLine("Enter the student's last name"); string lastName = Console.ReadLine(); // Code to finish getting the rest of the student data ..... } static void PrintStudentDetails(string first, string last, string birthday) { Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday); } 1. Using the...
Create an C# application named ConvertMilesToKilometers whose Main() method prompts a user for a number of...
Create an C# application named ConvertMilesToKilometers whose Main() method prompts a user for a number of miles, passes the value to a method that converts the value to kilometers, and then returns the value to the Main() method where it is displayed.
Write a Java application that prompts the user for an age. If the age entered is...
Write a Java application that prompts the user for an age. If the age entered is greater or equal to 65, display the statement "Age is greater than or equal to 65"; otherwise display the message "Age is less than 65". If the age entered is less than 18; display the statement "This person is a minor"; otherwise display the message "This person can legally vote. Do not create a class for this application. The code can be created in...
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
Write a Java program to do the following: Specifics: Write an application that prompts a user...
Write a Java program to do the following: Specifics: Write an application that prompts a user for two integers and displays every integer between them. Display a message if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger. Save the file as InBetween.java. Don’t forget to create the application/project  InBetweenTest.java Class that has the main method and an object to use the InBetween class.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT