CISC 3130 — Lab #1

CISC 3130
Data Structures
Lab #3
Programming to the Interface

How to Develop and Submit your Labs

Make sure you read the sections in the above on how to interpret CodeLab string comparison feedback

Labs 3.1 - 3.3 — Implementations of Time

Overview

There are two basic ways of representing time of day: civilian (12 hour) time and military (24 hour) time. Civilian time uses a 12 hour clock from 1:00 through 12:00 and an am/pm suffix to indicate before or after noon. Military time uses a 24 hour clock, from 0000-2300 and thus has no need for any additional indicators. Either can be used to represent time. You will be implementing both representations.

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.

Details

I've provided the Time interface and the TimeApp app; you must write the three implementations (CivilianTime, MilitaryTime, and ElapsedTime) and their apps.

The TimeInterface

Here is minimal interface for time:
interface Time {
	void set(String timeStr);
	int get12Hour();
	int get24Hour();
	int getMinute();
	void tick();
}
Notes
  • Like the social security example presented in class, the main point of this is the programming to the interface aspect; i.e., having multiple implementations of the same interface, and having an app which works with wither of them because it only uses methods from the interface.
    • As such, the classes have minimal functionality (to your benefit), but also may seem a bit forced. The basic idea was to not distract you too much with' time'-specific logic allowing you to concentrate on the interface/implementation aspects of the exercise.
  • I've added a set method in order to give you more practice with leveraging (as well as to make the common (interface) app, TimeApp, a bit simpler.
    • The form of the argument to 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).
    • You will need some logic in one or both of your implementation's constructors to transform the argument into the internal representation. The same logic will be necessary in the set. Try to avoid duplicating the logic by putting it into set and having the constructor call set.
  • Note that his is a mutable class; i.e., the obejcts in this class can be modified. In particular, set allows the user to change the value of an object, and tick modifies the receiver to the next 'instant' of time.
    • This is in contrast with immutable classes — like String where objects are never modified, rather new objects are created as method calls
  • As mentioned before, the methods of the interface may seem a bit forced; they are mainly there to give you something to implement (and slightly differently for each implementation) as well as something to 'do' in the app.

The Interface-Specific App 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();
		}
	}
}
Notes
  • Only one object is sent to the app (we'll see a different scenario in the next set of labs)
  • The time sent is first displayed in various ways
  • We then illustrate the tick method:
    • We set the time to 2 minutes before 1am and tick it 3 times, causing it to cross the hour barrier, displaying the time after each tick.
    • We then tick it 57 times to bring it up to the next hour barrier, and then again tick 3 times.
    • We repeat this until we've cycled back to our initial time
  • The output can be seen after the section for Lab 3.3.

The Implementations

For all three implementations, have the constructor accepts the same format argument as the set method, i.e., hh:mmxm

The Implementation-Specific App Classes

Codelab Notes

There are three CodeLabs for this lab, one per implementation.

Lab 3.1 — Civilian Time

Create the class CivilianTime that implements the Time interface with an internal representation corresponding to civilian time.

Lab 3.2 — Military Time

Lab 3.3 — Elapsed Time

The TimeApp Output

Should 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
Notes
  • Make sure you properly handle the leading 0's; I used printf with no problem

Labs 3.4 - 3.5 — JCF's Set Implementations>

Overview

As mentioned in Lab 2, the Java Collection Framework (JCF) contains two implementations of a set: 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.

Details

The SetApp Output

(Please note that the order of the elements will come out differently for HashSet and TreeeSet; consult the test case tables if you have an issue)
=== 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]
Notes
  • Since we're programming to the interface, both implementations are using the same app, and therefore the output is the same for both.

Lab 3.4HashSetApp

An implementation-specific app that invokes the interface-specific SetApp as spec'ed in the overview section above.

Lab 3.5TreeSetApp

An implementation-specific app that invokes the interface-specific SetApp as spec'ed in the overview section above.

Code Relevant to this Lab