Question
Duration ADT, JAVA programming A duration is a non-negative amount of time; a duration can be as short as a millisecond, or as long as
Duration ADT, JAVA programming
A duration is a non-negative amount of time; a duration can be as short as a millisecond, or as long as 100 million yeasrs. The zero duration is alos possible.
The duration ADT implements the standard methods described above, where toString() returns a string given in terms of milliseconds(ms.), seconds(s.), minutes(min.), hours(hr.), days or years. Units larger than milliseconds are chosen only if the duration is at least as big as the unit. The number in front of the unis is written as a decimal.
The Duration ADT provides the following additonal operations:
add(Duration) Return a new duration that is equal in size to the sum of the sizeds of this duration and the argument.
subtract(Duration) Return a new duration that when added to the argument will be equl to this. If no such duration exists (because the argument is bigger than this) throw an "arithmetic excpetion"
scale(double) Return a new duraction that is the argument time bigger than this duration. For example, scaling an hour by 24 gets a day; scaling an hour by 0.5 gets the same duration as scaling a minute by 30. Scaling by a negative number is not allowed and should lead to an IllegalArguementException
divide(Duration) Return the relative size of one duration to another as a double(number). For example, Day.divide(HOUR) gives 24 and SECOND.divide(MILLISECOND) gives 1000.
In lieu of a public constructor, the Duraction class provides the following constants.:
INSTANTANEOUS(zero-length duraction, MILLISECOND, SECOND, MINUTE, HOUR, DAY, YEAR
The last one is tricky to define. Needed is to implement a "Julian year" which assumes we have a leap year every four years, and so each year has one quarter of a leap day as well as the normal 365 days.
package lesson2;
import java.util.Objects;
/** * An amount of time, always positive. * To create a duration, scale an existing duration. */ public class Duration { private final long extent; // this must remain private. Do NOT add a getter! // this constructor must remain private: private Duration(long milliseconds) { extent = milliseconds; } // TODO: For all constants, have a line: // public static final Duration CONSTANT = new Duration(...);
public static final Duration INSTANTANEOUS = new Duration(0); public static final Duration MILLISECOND = new Duration(1); public static final Duration SECOND = new Duration(1000); // A long literal needs a "L" at the end, as in 1234567890L // If you are overriding a method from a super class, always // annotate it "@Override" as here, overriding Object#equals(Object) @Override public boolean equals(Object x) { return false; // TODO } @Override public int hashCode() { // the recommended ay to define hashCode: // call Objects.hash(...) listing all the fields. // The "hash" method takes multiple parameters return Objects.hash(extent); } // If you are overriding a method from an interface, then Java 5 // says you CANNOT use Override, but anything later says you MAY. // Please always do unless you write a javadoc comment. @Override public String toString() { return null; // TODO: print different things depending on the extent } // Methods that are not standard or private must have a documentation comment: /** * Add two durations together to get a larger duration. * @param d duration being added to this, must not be null * @return new duration equal to the combined duration of this and the argument. * @throws NullPointerException if d is null */ public Duration add(Duration d) { return null; // TODO (one single line of code) // Remember that you can access the fields of "d" // even though they are private, because this is your class! } /** * Return the duration remaining if the argument duration * 'is removed. The argument must not be longer than this. * @param d duration to remove, must not be null, must not be bigger than this * @return remaining duration after subtracting d * @throws NullPointerException if d is null * @throws ArithmeticException if d is too big */ public Duration subtract(Duration d) { return null; // TODO (2 lines of code) // Need one check before doing something similar to add. }
/** * Return the duration by scaling this duration by the given amount. * The result is rounded. * @param d amount to scale by, must not be negative. * @return new scaled duration * @throws IllegalArumentException if scale factor is negative. */ public Duration scale(double d) { return null; // TODO (2 lines of code) // Read the Javadoc if you find yourself failing tests. } /** * Return the scale of this duration against the argument. * @param d duration to divide by, must not be null or instantaneous * @return how many of the argument fit in this duration * @throws NullPointerException if d is null * @throws ArithmeticException if d is instantaneous */ public double divide(Duration d) { return null; // TODO (2 lines of code) } }
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