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 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. 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); } }
Write a client
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
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.
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);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images