Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please help me with my assignment. Thank you! I'm putting all the info here. ** * An amount of time, always positive. *

Can someone please help me with my assignment. Thank you! I'm putting all the info here.

image text in transcribed

image text in transcribed

** * 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 way 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) } }

/** * A time period within historical time. */ public class Period { // TODO: Fields (should be final) // Use the ADTs created already /** * Construct a period given the start time and length. * @param s start time, must not be null * @param l length, must not be null */ public Period(Time s, Duration l) { // TODO } /** * Construct a period given the start and end time * @param from start time, must not be null * @param to end time, must not be null or before the start time */ public Period(Time from, Time to) { // TODO } /** * Construct a period given the length and end time. * @param len length of the period, must not be null * @param end end time of the period. */ public Period(Duration len, Time end) { // TODO } /** * Return the start time of the period. * @return beginning time */ public Time getStart() { return null; // TODO } /** * Return the stop time of the period. * @return end time */ public Time getStop() { return null; // TODO } /** * Return the length of the period. * @return the amount of time in this period */ public Duration getLength() { return null; // TODO } @Override public boolean equals(Object x) { return false; // TODO // NB: If you find yourself failing tests using assertEquals, // watch out that you defined this method correctly // Remember that for immutable ADTs, one shouldn't use ==. } @Override public int hashCode() { return 0; // TDDO: return Objects.hash(...all the fields...); } @Override public String toString() { return null; // TODO } }

** * A point in time. */ public class Time { // TODO: declare a final long field., // representing milliseconds since the Java epoch. // The solution also has a private constructor, // which is very useful -- we recommend you declare it here. /** * Create a time for now. */ public Time() { // TODO one line of code } /** * Create a time according to the time parameter. * @param c calendar object representing a time, must not be null */ public Time(Calendar c) { // TODO: one line of code } // Override/implement three methods standard for immutable classes. /** * Return the difference between two time points. * The order doesn't matter --- the difference is always a * (positive) Duration. * @param t time point to compute difference with * @return duration between two time points. */ public Duration difference(Time t) { return null; // TODO (one line of code) // Duration has an easy way to create 'n' times some constant duration }

/** * Return the time point after a particular duration. * If the point advances too far into the future, * more than a hundred million years from now, this * method may malfunction. * @param d duration to advance, must not be null * @return new time after given duration */ public Time add(Duration d) { return null; // TODO (one line of code) // Duration has an easy way to ask how many of some constant Duration it is. } /** * Return the time point before a particular duration. * If a point regresses too far into the past, * more than a hundred million years from now, * this method may malfunction. * @param d duration to regress, must not be null * @return new time before this one by the given duration. */ public Time subtract(Duration d) { return null; // TODO (one line of code) // Very similar to add } }

1 Immutable ADTs and standard methods: This week, our ADTs will be immutable-all fields are final. The fields are assigned in the con[l]structor and never changed. If you find yourself adding non-final fields or wanting to change the value of a field, you are doing something wrong. Immutable ADTs simplify programming because then you don't need to worry that (say) an hour suddenly becomes a minute. This is one of many examples where reducing power makes programming clearer and simpler. In most situations (including this Homework), immutable ADTs should override the standard implementations of the following methods: equals(Object) Return true if the argument has the same value as this. hashCode() Return an integer that summarizes this value. This method should be efficient. Do not create a string in the process. toString() Return a descriptive string. 2 Concerning the Duration ADT: A duration is a non-negative amount of time; a duration can be as short as a millisecond, or as long as 100 million years. The zero duration is also possible. The Duration ADT implements the standard methods described above, where toString() re]turns 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 unit is written as a decimal. The Duration ADT provides the following additional operations: add(Duration) Return a new duration that is equal in size to the sum of the sizes of this duration and the argument. subtract(Duration) Return a new duration that when added to the argument will be equal to this. If no such duration exists (because the argument is bigger than this) throw an "arithmetic exception." scale(double) Return a new duration that is the argument times 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 IllegalArgumentException. 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 Duration class provides the following constants: INSTANTANEOUS (zero-length duration), MILLISECOND, SECOND, MINUTE, HOUR, DAY, YEAR The last one is tricky to define. You are required 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. Internally a duration is represented in terms of a integral number of milliseconds with a long field (rounding if we have a fraction), but this fact should not be revealed by defining a getter or making the instance fields non-private. Every other class (including Time) using Duration must work with the public methods described above. You are NOT required to handle overflow -if the user tries to define a billion years, that's their problem. 4 Concerning the Period ADT: A period is an extent of historical time. The toString() method should return a string of the form [time; duration]. A period has a start time, a length of time and a stop time, but only two of these are necessary to specific a period. You should write three constructors: one omitting the stop, one the length and one the start time. You may use two or three fields in your class, but these fields should be instances of the other classes from this homework; they should not be long fields. As well as the standard methods for an immutable class, the Period ADT provides the additional methods: getStart() Return the time that this period begins. getStop() Return the time that this period ends. getLength() Return the length of time of this period

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions

Question

What are the possible binding times in a program?

Answered: 1 week ago