Question
package option1.stage1; public class Time { private int hour, minute; public Time(int hour, int minute) { setHour(hour); setMinute(minute); } public Time(Time t) { setHour(t.hour); setMinute(t.minute);
package option1.stage1;
public class Time {
private int hour, minute;
public Time(int hour, int minute) {
setHour(hour);
setMinute(minute);
}
public Time(Time t) {
setHour(t.hour);
setMinute(t.minute);
}
public Time() {
setHour(0);
setMinute(0);
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
hour = hour%24;
this.hour = hour;
}
public int getMinute() {
return minute;
}
/**
*
* @param minute
*/
public void setMinute(int minute) {
if(minute >= 45)
this.minute = 45;
else if(minute >= 30)
this.minute = 30;
else if(minute >= 15)
this.minute = 15;
else
this.minute = 0;
}
public String toString() {
if(hour < 10 && minute < 10)
return "0"+hour+":0"+minute;
if(hour < 10)
return "0"+hour+":"+minute;
if(minute < 10)
return hour+":0"+minute;
return hour+":"+minute;
}
public boolean equals(Object other) {
if(other instanceof Time)
return hour == ((Time)other).hour && minute == ((Time)other).minute;
else
return false;
}
/**
*
* @param other
* @return the difference in minutes between calling object and other object.
* note that the value returned will be positive if calling object comes after
* parameter object and negative if calling object comes before parameter object.
* for example,
* if calling object represents 14:00 and parameter represents 12:30, return 90.
* if calling object represents 13:00 and parameter represents 17:30, return -270.
*/
public int diff(Time other) {
return 0; //TO BE COMPLETED
}
/**
* @param other: object against which calling object should be compared
* @return 1 if calling object is after parameter object
* -1 if calling object is before parameter object
* 0 if calling object and parameter object are at the same time
*/
public int compareTo(Time other) {
return 0; //TO BE COMPLETED
}
/**
*
* @param quarterHourCount (assumed to be non-negative).
* @return a Time object that represents the calling object shifted forward by
* quarterHourCount increments of 15 minutes.
* for example,
* if calling object represents 14:30 and quarterHourCount = 5,
* return Time object representing 15:45.
*/
public Time advance(int quarterHourCount) {
return null; //TO BE COMPLETED
}
----------------------------------------------------------------TestTime.java -------------------------------
package option1.stage1;
import common.Graded;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;
import org.junit.AfterClass;
import org.junit.Test;
import option1.stage1.*;
public class TimeTest {
private static int score = 0;
private static String result = "";
@Test @Graded(marks=5, description="Time:diff(Time)")
public void testDiff() {
Time t1 = new Time(14, 30);
Time t2 = new Time(18, 45);
Time t3 = new Time(18, 45);
Time t4 = new Time(0, 15);
Time t5 = new Time(23, 45);
assertEquals(-255, t1.diff(t2));
assertEquals(255, t2.diff(t1));
assertEquals(0, t2.diff(t3));
assertEquals(-1410, t4.diff(t5));
score+=5;
result+="Time:diff(Time) passed (5 marks) ";
}
@Test @Graded(marks=5, description="Time:compareTo(Time)")
public void testCompareTo() {
Time t1 = new Time(14, 30);
Time t2 = new Time(18, 45);
Time t3 = new Time(18, 45);
Time t4 = new Time(10, 45);
Time t5 = new Time(18, 15);
Time t6 = new Time(17, 30);
assertEquals(-1, t1.compareTo(t2));
assertEquals(1, t2.compareTo(t1));
assertEquals(0, t3.compareTo(t2));
assertEquals(1, t1.compareTo(t4));
assertEquals(-1, t1.compareTo(t5));
assertEquals(-1, t1.compareTo(t6));
assertEquals(-1, t5.compareTo(t2));
score+=5;
result+="Time:compareTo(Time) passed (5 marks) ";
}
@Test @Graded(marks=5, description="Time:advance(int)")
public void testAdvance() {
Time t1 = new Time(14, 30);
Time t2 = t1.advance(0);
assertNotNull(t2);
assertEquals(14, t2.getHour());
assertEquals(30, t2.getMinute());
t1 = new Time(14, 30);
t2 = t1.advance(2);
assertNotNull(t2);
assertEquals(15, t2.getHour());
assertEquals(00, t2.getMinute());
t1 = new Time(14, 30);
t2 = t1.advance(21);
assertNotNull(t2);
assertEquals(19, t2.getHour());
assertEquals(45, t2.getMinute());
score+=5;
result+="Time:advance(int) passed (5 marks) ";
}
//log reporting
@AfterClass
public static void wrapUp() throws IOException {
System.out.println(result.substring(0,result.length()-1));
System.out.println("--------------------------------------------");
System.out.println("Total: "+score+" out of 15");
System.out.println("-------------------------------------------- ");
String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
File file = new File("log/Time"+timeStamp+".txt");
FileWriter writer = new FileWriter(file);
writer.write(result.substring(0,result.length()-1)+" ");
writer.write("-------------------------------------------- ");
writer.write(score+" ");
writer.flush();
writer.close();
}
}
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