Homework 1

Due Date: Wed, Sep 8
Point Value: 100 points 

In this homework assignment, you will write two independent classes: a Vector class and a HospitalAnalyzer class.

The Vector class

The Java Application Programming Interface (API) provides a Vector class as part of the java.util package. You will write your own Vector class using the same data members and implementing a subset of the methods.

The Vector class maintains a list of Objects using an array and keeps track of the number of Objects currently stored in the array. When an attempt is made to add another Object to a full array, the Vector class first creates a new larger array and copies all of the elements in the original array to the new array. The reference to the original array is then set to refer to the new array. The Java API specifies how much larger the new array is compared to the original array depending on the value of the capacityIncrement.

Your Vector class must contain and use only the instance variables defined EXACTLY as shown below:

  protected Object[] elementData;
  protected int elementCount;       
  protected int capacityIncrement;
The protected keyword means that any class that extends the Vector class will have access to the instance variables. We will discuss this further when we go over Inheritance.

The table below lists the Vector class methods that you must implement. Please see the (API) for the Vector class for the specifications for each method and follow it carefully.

Vector Class Methods to Implement
public Vector(int initialCapacity, int capacityIncrement)
public Vector(int initialCapacity)
public Vector()
public void copyInto(Object[] anArray)
public void trimToSize()
public int capacity()
public int size()
public boolean isEmpty()
public boolean contains(Object elem)
public int indexOf(Object elem)
public int indexOf(Object elem, int index)
public int lastIndexOf(Object elem)
public int lastIndexOf(Object elem, int index)
public void removeElementAt(int index)
public void insertElementAt(Object obj, int index)
public void addElement(Object obj)
public boolean removeElement(Object obj)
public void clear()
public Object get(int index)
public Object set(int index, Object element)

Some of the methods above must ``throw an exception'' to handle various error conditions. Here is an example of how to write the code for one of the Vector constructors which must throw an IllegalArgumentException if the value of the initialCapacity argument is negative:

public Vector(int initialCapacity, int capacityIncrement) throws IllegalArgumentException {
  if (initialCapacity < 0)
    throw new IllegalArgumentException();


NOTE that to get the complete specification for the removeElement(Object obj) you will also need to see the Java API documentation for the remove(Object obj) method which functions identically.

NOTE also that the documentation of the clear() method is NOT very clear, so you can implement that method anyway you like as long as to a client of your Vector class, the Vector is empty after calling the clear method.

You may not import the java.util package!


The file, TestVector.java, contains a main method which can be used to test your Vector class. This main method together with your Vector class implementation must compile and run. Provide a "stub" method for any methods you do not complete so that the provided main method will still compile and run. For example,

public int indexOf(Object elem) {
  return -1;
You can compile and run the program as follows (the first command only needs to be done once):

csc% add jdk140
csc% javac Vector.java
csc% javac TestVector.java
csc% java TestVector
Your output should be the exactly same as the output in the file, TestVectorOutput. To test this, you can use redirection to capture the output from your program and then use the diff utility to be sure the output is the same. For example,

csc% java TestVector > YourOutput
csc% diff YourOutput TestVectorOutput
By looking at the provided main method and its output, you can get a better idea of how each method should be implemented. You should add additional tests to the main method to more thoroughly test your code!!

NOTE that the main method does not test all of the things we will test when grading, so be sure to adhere to the specification and test thoroughly!

The HospitalAnalyzer class

You will design and implement a complete program that helps a hospital analyze the flow of patients into the emergency room. An input file contains integers that represent the number of patients that entered the emergency room during each hour of each day for four weeks. Report the total number of patients per week, per day of the week, and per hours of the day.


Your program must accept exactly one argument at the command line which is the name of the input file and output a report of the total number of patients that entered the emergency room each week, day of the week, and hour of the day as shown below:

csc% java HospitalAnalyzer MarchPatientData

             Hospital Emergency Room Analysis

Week 1  Week 2  Week 3  Week 4
 3247    2535    2744    2703   

 Sun  Mon  Tue  Wed  Thu  Fri  Sat
1504 1593 1484 1535 1934 1544 1635 

  00   01   02   03   04   05   06   07   08   09   10   11
 429  531  429  348  425  453  479  459  515  820  442  423 

  12   13   14   15   16   17   18   19   20   21   22   23
 635  412  396  409  400  470  471  448  537  445  383  470 

The input file contains white-space delimited integer values. Each line of the file may contain one or more integer values. The first value in the file corresponds to the number of patients entering the emergency room on Sun of Week 1 at midnight (00 hours). The file must contain at least 4 weeks of data in order for the analysis to be completed. Any additional data is ignored by the program.

You can assume the total number of hours for any category will range from 1 - 9999 and must format your report accordingly. (In other words, your report should still look decent with the headings above the correct values regardless of the magnitude of the values.)

If the user does not enter exactly one argument on the command line, a usage message should be output and the program should exit. For example:

csc% java HospitalAnalyzer way too many arguments!
usage: HospitalAnalyzer filename

The HospitalAnalyzer class must contain the following methods:

 * Creates a HospitalAnalyzer object to analyze emergency room data
 * @param filename file containing integer values for each hour of the day for
 *                 four weeks starting with Sun at 00 (midnight)
 * throws HospitalAnalyzerException  if file cannot be opened, an error occurs
 * while reading the file, the file does not contain enough data, the file contains
 * a non-integer value.
  public HospitalAnalyzer(String filename)
 * Returns total patients per week for each of 4 weeks being analyzed
 * @return array containing the total number of patients per week (Weeks 1 - 4)
  public int[] getPatientsPerWeek()

 * Returns total patients per day for each day of the week starting with Sunday
 * @return array containing the total number of patients per day of the week (Sun - Sat)
  public int[] getPatientsPerDay()

 * Return total patients per hour of the day starting with midnight (00) and ending with
 * 11:00 pm (23) 
 * @return array containing the total number of patients per hour of the day (00 - 23)
  public int[] getPatientsPerHour()
 * Returns formatted analysis of patient data showing total patients per week, per day
 * of the week and per hour of the day
 * @return report showing total patients per week, day of the week and hour of the day
  public String getReport() 

 * Provides a report of the total patients per week, day of the week, and hour of the day
 * for 4 weeks based on the patient data contained in the given input file. The file should
 * should contain the number of patients that entered the emergency room each hour of the day 
 * for 4 weeks beginning at midnight Sunday of the first week as white-space delimited integer
 * values. Any additional values are ignored by the program.
 * @param args[0] input file name
  public static void main(String[] args) {
    if (args.length == 1) {
      try { 
        HospitalAnalyzer analyzer = new HospitalAnalyzer(args[0]);
	System.out.println("\n\nPATIENTS PER WEEK: ");
	int[] array = analyzer.getPatientsPerWeek();
	for (int i = 0; i < array.length; i++)
	  System.out.println("Week " + (i + 1) + " " + array[i]);
	System.out.println("\n\nPATIENTS PER DAY: ");
	array = analyzer.getPatientsPerDay();
	for (int i = 0; i < array.length; i++)
	  System.out.println("Day " + (i + 1) + " " + array[i]);
	System.out.println("\n\nPATIENTS PER HOUR: ");
	array = analyzer.getPatientsPerHour();
	for (int i = 0; i < array.length; i++)
	  System.out.println("Hour " + i + " " + array[i]);
      catch (HospitalAnalyzerException e) {
	System.out.println("usage: java HospitalAnalyzer filename");
      System.out.println("usage: java HospitalAnalyzer filename");
The patient data must be stored internally using a three dimensional array. If you maintain arrays for the patients per week, day, or hour, you must return a ``deep'' copy of the array from the corresponding getter method. In other words, do not return the instance variable array directly since that will give the client of your class access to your private instance data!!!

A HospitalAnalyzerException should be thrown, with an appropriate message, for each of the following error conditions:

For example,
public HospitalAnalyzer(String filename) throws HospitalAnalyzerException {
  //Catch exception that occurs when trying to convert non-integer String contained in
  //input file to an integer and throw a HospitalAnalyzerException with 
  //appropriate message
  catch (NumberFormatException e) {
      throw new HospitalAnalyzerException(filename + 
	                         " contains non-numeric input on line " + lineNumber);

The HospitalAnalyzerException class is provided for you in the file HospitalAnalyzerException.java.

Use the main method provided above which catches any HosptialAnalyzerException's that occur, outputs the exception message along with the usage message and exits. For example,

csc% java HospitalAnalyzer jlkjl
HospitalAnalyzerException: File error: java.io.FileNotFoundException: jlkjl (No such file or directory)
usage: HospitalAnalyzer filename


Sample input files, MarchPatientData, AprilPatientData, and MayPatientData, together with the expected output files, MarchPatientReport, AprilPatientReport, and MayPatientReport, are provided for you.


The HospitalAnalyzer class is based on a exercise from JAVA Software Solutions by John Lewis and William Loftus.

Submit and Grading

Your code must follow the Style Guidelines. Every method and class must be documented using javadoc comments. The documentation for each class must contain the "@author" tag as well as the basic description of the class. The documentation of each method must contain a basic description, the "@param" tag for every parameter, the "@return" tag for any non-void returns, and the "@throws" tag if it throws an exception. For example,

 * Inserts the integers in the other IntegerList in this Integer List beginning at
 * the given index
 * @param other other IntegerList
 * @param n index in IntegerList where first integer in the other IntegerList should
 *          be inserted
 * @throws java.lang.ArrayIndexOutOfBoundsException
  public void insert(IntegerList other, int n) 
                             throws ArrayIndexOutOfBoundsException {

Turn in a hardcopy of the 2 source files, Vector.java and HospitalAnalyzer.java, by Wed, Sep 8 and be prepared to demo your programs. You will then write TestDriver's to thoroughly test your classes. These will be due Mon, Sep 13.