Assignment 7: Arrays of objects, searching, sorting, file
input and output
Due Monday, June 7 by 11:59:59pm on Blackboard -- NO LATE ASSIGNMENTS
WILL BE ACCEPTED FOR POINTS
This assignment will introduce you to working with an array of
objects of some kind (Clock in this case). You will perform
basic operations on the array including sorting, searching, retrieving
and modifying. You will also get a chance to further work
with input files and output files. The assignment will
require you to slightly modify your Clock class from the previous
assignment, write a new class ClockShop, and work with an existing
class (ClockShopTester) to test the functionality of your ClockShop
class. Details for each class are given below.
- Modify your Clock class so that it implements the
public class Clock implements
- Write the required compareTo method to compare the current
clock to the clock passed in as a parameter. The signature of
this method looks like this:
public int compareTo(Clock
This method should return a negative value if the current object (this)
is less than the object passed in (that), a positive value if the
current object is greater, and 0 if the two objects are the same.
This class will hold an array of Clock
objects. It will provide behaviors (methods) that allow for
sorting, searching, retrieving, and printing the Clocks
it contains. Specifics for this class are as follows:
- Declare a private field that is an array of type Clock
- Include a method called fillClockShop
which is passed the name of a file (which is a String).
The method opens the file specified in the parameter to the method for
input. This file will contain times for clocks, one per line,
in the following format: hour.minute.second (e.g.
11.29.45). There will be at least one entry in the
file. Read through the file and determine how many entries
there are. After this, allocate an appropriately sized array
of type Clock, then fill the array with Clock objects initialized based
on the values in the input file. NOTE: You will most likely
need to read through the input file twice.
- Include a method called sortClocks that
sorts the Clocks in ascending (smallest time to
largest time) order. You must write the code to sort the
array of Clocks (meaning calls to the Java API to
do this are not allowed)
- Include a method called findClock that
is passed a Clock object and checks to see if it
exists in the Clock array. If it does
exist, return the index of where the first one was found. If
it does not exist, return a -1. You must use Binary Search to
perform this task. Remember that Binary Search requires the
array to be in sorted order to work properly.
- Include a toString method that builds a
String containing each Clock
object in String format (via the toString
in the Clock class) and returns that String.
Each Clock object should be separated from the
others by a newline (\n).
- Include a getClock method that is
passed an integer index of which Clock to retrieve
from the array of Clocks. If the index is
outside the range of the array, throw an IllegalArgumentException.
If the index falls within the array, return the Clock
object at that index.
- Include a setClock method that is
passed a Clock object and an index. The
method places the Clock object at the specified
index in the array. If the index is outside the range of the
array, throw an IllegalArgumentException.
- Include a writeClocksToFile method that
is passed the name of a file (which is a String),
opens a file by that name, then writes the contents of the array of Clocks
to the file. It then closes the file.
- Here is a 'skeleton' of the ClockShop class you can use to
get started: ClockShop.java
Use the following class to test/run your program.
You *must* design your ClockShop class so that the
code in this class compiles and executes properly. You are
not allowed to modify the contents of this class without express
permission from the management (Tom).
To Turn In
Submit to Blackboard all source files (Clock.java,
ClockShop.java, ClockShopTester.java) and the input file (name it
clocks.txt) you used to test your program. Make sure
Clock.java, ClockShop.java are fully
javadoc'ed. Include your history of work in ClockShop.java. This is hw7.
REMINDER: No assignments will be accepted after 11:59:59pm, Monday,
June 7 for points.