Question
Need to Create a Time.java and TimeTest.java Class. The Time object represents a particular time of day such as 12:30 PM or 9:47 AM. You
Need to Create a Time.java and TimeTest.java Class.
The Time object represents a particular time of day such as 12:30 PM or 9:47 AM. You must ensure that all Time objects always have valid state as described in this document. Each Time object should have the following public behavior.
If any method or constructor is passed a parameter whose value is invalid, you should throw an IllegalArgumentException. In particular, you must enforce the following Time values should have valid numbers of hours (1-12) and minutes (0-59). For example, calling shift(120) on 11:30 AM should result in 01:30 PM. (NOT 11:150 AM!)
Here is my JUnit class, TimeInstructorTest.java
import org.junit.Assert; import org.junit.Test; import java.util.*; public class TimeInstructorTest { private static void assertHelper(int h, int m, boolean b, Time t){ Assert.assertEquals("hour should match", h, t.getHour()); Assert.assertEquals("minute should match", m, t.getMinute()); Assert.assertEquals("isPM should match", b, t.isPM()); } @Test public void constructorTest(){ //Given Time t = new Time(12, 59, true); Time t2 = new Time(1, 0, false); //When/Then assertHelper(12, 59, true, t); assertHelper(1, 0, false, t2); } private void constNegativeHelper(int h, int m, boolean b){ try { Time t = new Time(h, m, b); Assert.fail("hour "+ h + " and/or minute " + m + "isPM "+ b + " is invalid"); }catch(IllegalArgumentException e){ } } @Test public void constNegativeTest(){ constNegativeHelper(0,59, true); //00:59 PM is invalid constNegativeHelper(13,59, true); //13:59 PM is invalid constNegativeHelper(-5,59, false); //-5:59 AM is invalid constNegativeHelper(11,60, false); //11:60 AM is invalid constNegativeHelper(11,-1, false); //11:-1 AM is invalid } @Test public void fromStringTest(){ Time t = Time.fromString("02:59 PM"); assertHelper(2, 59, true, t); Time t2 = Time.fromString("12:59 AM"); assertHelper(12, 59, false, t2); Time t3 = Time.fromString("05:05 PM"); assertHelper(5, 5, true, t3); } @Test public void toStringTest() { Time t = new Time(3, 1, false); Assert.assertEquals("03:01 AM", t.toString()); Time t2 = new Time(12, 20, false); Assert.assertEquals("12:20 AM", t2.toString()); Time t3 = new Time(6, 0, true); Assert.assertEquals("06:00 PM", t3.toString()); } @Test public void equalsTest(){ Time t = new Time(5, 0, false); Time t2 = new Time(5, 0, false); Time t3 = t; Assert.assertTrue(t.equals(t2)); Assert.assertTrue(t2.equals(t)); Assert.assertTrue(t.equals(t3)); Assert.assertEquals(t.hashCode(), t2.hashCode()); Assert.assertEquals(t3.hashCode(), t.hashCode()); } private void fromStringTestHelper(String str){ try { Time.fromString(str); Assert.fail("str is invalid format"); } catch (IllegalArgumentException e) { } } @Test public void fromStringNegativeTest(){ fromStringTestHelper("hello world"); fromStringTestHelper("when is thanks-giving?"); fromStringTestHelper("5:03 PM"); fromStringTestHelper("05:03 pM"); fromStringTestHelper("05;06 AM"); fromStringTestHelper("05:065AM"); fromStringTestHelper("05:05 XX"); fromStringTestHelper("13:59 PM"); fromStringTestHelper("12:60 AM"); } @Test public void equalsNegativeTest(){ Time t = new Time(5, 0, false); Time t2 = new Time(5, 0, true); Assert.assertFalse(t.equals(t2)); Assert.assertFalse(t2.equals(t)); } @Test public void hashCodeTest(){ Time t = new Time(5, 0, false); t.shift(10); Time t2 = new Time(5, 10, false); Assert.assertTrue(t.equals(t2)); Assert.assertTrue(t2.equals(t)); Assert.assertEquals(t.hashCode(), t2.hashCode()); } @Test public void hashSetTest(){ Set
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started