There is also a standard internal representation of time: elapsed time, i.e., the number of unit intervals that have elapsed since some fixed, agreed upon starting time.
Time
interface and the TimeApp
app; you must write the three implementations (CivilianTime
, MilitaryTime
, and ElapsedTime
) and their apps.
Time
Interfaceinterface Time { void set(String timeStr); int get12Hour(); int get24Hour(); int getMinute(); void tick(); }
set
method in order to give you more practice with leveraging (as well as to make the common (interface) app, TimeApp
, a bit simpler.
set
is hh:mmxm
, where the x
is either a
or p
. Please also use this format for the
result of your toString
method (don't forget you have to produce output compatible with what CodeLab expects), as well as the argument to the constructor's for all
three implementations (see below).
set
. Try to avoid duplicating the logic by putting it into set
and having the constructor call set
.
set
allows the user to change the value
of an object, and tick
modifies the receiver to the next 'instant' of time.
String
where objects are never modified, rather new objects are created as
method calls
TimeApp
public class TimeApp { public static void demo(Time time) { System.out.println("Should be receiving time of 11:45pm"); System.out.println(time); System.out.println("12 hour: " + time.get12Hour()); System.out.println("24 hour: " + time.get24Hour()); System.out.println("minute: " + time.getMinute()); System.out.println(); System.out.println("Setting time to 12:58am"); time.set("12:58am"); System.out.println(time); System.out.println(); System.out.println("Bunch of ticking"); System.out.println(); for (int h = 0; h < 24; h++) { for (int m = 1; m <= 3; m++) { time.tick(); System.out.println(time); } for (int m = 1; m <= 57; m++) time.tick(); System.out.println(); } } }
set
method, i.e., hh:mmxm
TimApp
. Please pass it the time value of 11:45pm, as you will be needing to pass
CodeLab's output checking.
CivilianTime
that implements the Time
interface with an internal representation corresponding to civilian time.
TimeApp
OutputShould be receiving time of 11:45pm 11:45pm 12 hour: 11 24 hour: 23 minute: 45 Setting time to 12:58am 12:58am Bunch of ticking 12:59am 01:00am 01:01am 01:59am 02:00am 02:01am 02:59am 03:00am 03:01am 03:59am 04:00am 04:01am 04:59am 05:00am 05:01am 05:59am 06:00am 06:01am 06:59am 07:00am 07:01am 07:59am 08:00am 08:01am 08:59am 09:00am 09:01am 09:59am 10:00am 10:01am 10:59am 11:00am 11:01am 11:59am 12:00pm 12:01pm 12:59pm 01:00pm 01:01pm 01:59pm 02:00pm 02:01pm 02:59pm 03:00pm 03:01pm 03:59pm 04:00pm 04:01pm 04:59pm 05:00pm 05:01pm 05:59pm 06:00pm 06:01pm 06:59pm 07:00pm 07:01pm 07:59pm 08:00pm 08:01pm 08:59pm 09:00pm 09:01pm 09:59pm 10:00pm 10:01pm 10:59pm 11:00pm 11:01pm 11:59pm 12:00am 12:01am
printf
with no problem
HashSet
and TreeSet
. We will discuss
the details of the two implementations and their differences, but for the moment it suffices to say they both implement an interface name Set
.
In this lab we continue working with the 'programming to the interface' concept and apply these two implementations to a common interface-sepcific app.
Set
interface. It specifies many more methods than the ones we implemented (contains
and add
) so
we will not be using our set class as one of the implementations in this lab. We COULD bring our set class into synch with the JCF Set
interface, but that would be
nothing more than an unwanted distraction at this point).
HashSet
and TreeSet
. These apps should be trivial at this point;
they mirror the implementation-specific list apps of the lecture
SetApp
; but giain, you have a role model in the ListApp
of the lecture.
SetApp
Output=== Basic Functionality === --- Creating a Set<Integer> and printing it and its initial stats --- [] (0) --- Loading it up to 10 with add --- Adding 0 -> [0] (1) Adding 1 -> [0, 10] (2) Adding 2 -> [0, 20, 10] (3) Adding 3 -> [0, 20, 10, 30] (4) Adding 4 -> [0, 20, 40, 10, 30] (5) Adding 5 -> [0, 50, 20, 40, 10, 30] (6) Adding 6 -> [0, 50, 20, 40, 10, 60, 30] (7) Adding 7 -> [0, 50, 20, 70, 40, 10, 60, 30] (8) Adding 8 -> [0, 80, 50, 20, 70, 40, 10, 60, 30] (9) Adding 9 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) --- Loading it up with the same elements --- Adding 0 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 1 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 2 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 3 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 4 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 5 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 6 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 7 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 8 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) Adding 9 -> [0, 80, 50, 20, 70, 40, 10, 90, 60, 30] (10) --- Membership checking --- set does not contain -50 set does not contain -40 set does not contain -30 set does not contain -20 set does not contain -10 set contains 0 set contains 10 set contains 20 set contains 30 set contains 40 set contains 50 set contains 60 set contains 70 set contains 80 set contains 90 set does not contain 100 set does not contain 110 set does not contain 120 set does not contain 130 set does not contain 140 === Generics === --- Set<Double> --- [0.0, 1.0, 2.0, 4.0, 8.0, 9.0, 5.0, 3.0, 6.0, 7.0] --- Set<String> --- [Str6, Str5, Str4, Str3, Str2, Str1, Str0] === Element Order === --- Adding 10 random values to a Set<Integer> using a Random object with seed=12345 --- adding 51 -> [51] adding 80 -> [80, 51] adding 41 -> [80, 51, 41] adding 28 -> [80, 51, 41, 28] adding 55 -> [80, 51, 55, 41, 28] adding 84 -> [80, 51, 84, 55, 41, 28] adding 75 -> [80, 51, 84, 55, 41, 75, 28] adding 2 -> [80, 2, 51, 84, 55, 41, 75, 28] adding 1 -> [80, 1, 2, 51, 84, 55, 41, 75, 28] adding 89 -> [80, 1, 2, 51, 84, 55, 41, 89, 75, 28] [80, 1, 2, 51, 84, 55, 41, 89, 75, 28]
HashSetApp
An implementation-specific app that invokes the interface-specific SetApp
as spec'ed in the overview section above.
TreeSetApp
An implementation-specific app that invokes the interface-specific SetApp
as spec'ed in the overview section above.