I am unable to get this to compile. Struggling through the text but below these
ID: 3767273 • Letter: I
Question
I am unable to get this to compile. Struggling through the text but below these requirements is what I have thus far. Any assistance would be appreciated.
There are 2 programs that need to be named LIFIUnit6Ch15 and LIFITime.
The topics are Exceptions and Exception Messages, try/catch, checked and unchecked exceptions, generic catch block, and throws.
Need to write a program that validates time when entered. It should ouput the time as either AM or PM or the rror if entered incorrectly.
So for LIFIUnit6Ch15, Driver class should loop until "q" is entered to quit, If enter is no "q". then create an instance of Time object passing the entery as an argument, if no error print time by calling print method of LIFITime class, if error printer error message "invalid minute entered, invalid hour entered, invalid seperator entered.
For my second program LIFITime, TIme objects should stoe the time in 2 integer variables, hour and minute, and include a string variable to hold the error. This should be intialized with null. LIFITIME constrcuting will be recieve time as string, perform error checking to ensure time was entered in proper format to inclode a color (:) between digits, Use indexOf and substring to seperate the time string into the appropriate instance variables, check hour to ensure is between 0 and 23, check minute to ensure is between 0 and 59, if an erro occures, change the erro instance variable to reflect the error ( "invalid minute entered, invalid hour entered, invalid seperator entered.)
print method: include a sperate method to pring the date using printf to output time in digits for the hour and 2 digits for the minutes. Print AM if before noon and PM if after nonn. Do NOT pring in military time.
getError method: include a getError method that returns the rror instance variable to the print method.
My LiFiTime class
import java.util.Scanner;
public class LiFiTime //class
{
private int hour;
private int minute;
private String ampm = null;
private String error = null;
public LiFiTime(String time)
{
String hr = time.substring(0,time.indexOf(":"));
String min = time.substring(time.indexOf(":")+1);
this.hour = Integer.parseInt(hr);
this.minute = Integer.parseInt(min);
try (hour > 23 || hour < 0)
{
if (minute > 59 || minute < 0)
{
error = "Invalid hour and minute is entered"; //setting invalid user input instances
}
else
{
error = "Invalid hour is entered";
}
}
else if (minute > 59 || minute < 0)
{
error = "Invalid minute is entered";
}
if (hour > 11)
{
ampm = "PM";
} else
{
ampm = "AM";
}
print();
} catch (ArrayIndexOutOfBoundsException e) //exceptions set
{
error = "Invalid seperator";
} catch (StringIndexOutOfBoundsException e) {
error = "Invalid seperator";
} catch (NumberFormatException e) {
error = "Please Enter numeric value in time";
}
}
public void print()
{
if (error == null)
{
System.out.format("%02d:%02d %s%n", hour, minute, ampm);
}else
{
System.out.println(error);
}
}
public String getError()
{
return error;
}
//ending main/class
}
My driver class
import java.util.Scanner;
public class LiFi6Ch15
{
public static void main(String[] args)
{
String time;
Scanner scan=new Scanner(System.in);
System.out.print("Enter time in the form mm:dd ("q" to quit}");
timer = scan.next();
while (!time.equalsIgnoreCase("q")
)
{
System.out.print("Enter time in the form mm:dd ("q" to quit}");
FCTime timer = new FCTime(time);
timer = scan.next();
}
}
}
Once again, any assistance would be appreciated with appropriate ratings.
Thanks
Explanation / Answer
Driver Class
LiFi6Ch15.java
package com.test;
import java.util.Scanner;
/**
* Class to get time in format (Example: 12:30 PM)
*
* @author Your Name
*
*/
public class LiFi6Ch15 {
public static void main(String[] args) {
String time;
Scanner scan = new Scanner(System.in);
System.out.print("Enter time in the form mm:dd ("q" to quit}");
time = scan.next();
// Continue Until presses q
while (!time.equalsIgnoreCase("q")) {
LiFiTime timer = new LiFiTime(time);
System.out.print("Enter time in the form mm:dd ("q" to quit}");
time = scan.next();
}
// Closing Scanner
scan.close();
}
}//End of Class
LiFiTime class.java
package com.test;
/**
* Class to do validation for given time
*
* @author Your Name
*
*/
public class LiFiTime {
private int hour;
private int minute;
private String ampm = null;
private String error = null;
public LiFiTime(String time) {
try {
String hr = time.substring(0, time.indexOf(":"));
String min = time.substring(time.indexOf(":") + 1);
this.hour = Integer.parseInt(hr);
this.minute = Integer.parseInt(min);
if (hour > 23 || hour < 0) {
if (minute > 59 || minute < 0) {
// setting invalid user input instances
error = "Invalid hour and minute is entered";
} else {
error = "Invalid hour is entered";
}
} else if (minute > 59 || minute < 0) {
error = "Invalid minute is entered";
}
if (hour > 11) {
ampm = "PM";
} else {
ampm = "AM";
}
print();
} catch (ArrayIndexOutOfBoundsException e) // exceptions set
{
error = "Invalid seperator";
print();
} catch (StringIndexOutOfBoundsException e) {
error = "Invalid seperator";
print();
} catch (NumberFormatException e) {
error = "Please Enter numeric value in time";
print();
}
}
/**
* Method to print result
*/
public void print() {
if (getError() == null) {
System.out.format("%02d:%02d %s%n", hour, minute, ampm);
} else {
System.out.println(getError());
}
}
/**
* Method to get error value
*
* @return error
*/
public String getError() {
return error;
}
}//End of class
OutPut:
Enter time in the form mm:dd ("q" to quit}123:23
Invalid hour is entered
Enter time in the form mm:dd ("q" to quit}12:234
Invalid minute is entered
Enter time in the form mm:dd ("q" to quit}12:23
12:23 PM
Enter time in the form mm:dd ("q" to quit}11:30
11:30 AM
Enter time in the form mm:dd ("q" to quit}1234
Invalid seperator
Enter time in the form mm:dd ("q" to quit}26:67
Invalid hour and minute is entered
Enter time in the form mm:dd ("q" to quit}qw:wer
Please Enter numeric value in time
Enter time in the form mm:dd ("q" to quit}q
Note:
Please comment if you have any doubts.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.