Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview The goal of this assignment is to practice using Java classes, methods, and objects, including instance variables and overloading and overriding methods. Use the

Overview The goal of this assignment is to practice using Java classes, methods, and objects, including instance variables and overloading and overriding methods. Use the provided Eclipse project as your starting point. You must implement all stubbed methods in Time.java and TimeUI.java; do not modify TimeDMRTests.java (my JUnit tests) or any other provided classes or data unless directed to do so.

Part 1: class Time Consider a class Time that represents a time of day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Complete the provided Time.java: a. Implement three constructors: A default constructor that initializes the time to 0 hours, 0 minutes (aka midnight). The other two constructors are analogous to the setTime() methods described in Parts c and d. Hint: The 2- and 3-arg constructors utilize the no-arg constructor and setTime() to do their work. b. Implement method isValid( hour, minute ) that returns true if the given hour and minute values are in the appropriate range. c. Implement a method setTime( hour, minute ) that sets the time only if the given values are valid. Valid hour value ranges from 0 to 23. Valid minute value ranges from 0 to 59. d. Implement another method setTime( hour, minute, isAm ) that sets the time only if the given values are valid. The given hour should be in the range 1 to 12. The parameter isAm is true if the time is before noon and false otherwise. e. Implement method getTime24() that returns a string in 24-hour notation hhmm. For example, if this.hour is 7 and this.minute is 25, return "0725". If this.hour is 0 and this.minute is 5, return "0005". If this.hour is 15 and this.minute is 30, return "1530". You must zero-fill the hour you may not prepend the character 0 to the hour if it is less than 10. Hint: Use String.format(). f. Implement method getTime12() that returns a string in 12-hour notation h:mm xx. For example, if this.hour is 7 and this.minute is 25, return "7:25 am". If this.hour is 0 and this.minute is 5, return "12:05 am". If this.hour is 15 and this.minute is 30, return "3:30 pm". 12-hour notation does not zero-fill the hour if its one digit. Note: am and pm must be lowercase and separated from the minutes by a single space character. Hint: Use String.format(). g. Implement methods equals() and toString() overriding Objects versions. toString() must produce the text (hh, mm) where hh and mm represent the instances hours and minutes as 2-digit numbers with leading 0-fill; include the parentheses, comma, and a single space after the comma. h. Implement method compareTo() Time implements Comparable< Time >. Notes: You may not use any Java library classes (Date, Time, etc.) to manipulate the time. You should use the String.format() method for formatting the text in getTime12(), getTime24(), and toString(). You must use exactly 2 instance variables (int hours; int minutes) you may not have an instance variable to indicate am/pm you always store time in 24-hour format! Part 2: class TimeUI Write an interactive program (separate class TimeUI) to use your Time class. Your program must prompt for 12- or 24-hour input format or to quit. For 12-hour format, prompt for hours, minutes, and am or pm, then invoke the 3-parameter constructor or setTime(). For 24-hour format, prompt for hours and minutes, then invoke the 2-parameter constructor or setTime(). Regardless of input format, display the time in both 12- and 24-hour formats. Repeat. You must use a switch() statement as the dispatcher to handle the first prompt (12-hour, 24-hour, quit) its ok to only check the first character (1, 2, q).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain what is meant by the terms unitarism and pluralism.

Answered: 1 week ago