This assignment requires two files to be named < LnFnUnit6.java > (Driver class
ID: 3690758 • Letter: T
Question
This assignment requires two files to be named <LnFnUnit6.java> (Driver class file) and <LnFnTime.java> (Time class file)
The files must be called as specified above, (LnFn = Your Last Name Your First Name)
Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
Only submit the .java files needed to make the program run. Do not submit the .classfiles or any other files.
Style Components
Include properly formatted prologue, comments, indenting, and other style elements as shown in Chapter 2 starting page 64 and Appendix 5 page 881-892.
Compilation
All programs should compile. Even if the program is not fully operational, students should comment out the sections that are not working and present a code that compiles. The commented-out sections will be an indication of the attempts the student made to complete the tasks in the assignment.
Basic Requirements
Write a program that validates time when entered. The input should be a String containing the time with the format hh:mm (hours: minutes). The program will output the time as either AM or PM format, or it will print an error if entered incorrectly. You will use two classes: The Time class and a Driver class for Time objects.
See sample output below.
LnFnUnit6.java
• Driver class should loop until “q” is entered to quit.
Do not use the break command to stop the loop.
Inside the loop:
• The entered String should be passed to a newly created instance of the LnFnTime object (the LnFnTime constructor will receive this String as argument).
• After creating the LnFnTime object in the previous step, the main program will call the print method on this object.
LnFnTime.java
Time objects should store the time in 2 integer instance variables, hour and minute, and include a string variable to hold an error message. This should be initialized with null.
Accessor and mutator methods should be created and used in the program whenever the instances variables are required.
LnFnTime Constructor:
• Receive time as a string
• Perform error checking to ensure time was entered in proper format to include a colon (:) between the digits
• Use indexOf and substring to separate the time string into the appropriate instance variables
• Check hour to ensure is between 1 and 23
• Check minute to ensure is between 0 and 59
• If an error occurs, change the error instance variable to reflect the error (see sample)
print method:
• If the error attribute is null, then, this method prints the date using printf to output time in digits for the hour and 2 digits for the minutes Be sure to print AM if before noon and PM if after noon. Do NOT print in military time
• If the error attribute is not null, this method will print the error message.
NOTE: Complete your activity and submit it by clicking “Submit Assignment”
Sample
Your output will vary based on the random numbers generated.
Sample session (requires no user input):
Enter time in the form hh:mm ("q" to quit): 5:20
05:20 AM
Enter time in the form hh:mm ("q" to quit): 05:20
05:20 AM
Enter time in the form hh:mm ("q" to quit): 05:80
Invalid minutes entered: 80
Enter time in the form hh:mm ("q" to quit): 15:20
03:20 PM
Enter time in the form hh:mm ("q" to quit): 25:20
Invalid hours entered: 25
Enter time in the form hh:mm ("q" to quit): 0520
Invalid or No separator entered
Enter time in the form hh:mm ("q" to quit): aa:20
Invalid input for time.
Enter time in the form hh:mm ("q" to quit): q
This assignment requires two files to be named <LnFnUnit6.java> (Driver class file) and <LnFnTime.java> (Time class file)
The files must be called as specified above, (LnFn = Your Last Name Your First Name)
Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
Only submit the .java files needed to make the program run. Do not submit the .classfiles or any other files.
Style Components
Include properly formatted prologue, comments, indenting, and other style elements as shown in Chapter 2 starting page 64 and Appendix 5 page 881-892.
Compilation
All programs should compile. Even if the program is not fully operational, students should comment out the sections that are not working and present a code that compiles. The commented-out sections will be an indication of the attempts the student made to complete the tasks in the assignment.
Basic Requirements
Write a program that validates time when entered. The input should be a String containing the time with the format hh:mm (hours: minutes). The program will output the time as either AM or PM format, or it will print an error if entered incorrectly. You will use two classes: The Time class and a Driver class for Time objects.
See sample output below.
LnFnUnit6.java
• Driver class should loop until “q” is entered to quit.
Do not use the break command to stop the loop.
Inside the loop:
• The entered String should be passed to a newly created instance of the LnFnTime object (the LnFnTime constructor will receive this String as argument).
• After creating the LnFnTime object in the previous step, the main program will call the print method on this object.
LnFnTime.java
Time objects should store the time in 2 integer instance variables, hour and minute, and include a string variable to hold an error message. This should be initialized with null.
Accessor and mutator methods should be created and used in the program whenever the instances variables are required.
LnFnTime Constructor:
• Receive time as a string
• Perform error checking to ensure time was entered in proper format to include a colon (:) between the digits
• Use indexOf and substring to separate the time string into the appropriate instance variables
• Check hour to ensure is between 1 and 23
• Check minute to ensure is between 0 and 59
• If an error occurs, change the error instance variable to reflect the error (see sample)
print method:
• If the error attribute is null, then, this method prints the date using printf to output time in digits for the hour and 2 digits for the minutes Be sure to print AM if before noon and PM if after noon. Do NOT print in military time
• If the error attribute is not null, this method will print the error message.
NOTE: Complete your activity and submit it by clicking “Submit Assignment”
Sample
Your output will vary based on the random numbers generated.
Sample session (requires no user input):
Enter time in the form hh:mm ("q" to quit): 5:20
05:20 AM
Enter time in the form hh:mm ("q" to quit): 05:20
05:20 AM
Enter time in the form hh:mm ("q" to quit): 05:80
Invalid minutes entered: 80
Enter time in the form hh:mm ("q" to quit): 15:20
03:20 PM
Enter time in the form hh:mm ("q" to quit): 25:20
Invalid hours entered: 25
Enter time in the form hh:mm ("q" to quit): 0520
Invalid or No separator entered
Enter time in the form hh:mm ("q" to quit): aa:20
Invalid input for time.
Enter time in the form hh:mm ("q" to quit): q
Explanation / Answer
Hello there ,
Kindly find below code and it's o/p.
Let me know if you have any queries in this.
Thanks.
/**
*
* It represents the Time class having hour,minute and error message if time is
* not given in valid format or valid.
*
* @author dipal.prajapati
*
*/
public class PrajapatiDipalTime {
private int hour;
private int minute;
private String error;
public PrajapatiDipalTime(String time) {
int iHour = 0, iMinute = 0;
int separatorIndex = time.indexOf(":");
if (separatorIndex == -1) {
error = "Invalid or No separator entered ";
return;
} else {
try {
iHour = Integer.parseInt(time.substring(0, separatorIndex));
iMinute = Integer.parseInt(time.substring(separatorIndex + 1, separatorIndex + 3));
} catch (Exception e) {
error = "Invalid input for time.";
return;
}
}
if (iHour > 23 | iHour < 1) {
error = "Invalid hours entered: " + iHour;
} else if (iMinute > 59 | iMinute < 0) {
error = "Invalid minutes entered: " + iMinute;
} else {
this.hour = iHour;
this.minute = iMinute;
}
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
/**
* It pritns the hour and minute in the given format if valid or prints the
* error message.
*/
public void print() {
if (error == null) {
if (this.hour != 0 && this.minute != 0) {
if (hour < 12)
System.out.println(hour + ":" + minute + " AM");
else
System.out.println((hour - 12) + ":" + minute + " PM");
}
} else {
System.out.println(error);
}
}
}
import java.util.Scanner;
/**
* Driver class
*
* @author dipal.prajapati
*
*/
public class PrajapatiDipalUnit6 {
static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
String strTime;
System.out.println("Enter time in the form hh:mm ("q" to quit): ");
while (!(strTime = scanner.next()).equals("q")) {
PrajapatiDipalTime time = new PrajapatiDipalTime(strTime);
time.print();
System.out.println("Enter time in the form hh:mm ("q" to quit): ");
}
}
}
====O/P===
Enter time in the form hh:mm ("q" to quit):
5:20
5:20 AM
Enter time in the form hh:mm ("q" to quit):
05:20
5:20 AM
Enter time in the form hh:mm ("q" to quit):
05:80
Invalid minutes entered: 80
Enter time in the form hh:mm ("q" to quit):
15:20
3:20 PM
Enter time in the form hh:mm ("q" to quit):
25:20
Invalid hours entered: 25
Enter time in the form hh:mm ("q" to quit):
0520
Invalid or No separator entered
Enter time in the form hh:mm ("q" to quit):
aa:20
Invalid input for time.
Enter time in the form hh:mm ("q" to quit):
22:56
10:56 PM
Enter time in the form hh:mm ("q" to quit):
q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.