Question

1. Write a program in Java to do the following a. The program should ask me...

1. Write a program in Java to do the following a. The program should ask me to input the time (hour, min, second) the hour is in the 24 hour format (example 6:00 pm is 18:00) and the city Abu Dhabi or New York) b. Given the time in one of these two cities, the program should display the time for both cities (Abu Dhabi and New York).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshots of the code:

//Import all required packages import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.Scanner; //Create class showTime public class showTime { //start main() method public static void main(String[] args) //Declare strings s1 and s2 String s1- Abu Dhabi; String s2-New York //Take user input using Scanner class System.out.println(Name the city); Scanner sc-new Scanner (System.in); String text-sc.nextLine(); System.out.println(Enter the time); DateTimeFormatter timeDateTimeFormatter.ofPattern(H:mm:ss); canner st - new Scanner (System.in) String timestring-st.next Line(); //Compare city names and print the desired output. if(text.equalsIgnoreCase(s1)) LocalTime Time1 =LocalTime . parse( timestring, time ); System.out.print(Abu Dhabi time); System.out.println(Timel); LocalTime Time2 =LocalTime . parse( timestring, time ).minusHours( 9 ); System.out.print( New York time : ); System.out.println(Time2);

Sample output:

Code to copy:

//Import all required packages

import java.time.LocalTime;

import java.time.format.DateTimeFormatter;

import java.util.Scanner;

//Create class showTime

public class showTime {

//Start main() method

public static void main(String[] args)

{

//Declare strings s1 and s2

String s1="Abu Dhabi";  

String s2="New York";  

//Take user input using Scanner class.

System.out.println("Name the city");

Scanner sc=new Scanner(System.in);

String text=sc.nextLine();

System.out.println("Enter the time");

DateTimeFormatter time = DateTimeFormatter.ofPattern("H:mm:ss");

Scanner st = new Scanner(System.in);

String timeString = st.nextLine();

//Compare city names and print the desired output.

if(text.equalsIgnoreCase(s1))

{

LocalTime Time1 =LocalTime.parse( timeString, time );

System.out.print("Abu Dhabi time : ");

System.out.println(Time1);

LocalTime Time2 =LocalTime.parse( timeString, time ).minusHours( 9 );

System.out.print("New York time : ");

System.out.println(Time2);

}

else if(text.equalsIgnoreCase(s2))

{

LocalTime Time1 =LocalTime.parse( timeString, time );

System.out.print("Abu Dhabi time is: ");

System.out.println(Time1);

LocalTime Time2 =LocalTime.parse( timeString, time ).plusHours( 9 );

System.out.print("New York time is: ");

System.out.println(Time2);

}

}

}

Add a comment
Know the answer?
Add Answer to:
1. Write a program in Java to do the following a. The program should ask me...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • in C++ Write a program that converts a time in 12-hour format to 24-hour format. The...

    in C++ Write a program that converts a time in 12-hour format to 24-hour format. The program will prompt the user to enter a time in HH:MM:SS AM/PM form. (The time must be entered exactly in this format all on one line.) It will then convert the time to 24 hour form. You may use a string type to read in the entire time at once including the space or you may choose to use separate variables for the hours,...

  • JAVA Write a complete program that prompts the user to provide a time span expressed in...

    JAVA Write a complete program that prompts the user to provide a time span expressed in seconds from keyboard, gets the input, then converts the seconds to the time expressed in days, hours, and minutes and seconds in that order, and output them in a readable format of your choice. For example, suppose the input for seconds is 3662, your program should figure out that it is equivalent to 0 day, 1 hour, 1 minute and 2 seconds. So your...

  • Write a Java program that calculates the average rainfall for three months. The program should ask...

    Write a Java program that calculates the average rainfall for three months. The program should ask the user to enter the name of each month such as June or July, and the amount of rain (in inches) that fell each month. The program should display a message similar to the following: The average rainfall for June, July, and August is 6.72 inches

  • JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work...

    JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work with “big” numbers.) Create a completely-skewed BST S containing 1, 2, . . . , x. Create a BST R containing x integers without repetitions gen- erated at random. (To minimize the risk of repetitions, you can multiply the value returned by random() by a big number.) Given that the numbers are generated uniformly at random, the tree will likely be balanced. Measure the...

  • C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it...

    C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and two for output (one for 12-hour time and another for 24-hour time). Record the AM/PM information as a value of type char, ‘A’ for AM and ‘P’ for PM. Thus, the function for doing the...

  • 1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice...

    1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the...

  • PYTHON CODING Create a program that prompts the user twice. The first prompt should ask for...

    PYTHON CODING Create a program that prompts the user twice. The first prompt should ask for the user's most challenging course at Wilmington University. The second prompt should ask for the user's second most challenging course. The red boxes indicate the input from the user. Your program should respond with two copies of an enthusiastic comment about both courses. Be sure to include the input provided by the user to display the message. There are many ways to display a...

  • Write a Java program for a fortune teller. The program should begin by asking the user...

    Write a Java program for a fortune teller. The program should begin by asking the user to enter their name. Following that the program will display a greeting. Next, the program will ask the user to enter the month (a number 1-12) and day of the month (a number 1-13) they were born. Following that the program will display their zodiac sign. Next, the program will ask the user their favorites color (the only choices should be: red, green and...

  • please use pythom Write a program that ask the user for a string as input. It...

    please use pythom Write a program that ask the user for a string as input. It should duplicate of all the characters in the string and print it back out to the user. For example: AbC123 would be printed out as AAbbCC112233 Write a program that takes two lists and displays the items that occur in both lists. For example: ["a", "b", "c"] ["c", "a", "d"] would display a and c

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT