Getting (back) into Java
1 Getting (back) into Java

Getting (back) into Java

CS 3500, Spring 2024

Example: Durations

Analysis

What do we need to think about?

Examples of durations

  • \(43.27 \mu s\)

  • \(10^9\) years

  • 525,600 minutes

  • 3 days, 6 hours, 17 minutes, and 25 seconds

Object-oriented analysis

What might we do with them?

  • Convert units: 10 minutes = 600 seconds

  • Format: 257 seconds as 4:17 or 0:04:17

  • Add: 2:45 plus 3:30 is 6:15

  • Subtract: 5 hours minus 2 hours is 3 hours

  • Decompose: 7868 as 2 hours, 11 minutes, and 8 seconds

  • Compare durations for equality and ordering

Assumptions

Some simplifying assumptions:

  • Minimum time resolution: seconds

  • Lower bound: 0

  • Upper bound: unspecified

  • Unitless: 180 seconds is equal to 3 minutes

Observations

long inSeconds();

String asHms();

int compareTo(Duration other);

boolean equals(Object other);

int hashCode();

Operations

void add(Duration other);
void sub(Duration other);

Operations

Duration plus(Duration other);
Duration minus(Duration other);

(We'll just do plus.)

Representation

Let's discuss.