Question

JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour...

JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock like 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). Provide a default constructor, a set method which is given a String (eg, “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate (in your opinion) to the time. Please see the NOTE below concerning input validation and exception. 2. Write a client program for MyTime which loops around getting strings from the user representing times and responding with a greeting such as “good morning” as appropriate. The user should enter the string “quit” to exit the program. NOTE for 1 and 2: the set method for MyTime should detect strings which do not represent valid times. An exception should be thrown in that case. Clients (such as the one you are writing for question 2) will have to handle the exceptions. Thus you will probably have to also write your own type of exception class as well. EXAMPLE run of client: USER: 2:07 pm PROGRAM: good afternoon USER: 10:71 am PROGRAM: that’s not a valid time, please re-enter USER: 14:30am PROGRAM: that’s not a valid time, please re-enter USER: 10:21am PROGRAM: good morning

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

import java.util.Scanner;
//a client program for MyTime which loops around getting strings from the user representing times and responding with a greeting such as “good morning” as appropriate.
public class MyTime {
   int hours;
   int minutes;
   public MyTime()
   {
   }
//   a set method which is given a String (eg, “7:53am”),
   public void setHours(int hours) {
       this.hours = hours;
   }

   public void setMinutes(int minutes) {
       this.minutes = minutes;
   }
//   a getHours method,
   public int getHours() {
       return hours;
   }
//   a getMinutes method
   public int getMinutes() {
       return minutes;
   }
//   a boolean isAm method.
   public boolean isAm(){
       return false;
   }
//   a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate (in your opinion) to the time.
   public void printWish()
   {
   }

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String userInput = "";
       while(true)
       {
           System.out.print("USER: ");
           userInput = sc.nextLine();
//           enter the string “quit” to exit the program
           if(userInput.equals("quit"))
           {
               break;
           }
           System.out.print("PROGRAM: ");
           boolean result = false;
           String temp[] = userInput.split(" ");
           String temp2[] = temp[0].split(":");
           if(!(temp[1].equals("am") || temp[1].equals("pm")))
           {
               System.out.println("that’s not a valid time");
               System.out.println("please re-enter");
           }
           else if(Integer.parseInt(temp2[0])<0 || Integer.parseInt(temp2[0])>12)
           {
               System.out.println("that’s not a valid time");
               System.out.println("please re-enter");
           }
           else if(Integer.parseInt(temp2[1])<0 || Integer.parseInt(temp2[1])>60)
           {
               System.out.println("that’s not a valid time");
               System.out.println("please re-enter");
           }
           else if(userInput.contains("am"))
               System.out.println("good morning");
           else if(userInput.contains("pm"))
               System.out.println("good afternoon");
       }
   }
}

Console X <terminated> MyTime [Java Application] C:\Program Files\Java\jrel. USER: 2:07 pm PROGRAM: good afternoon USER: 10:7

Add a comment
Know the answer?
Add Answer to:
JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour...
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
  • 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock...

    1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night”...

  • What to submit: your answers to exercises 1, and 2 and separate the codes to each...

    What to submit: your answers to exercises 1, and 2 and separate the codes to each question. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a...

  • Java Store Time Clock Objects

    1. Create a Time class which is designed to contain objects representing times in 12-hour clock format. Eg: 6:58am, 11:13pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples.Provide a default constructor, a set method which is given a String (eg: “9:42am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • Here’s your task: You’re going to create exactly two classes. One class will act as the...

    Here’s your task: You’re going to create exactly two classes. One class will act as the functional server. The main method of this server will setup the connection on a localhost, find the two clients, let them know which one is Player 1 and which one is Player 2 (depending on the order they connect), and then manage the game as it’s being played. This is important: For each player’s turn they should start by displaying their current score to...

  • C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪...

    C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪ Writing a while loop ▪ Write functions and calling functions Text Processing [50 points] We would like to demonstrate our ability to control strings and use methods. There are times when a program has to search for and replace certain characters in a string with other characters. This program will look for an individual character, called the key character, inside a target string. It...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Solve it for java Question Remember: You will need to read this assignment many times to...

    Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

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
ADVERTISEMENT