What to submit: your answers to exercises 1, and 2 and separate the codes to each question.
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.
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.
Assumption:
1) You are asking to write classes in Java (If any other, then
let me know in comments)
2) night (from 12:00 am to 5:59 am), morning
(from 6:00 am to 11:59 am), afternoon (from 12:00 pm to
6:00 pm), evening (from 6:00 pm to 11:59 pm)
I have made three classes
1) Main.java: class with main method for
getting input from user. (client program)
2) MyTime.java: class for time
3) InvalidTimeException.java: exception class for
Invalid time
Below are the links of code. I'm pasting code here also
Main.java --> https://pastebin.com/NB109rFN
MyTime.java --> https://pastebin.com/nH0gSNV8
InvalidTimeException --> https://pastebin.com/NZA302Ja
Main.java
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("USER: ");
String time = sc.nextLine().replaceAll("\\s","");
if(time.equals("quit")) break;
MyTime obj = new MyTime();
System.out.println("PROGRAM: ");
try{
obj.setTime(time);
System.out.println("good "+obj.getGreeting());
}
catch(Exception e){
System.out.println("that’s not a valid time, please re-enter");
}
}
}
}
MyTime.java
import java.util.*;
import java.time.*;
public class MyTime{
private String time;
private int hour;
private int minute;
private String format;
public void setTime(String time) throws InvalidTimeException{
try{
this.time = time;
int idx = 0;
while(idx<time.length() && ! Character.isLetter(time.charAt(idx))){
idx++;
}
this.format = time.substring(idx,time.length());
String[] str = time.substring(0,idx).split(":");
this.hour = Integer.parseInt(str[0]);
this.minute = Integer.parseInt(str[1]);
if(!(format.equals("am") || format.equals("pm"))){
throw new InvalidTimeException("that’s not a valid time, please re-enter");
}
else if(hour>12 || hour<1 || minute>59 || minute<0){
throw new InvalidTimeException("that’s not a valid time, please re-enter");
}
}
catch(Exception e){
throw new InvalidTimeException("that’s not a valid time, please re-enter");
}
}
public int getHours(){
return hour;
}
public int getMinutes(){
return minute;
}
public boolean isAm(){
return format.equals("am");
}
public String getGreeting(){
if (format.equals("am")){
if((hour>=1 && hour<6 && minute<=59 && minute>=0) || (hour==12 && minute<=59 && minute>0)){
return "night";
}
else return "morning";
}
else{
if((hour>=1 && hour<6 && minute<=59 && minute>=0) || (hour==12 && minute<=59 && minute>0)){
return "afternoon";
}
else return "evening";
}
}
}
InvalidTimeException.java
class InvalidTimeException extends Exception{
InvalidTimeException(String msg){
super(msg);
}
}
I hope you got your desired answer. Have a good day. :)
What to submit: your answers to exercises 1, and 2 and separate the codes to each...
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...
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”...
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...
What to submit: your answers to exercises 1, 3, and 4 and separate the codes to each question. Using your solution to question 3 of Lab Practice 2, modify it to create a class that contains a static method; the method takes a string as a parameter and returns a boolean value indicating whether the parameter string has repeated characters in it or not. That is, return true if there is at least one character which appears more than once...
What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...
Please solve in python and provide comments explaining your codes. Your output must be the same as the example and result given below in the images. This task is to write a very basic text editor. It allows you to add, delete and modify lines of text. Use a list of strings to store the lines, with each list element being one line. The elements of the list are modified according the commands given by the user. The editor repeatedly: 1. displays...
Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...
PLEASE I NEED C# Objectives Learn the basics of exception handling. Background Exceptions are essentially unexpected situations. It is difficult to write a program that can handle all possible situations, as we have found out through the many programs we have written. For example, should your program accept accidental input? Does it use the metric system or the empirical system? Do users of your program know which system it uses? In order to deal with all these possibilities, exceptions were...
Chapter 9 Lab Text Processing and Wrapper Classes Lab Objectives ? Use methods of the Character class and String class to process text ? Be able to use the StringTokenizer and StringBuffer classes Introduction In this lab we ask the user to enter a time in military time (24 hours). The program will convert and display the equivalent conventional time (12 hour with AM or PM) for each entry if it is a valid military time. An error message will...
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...