Create a JUNIT test case for this function: /** * Test method for {@link edu.odu
ID: 3795639 • Letter: C
Question
Create a JUNIT test case for this function:
/**
* Test method for {@link edu.odu.cs.cs350.Duration#setTotalSeconds(long)}.
*/
@Test
public final void testSetTotalSeconds() {
fail("Not yet implemented"); // TODO
}
Based off this class definition:
package edu.odu.cs.cs350;
import java.util.Arrays;
/**
* A duration represents a period of elapsed time, e.g., 4 hours, 23 minutes, 2 seconds.
* This is differentiated from a point in time (e.g., 4:23:02AM). A meeting that
* has a duration of 2 hours has that same duration no matter where it was held. A
* starting time (point) for that of 2:00PM EST can be unambiguous only if the time
* zone is added.
*
* <p>
* Because durations are often used in calculations, both positive and
* negative values are possible.
*
* <p>
* Most of the accessor functions for this class will respond with normalized
* values, where the normalization rules are as follows:
* <ul>
* <li>The seconds and minutes components will have an absolute
* value in the range 0..59, inclusive.</li>
* <li>The hours component will have an absolute value in the
* range 0..23, inclusive.</li>
* <li>The sign of each component matches the sign of the
* overall duration. A duration of -61 seconds, for example,
* has normalized components of -1 seconds and -1 minutes.</li>
* </ul>
* Inputs to the member functions are not, however, required to
* be normalized. new Duration(0,0,3,-61) and new Duration(0,0,1,59)
* are both acceptable (and the resulting Duration objects are equal).
*
* @author zeil
*
*/
public class Duration implements Cloneable {
/**
* Construct a new duration, equivalent to
* Duration(0,0,0,0).
*/
public Duration() {
//ToDo
}
/**
* Create a new duration.
*
* @param totalSeconds total number of seconds in duration
*/
public Duration(long totalSeconds) {
//ToDo
}
/**
* Create a new duration.
*
* @param days number of days in duration
* @param hours number of hours in duration
* @param minutes number of minutes in duration
* @param seconds number of seconds in duration
*/
public Duration(int days, int hours, int minutes, int seconds) {
//ToDo
}
/**
* Get the total seconds of this duration, including the contributions
* of the days, hours, minutes, & seconds components.
*
* @return the total seconds
*/
public long getTotalSeconds() {
//ToDo
return 0;
}
/**
* Set the total seconds of this duration, potentially altering
* the days, hours, minutes, & seconds components.
* @param totalSeconds the total seconds to set
*/
public void setTotalSeconds(long totalSeconds) {
//ToDo
}
/**
* How many days in this duration?.
*
* @return the normalized days component
*/
public int getDays() {
//ToDo
return 0;
}
private static final long secondsPerHour = 60 * 60;
/**
* How many hours in this duration?.
*
* @return the normalized hours component
*/
public int getHours() {
//ToDo
return 0;
}
/**
* How many minutes in this duration?.
*
* @return the normalized minutes component
*/
public int getMinutes() {
//ToDo
return 0;
}
/**
* How many seconds in this duration?.
*
* @return the normalized seconds component
*/
public int getSeconds() {
//ToDo
return 0;
}
/**
* Add another duration to this one.
* @param dur a duration
*/
public void add(Duration dur) {
//ToDo
}
/**
* Subtract another duration from this one.
* @param dur a duration
*/
public void subtract(Duration dur) {
//ToDo
}
/**
* Multiply this duration by a scaling factor,
* rounding to the closest second.
* @param factor a scaling factor
*/
public void scale(double factor) {
//ToDo
}
/**
* Render the duration as
* d:h:m:s
* (preceded by a '-'if the duration is negative)
* where the four components are normalized non-negative
* integer values. The final three components are always rendered
* in 2 digits. The two leading components and their
* associated ':' delimiters are omitted if the leading values
* are zero. E.g., Duration(0,-1,-59,-61) would be rendered as
* "-02:00:01".
*/
public String toString() {
//ToDo
return "";
}
// Comparison and hashing
/**
* Compares two durations for equality. They are considered equal if
* their getTotalSeconds() values are equal.
*
* @param obj object to be compared for equality with this duration
* @return <tt>true</tt> if the specified object is equal to this one
*/
public boolean equals(Object obj) {
//ToDo
return false;
}
/**
* Returns the hash code value for this object.
*
* @return the hash code value for this duration
*/
public int hashCode() {
//ToDo
return 0;
}
/**
* Return a (deep) copy of this object.
*/
@Override
public Object clone() {
//ToDo
return null;
}
}
Explanation / Answer
Hi I have added testcases for five modules. But due to time constraint not able to complete last three
They are hashcode, equals and ignore
Please ignore that
Testduration.java
---------------------------------------------------------------------------------------------------------------------
package edu.odu.cs.cs350;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TestDuration {
Duration duration = new Duration(1,1,1,1);
long totalseconds = (1*24*60*60) + (1*60*60) + (1*60) + 1;
@Test
public void getTotalSeconds() {
assertEquals(totalseconds,duration.getTotalSeconds());
}
/**
* covers cases for
* getDays getHours getMinutes getSeconds
*
*/
@Test
public void setTotalSeconds() {
duration.setTotalSeconds(totalseconds);
assertEquals(duration.getDays(),1);
assertEquals(duration.getHours(),1);
assertEquals(duration.getMinutes(),1);
assertEquals(duration.getSeconds(),1);
Duration duration1 = new Duration(-1,-1,-1,-1);
assertEquals(duration1.getDays(),-1);
assertEquals(duration1.getHours(),-1);
assertEquals(duration1.getMinutes(),-1);
assertEquals(duration1.getSeconds(),-1);
}
@Test
public void add() {
Duration duration1 = new Duration(1,1,1,0);
duration.add(duration1);
assertEquals(duration.getDays(),2);
assertEquals(duration.getHours(),2);
assertEquals(duration.getMinutes(),2);
assertEquals(duration.getSeconds(),1);
}
@Test
public void subtract() {
Duration duration2 = new Duration(0,0,0,1);
duration.subtract(duration2);
assertEquals(duration.getDays(),1);
assertEquals(duration.getHours(),1);
assertEquals(duration.getMinutes(),1);
assertEquals(duration.getSeconds(),0);
}
@Test
public void equals() {
Duration duration1 = new Duration(1,1,1,1);
duration.toString();
assertEquals(duration, duration1);
}
/*//@Test
public int hashCode() {
//Duration duration123 = new Duration(1,1,1,1);
assertEquals(duration.hashCode(), duration.hashCode());
return 123;
}
@Test
public void Clone() throws CloneNotSupportedException {
Duration duration1 = (Duration) duration.clone();
try {
assertEquals(duration, duration1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public String toString(){
duration.toString();
assertEquals(duration.toString(), "01:01:01:01");
Duration duration1 = new Duration(-1,-1,-1,-1);
assertEquals(duration.toString(), "-01:-01:-01:-01");
return null;
}*/
}
Duration.java
------------------------------------------------------------------------
package edu.odu.cs.cs350;
import java.util.Arrays;
/**
* A duration represents a period of elapsed time, e.g., 4 hours, 23 minutes, 2
* seconds. This is differentiated from a point in time (e.g., 4:23:02AM). A
* meeting that has a duration of 2 hours has that same duration no matter where
* it was held. A starting time (point) for that of 2:00PM EST can be
* unambiguous only if the time zone is added.
*
* <p>
* Because durations are often used in calculations, both positive and negative
* values are possible.
*
* <p>
* Most of the accessor functions for this class will respond with normalized
* values, where the normalization rules are as follows:
* <ul>
* <li>The seconds and minutes components will have an absolute value in the
* range 0..59, inclusive.</li>
* <li>The hours component will have an absolute value in the range 0..23,
* inclusive.</li>
* <li>The sign of each component matches the sign of the overall duration. A
* duration of -61 seconds, for example, has normalized components of -1 seconds
* and -1 minutes.</li>
* </ul>
* Inputs to the member functions are not, however, required to be normalized.
* new Duration(0,0,3,-61) and new Duration(0,0,1,59) are both acceptable (and
* the resulting Duration objects are equal).
*
* @author zeil
*
*/
public class Duration implements Cloneable {
int days;
int hours;
int minutes;
int seconds;
long totalSeconds;
/**
* Construct a new duration, equivalent to Duration(0,0,0,0).
*/
public Duration() {
// ToDo
}
/**
* Create a new duration.
*
* @param totalSeconds
* total number of seconds in duration
*/
public Duration(long totalSeconds) {
// ToDo
boolean negative = false;
if (totalSeconds<0) {
negative = true;
}
this.totalSeconds = totalSeconds;
days = (int) (totalSeconds/(24*60*60));
totalSeconds = totalSeconds % (24*60*60);
hours = (int) (totalSeconds)/(60*60);
totalSeconds = totalSeconds % (60*60);
minutes = (int) (totalSeconds)/(60);
seconds = (int) (totalSeconds % (60));
if (negative) {
days = days*-1;
hours = hours*-1;
minutes = minutes*-1;
seconds = seconds*-1;
}
}
/**
* Create a new duration.
*
* @param days
* number of days in duration
* @param hours
* number of hours in duration
* @param minutes
* number of minutes in duration
* @param seconds
* number of seconds in duration
*/
public Duration(int days, int hours, int minutes, int seconds) {
// ToDo
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.totalSeconds = seconds + (minutes*60) + (hours*60*60) + (days*24*60*60);
}
/**
* Get the total seconds of this duration, including the contributions of
* the days, hours, minutes, & seconds components.
*
* @return the total seconds
*/
public long getTotalSeconds() {
// ToDo
return totalSeconds;
}
/**
* Set the total seconds of this duration, potentially altering the days,
* hours, minutes, & seconds components.
*
* @param totalSeconds
* the total seconds to set
*/
public void setTotalSeconds(long totalSeconds) {
this.totalSeconds = totalSeconds;
days = (int) (totalSeconds/(24*60*60));
totalSeconds = totalSeconds % (24*60*60);
hours = (int) (totalSeconds)/(60*60);
totalSeconds = totalSeconds % (60*60);
minutes = (int) (totalSeconds)/(60);
seconds = (int) (totalSeconds % (60));
}
/**
* How many days in this duration?.
*
* @return the normalized days component
*/
public int getDays() {
// ToDo
return this.days;
}
private static final long secondsPerHour = 60 * 60;
/**
* How many hours in this duration?.
*
* @return the normalized hours component
*/
public int getHours() {
// ToDo
return this.hours;
}
/**
* How many minutes in this duration?.
*
* @return the normalized minutes component
*/
public int getMinutes() {
// ToDo
return this.minutes;
}
/**
* How many seconds in this duration?.
*
* @return the normalized seconds component
*/
public int getSeconds() {
// ToDo
return this.seconds;
}
/**
* Add another duration to this one.
*
* @param dur
* a duration
*/
public void add(Duration dur) {
// ToDo
long totalseconds = dur.totalSeconds + this.totalSeconds;
setTotalSeconds(totalseconds);
}
/**
* Subtract another duration from this one.
*
* @param dur
* a duration
*/
public void subtract(Duration dur) {
// ToDo
long totalseconds = this.totalSeconds - dur.totalSeconds;
setTotalSeconds(totalseconds);
}
/**
* Multiply this duration by a scaling factor, rounding to the closest
* second.
*
* @param factor
* a scaling factor
*/
public void scale(double factor) {
// ToDo
}
/**
* Render the duration as d:h:m:s (preceded by a '-'if the duration is
* negative) where the four components are normalized non-negative integer
* values. The final three components are always rendered in 2 digits. The
* two leading components and their associated ':' delimiters are omitted if
* the leading values are zero. E.g., Duration(0,-1,-59,-61) would be
* rendered as "-02:00:01".
*/
public String toString() {
// ToDo
String text = "";
if (days > 0) {
text = text + String.format("%02d",days);
}
text = text +":"+ String.format("%02d",hours) +":" + String.format("%02d",minutes) +":" + String.format("%02d",seconds);
System.out.println(text);
return text;
}
// Comparison and hashing
/**
* Compares two durations for equality. They are considered equal if their
* getTotalSeconds() values are equal.
*
* @param obj
* object to be compared for equality with this duration
* @return <tt>true</tt> if the specified object is equal to this one
*/
public boolean equals(Object obj) {
// ToDo
if (obj == this) {
return true;
}
if((obj == null) || (obj.getClass() != this.getClass())) {
return false;
}
Duration dur = (Duration) obj;
return (dur.totalSeconds == this.totalSeconds);
}
/**
* Returns the hash code value for this object.
*
* @return the hash code value for this duration
*/
public int hashCode() {
return (int) (this.totalSeconds % 13) ;
}
/**
* Return a (deep) copy of this object.
* @throws CloneNotSupportedException
*/
@Override
public Object clone() throws CloneNotSupportedException {
// ToDo
return (Duration) super.clone();
}
}