In Project 0, we'll get our development environment set up, dust off our Java skills, practice Test Driven Development (TDD), and implement some interfaces. Haven't installed Java or an IDE yet? Follow the links in the Software and Tools section on the home page. We'll be using Java SE 11; I recommend the free IntelliJ Community Edition IDE which has excellent jUnit integration.
In Test-Driven Development, we start with tests first and implementation second. This is backwards from the usual approach of writing code first, and making it more robust by testing it later (or not testing it at all!). TDD incurs a short-term upfront cost (coming up with a design and writing tests for them), but provides long-term benefits by forcing you to consider "testable" modular design at the outset.
In this project (and all subsequent projects), the tests have already been written - and testing is half the battle! (Knowing is the other half). Your job is to understand the interface and intended behavior described by the tests, and provide a program implementation that matches the implied specification.
Keep in mind that that the test only evaluate the public-facing interface each class provides. Encapsulation encourages us to design our classes in a way such that clients (users) are not concerned with the underlying implementation. You are always welcome and encouraged to define additional methods for your classes as you see fit.
Are you ready? You're always ready. Get the Project 0 starter code here! (jUnit dependencies included).
The project code should work out-of-the-box for IntelliJ (running and debugging), but if you're having trouble getting it working, try these IntelliJ setup instructions.
hello.Hello: A simple Hello, World! class. Hello implements the Comparable<Hello> interface
(official documentation),
which requires that the Hello class implement the .comapreTo(Hello)
method.
compareTo(T other)
returns an integer that indicates how this
and other
should be arranged in "natural" order, such as ascending order. Furthermore, any Comparable<T> type can be sorted using Java's built-in sort method Collections.sort
.
int compareTo(Hello other)
: Compares this
with the argument other
and returns an integer based on how the two instances should be ordered.
For this class, we will use the length of the greeting to dictate order, that is, Hello instances with longer greetings come after instances with shorter ones (ascending order).
this
should be ordered after other
, return a positive integer.this
should be ordered before other
, return a negative integer.0
.compareTo
is used.
hello.Range: An implementation of Python's
range (sequence of numbers) in Java.
Range implements the Iterable interface (official documentation), which requires Range to fulfill the iterator()
method. The Iterable interface which describes objects that can be "iterated over". Arrays, Lists, and many other data structures we will learn will be iterable.
An Iterable is obligated to supply an
Iterator via the method iterator()
. When a client retrieves an Iterator out of an Iterable, it can use the Iterator instance to walk the Iterable's internal values. Java's built in for-each loop construct is a shorthand for this approach, which you will see examples of in the test suite.
Iterator<Integer> iterator()
: Returns a new instance of RangeIterator (see below) which will iterate over the values in the Range.
hello.RangeIterator: An Iterator (official documentation) which supplies the integer sequence of a range. While Range is fixed and re-useable, RangeIterators can only walk each sequence once. Clients of Range can use iterator()
to retrieve a RangeIterator, then call next()
repeated on the RangeIterator to retrieve values from the number sequence.
RangeIterator(int begin, int end, int step)
: Constructor that defines the sequence of numbers the iterator will produce. Matches the Range(int, int, int)
constructor.
boolean hasNext()
: Returns a boolean indicating whether or not the RangeIterator has values remaining.
Integer next()
: Returns the next integer in the sequence while also advancing the iterator by one step.void remove()
: Not implemented; should throw new UnsupportedOperationException()
.Hint: You do not need an array, ArrayList, or any other storage container to store the values in the range. Instead, consider lazy loading by computing the output on-the-fly with every method call.
A run script is included to run the project test suite as run.bat (Windows) or run.sh (Mac/Linux). This will compile your entire project and produce a run report with your estimated grade. The example below demonstrates what an incomplete implementation might look like.
briancui@X1:/mnt/c/Users/brian/workspace/CSC-143-Project0/src$ ./run.sh BEGIN PROJECT 0 EVALUATION Total tests: 15 ========================== test.HelloTest [3/3] test.RangeTest [6/12] The following test(s) failed: - RangeEqualTo(test.RangeTest) - RangeTestLessThan(test.RangeTest) - RangeTestThrowsWhenEnd(test.RangeTest) - RangeTestCountEveryTwo(test.RangeTest) - RangeGreaterThan(test.RangeTest) - RangeTestCountEveryThree(test.RangeTest) Tests Passed: [9 / 15] Overall Project Score: 60% briancui@X1:/mnt/c/Users/brian/workspace/CSC-143-Project0/src$
The easiest way to run your code manually is using an IDE. In IntelliJ, you can right click on any class in the file explorer and select the Run option. If a test class is selected, IntelliJ will produce a nice visual summary. Furthermore, IntelliJ will indicate exactly which assertion failed and allow you to debug failing tests individually.
You may also compile and run your code directly from command line with javac and java. Command line Java offers additional flexibility by allowing for targeted compilation and execution (allowing you to run code in isolation when other parts of the project have errors). The syntax for Windows and Mac/Linux are a little different, but you can open the run scripts for an example. Below is an example for Mac/Linux:
briancui@X1:/mnt/c/Users/brian/workspace/CSC-143-Project0/src$ javac -cp *:lib/*:. -sourcepath . hello/Hello.java briancui@X1:/mnt/c/Users/brian/workspace/CSC-143-Project0/src$ java -cp *:lib/*:. hello.Hello Hello, World! Hello, There! General Kenobi! briancui@X1:/mnt/c/Users/brian/workspace/CSC-143-Project0/src$
See the submissions page for details. Have questions or need help? Post on Piazza!
These questions aren't graded, but are similar to those you may see on an exam. You are welcome to discuss these with classmates offline on on Piazza.