Question

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” 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. 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 USER: 7:00 pm PROGRAM: good evening NOTE for questions 1 and 2: the set method for MyTime should detect input strings which do not represent valid times. An exception should be thrown in such a case. Clients (such as the one you are writing for question 2) will have to handle (or catch) the exceptions. Thus you will have to also write your own exception class which can be used by MyTime and the client class.

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

Client class:

public class Client {

public static void main(String[] args) {

MyTime mt = new MyTime();

try {

mt.setTime();

} catch (InvalidTime e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

MyTime class:

import java.util.Scanner;

public class MyTime {

public MyTime() {

}

private String time;

public String getTime() {

return time;

}

public void setTime() throws InvalidTime {

String time = askTime();

if (time.equalsIgnoreCase("quit")) {

System.exit(0);

} else {

try {

getHours(time);

getMinutes(time);

AmOrPM(time);

greetUser(time);

} catch (InvalidTime e) {

System.out.println("that's not a valid time, please re-enter");

setTime();

}

}

}

public static int getHours(String time) throws InvalidTime {

String[] separatedHourAndMin = time.split(":");

int hour = Integer.parseInt(separatedHourAndMin[0]);

if (hour >= 0 && hour <= 12) {

return hour;

} else {

throw new InvalidTime("that's not a valid time, please re-enter");

}

}

public static int getMinutes(String time) {

MyTime mt = new MyTime();

String[] separatedHourAndMin = time.split(":");

String[] separatedMinandAM_PM = separatedHourAndMin[1].split(" ");

int min = Integer.parseInt(separatedMinandAM_PM[0]);

if (min >= 0 && min <= 60) {

return min;

} else {

System.out.println("that's not a valid time, please re-enter");

try {

mt.setTime();

} catch (InvalidTime e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return min;

}

public static boolean isAm(String time) throws InvalidTime {

String[] separatedHourAndMin = time.split(":");

String[] separatedMinandAM_PM = separatedHourAndMin[1].split(" ");

if (separatedMinandAM_PM[1].equalsIgnoreCase("am")) {

return true;

} else {

throw new InvalidTime("that's not a valid time, please re-enter");

}

}

public String AmOrPM(String time) throws InvalidTime {

String[] separatedHourAndMin = time.split(":");

String[] separatedMinandAM_PM = separatedHourAndMin[1].split(" ");

if (separatedMinandAM_PM[1].equalsIgnoreCase("am")) {

return "am";

} else {

if (separatedMinandAM_PM[1].equalsIgnoreCase("pm")) {

return "pm";

} else {

throw new InvalidTime("that's not a valid time, please re-enter");

}

}

}

public void greetUser(String timeAmOrPM) throws InvalidTime {

MyTime mt = new MyTime();

if (getHours(timeAmOrPM) >= 6 && getHours(timeAmOrPM) < 12 && AmOrPM(timeAmOrPM).equalsIgnoreCase("pm")) {

System.out.println("good evening");

mt.setTime();

;

} else {

if (getHours(timeAmOrPM) >= 0 && getHours(timeAmOrPM) < 3 && AmOrPM(timeAmOrPM).equalsIgnoreCase("am")) {

System.out.println("good night");

mt.setTime();

} else {

if (getHours(timeAmOrPM) >= 3 && getHours(timeAmOrPM) < 11

&& AmOrPM(timeAmOrPM).equalsIgnoreCase("am")) {

System.out.println("good morning");

mt.setTime();

;

} else {

if ((getHours(timeAmOrPM) >= 12 || getHours(timeAmOrPM) < 6)

&& AmOrPM(timeAmOrPM).equalsIgnoreCase("pm")) {

System.out.println("good afternoon");

mt.setTime();

} else {

System.out.println("that's not a valid time, please re-enter");

mt.setTime();

}

}

}

}

}

public static String askTime() {

Scanner sc = new Scanner(System.in);

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

return sc.nextLine();

}

}

Exception class:

public class InvalidTime extends Exception{

InvalidTime(String s){

super(s);

}

}

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

  • 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...

  • Create a class called Login which contains a HashMap as a private attribute. It should also...

    Create a class called Login which contains a HashMap as a private attribute. It should also have an instance method called loadCredentials which acccepts the two arrays. It will load the Hash Map with key/value pairs based on the two arrays above. The userNames should be the keys and the passwords the values. private static String[] userNameArray = {"John", "Steve", "Bonnie", "Kylie", "Logan", "Robert"); private static String) passwordArray = {"1111", "2222", "3333", "4444", "5555", "6666"}; Create a login method in...

  • Within DrJava, create a class called StudentQuizScores and do the following using a do-while loop. 1....

    Within DrJava, create a class called StudentQuizScores and do the following using a do-while loop. 1. You program will read in a student’s first name. The same will be done for the student’s last name. Your program will use respective methods (described below) to accomplish this. 2. Your program will then read in three quiz scores, respectively. This should be done by using the same method three times. This method is described below. 3. Your program will then calculate the...

  • 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...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

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