Question: This is a java clock assignment. I have posted the full assignment description and the test code used to grade the assignment. Please hlep me
This is a java clock assignment. I have posted the full assignment description and the test code used to grade the assignment. Please hlep me make sure my code answers all the required things and outputs just like the test code requires. Thank you!
TEST CODE;
import java.util.Scanner;
public class ClockTest {
/* * Test 1 * Should print the following * Creating clock with hour = 0 and minute = 0 * Printing the time * 12:00 AM */ private static String test1 = "0 print q"; /* * Test 2 * Should print the following * Please enter 1 integer to create a Clock object * Creating clock with hour = 13 * Printing the time * 01:00 PM */ private static String test2 = "1 13 print q"; /* * Test 3 * Should print the following * Please enter 2 integers to create a Clock object * Creating clock with hour = 14 and minute = 6 * Printing the time * 02:06 PM */ private static String test3 = "2 14 6 print q"; /* * Test 4 * Should print the following * Please enter 2 integers to create a Clock object * Creating clock with hour = 14 and minute = 58 * Incrementing timer with 1 minute * New Time: 14 59 * hour: 14 * minute: 59
*/ private static String test4 = "2 14 58 incrementOne getHour getMinute q"; /* * Test 5 * Should print the following * Please enter 2 integers to create a Clock object * Creating clock with hour = 14 and minute = 58 * Please enter how many minutes you want to increment the timer * New Time: 15 1 * hour: 15 * minute: 1 */ private static String test5 = "2 14 58 incrementMult 3 getHour getMinute q"; /* * Test 6 * Should print the following * Please enter 2 integers to create a Clock object * Creating clock with hour = 0 and minute = 24 * Incrementing timer with 1 minute * New Time: 0 25 * Please enter 2 integers to set the time of the Clock * Invalid Input * New Time: 0 25 * Printing the time * 12:25 AM */ private static String test6 = "2 0 24 incrementOne setTime 34 12 print q"; public static void main(String[] args) { // To run one of the above tests You can replace System.in with the test variable ie: // Scanner scan = new Scanner(test1); // Be sure to only have one scanner object at a time
// Otherwise you can still use the console to enter data Scanner scan = new Scanner(test6);
Clock c1 = new Clock(); String command = ""; while (command != "q") { command = scan.next(); switch(command) { case "0": System.out.println("Creating clock with hour = 0 and minute = 0"); c1 = new Clock(); break; case "1": System.out.println("Please enter 1 integer to create a Clock object"); int hour1 = scan.nextInt(); System.out.println("Creating clock with hour = " + hour1); c1 = new Clock(hour1); break; case "2": System.out.println("Please enter 2 integers to create a Clock object"); int hour2 = scan.nextInt(), minute = scan.nextInt(); System.out.println("Creating clock with hour = " + hour2 + " and minute = "+minute); c1 = new Clock(hour2, minute); break; case "incrementMult": System.out.println("Please enter how many minutes you want to increment the timer"); c1.incrementTimer(scan.nextInt()); System.out.println("New Time: " + c1.getHour() +" "+ c1.getMinute()); break; case "incrementOne": System.out.println("Incrementing timer with 1 minute"); c1.incrementTimer(); System.out.println("New Time: " + c1.getHour() +" "+ c1.getMinute()); break; case "print": System.out.println("Printing the time"); System.out.println(c1.toString()); break; case "setTime": System.out.println("Please enter 2 integers to set the time of the Clock"); c1.setTime(scan.nextInt(), scan.nextInt()); System.out.println("New Time: " + c1.getHour() +" "+ c1.getMinute()); break; case "getHour": System.out.println("hour: " + c1.getHour()); break; case "getMinute": System.out.println("minute: " + c1.getMinute()); break; case "q": System.exit(0); break; } } scan.close(); } }
Clock.java (40 points) In this question, you are asked to design a dass that models a 24-hour Clock class. Here is the skeleton of the class: /This is a 24-hour clock so that 13:15 is1:15 PM. The class should incl ude integer variables hour and minute class Clock( public Clock ) This is the default constructor of the class. It creates a Clock object and sets the time to midnight, hour 0, minute 0. public Clock (int h) This is a one-argument constructor with parameter for the hour. The constructor always sets minute to 0 public Clock (int h, int m) This is a two-argument constructor with parameters for the hour and minute. public int getHour ) Returns the hour. public int getMinute) Returns the minute. public void incrementTimer public void incrementTimer (int x) public void setTime(int h, int m) Basic incremented adds 1 minute to the ti me. Adds x minutes to the time. (Hint: you can reuse the increment Timer method here.) Sets the time to be hour h and minute m. All values must all be range-checked to make sure they are valid. For example, 3:78 is invalid. If invalid data is entered it should print Invalid Input". Please note that both the hour and minute should be in the correct ran ge for the time to be changed public String tostring) Returns a string in the form of xx:yy PM. eg. "02:53 PM" "11:05 AM", or "12:15 AM
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
