CS3134 Homework #1
Due on September 25, 2003 at 11:00am

There are two parts to this homework: a written component worth 15 points, and a programming assignment worth 10 points. See the homework submission instructions on how to hand it in and for important notes on programming style and structure.

Note: parts in red are revisions/clarifications.

Written questions

  1. (3 points) Explain, in a few sentences, why the concept of abstraction is important when designing and implementing data structures.
  2. (8 points, 4 points per part) You're given the following code that manipulates an array of ExampleData objects:
    public class ExampleRunner {
      public static void main(String[] args) {
        ExampleData[] dataArray = new ExampleData[5];
    
        for(int i=0; i < dataArray.length; i++) {
          System.out.println(dataArray[i].getString() + "," + 
    			 dataArray[i].getInteger());
        }
      }
    }
    
    class ExampleData {
      private int testInteger = 0;
      private String testString = null;
    
      public ExampleData() {
        testString = "testing";
        testInteger++;
      }
    
      public String getString() {
        return testString;
      }
    
      public int getInteger() {
        return testInteger;
      }
    }
    
    1. This code will compile, but it will immediately crash. State what error is generated, and explain in 1-2 sentences why this error is being generated. (Saying something generic like "a line of code is missing to do something" does not qualify as an explanation.) Explain in 1-2 sentences what needs to be added to the main() method to fix this. (Hint: if you want to, try typing in and compiling the code, and play around with it interactively.)
    2. Assuming you fix the problem in the previous section, running the program will give you five lines of output -- each containing a String and an integer. What is the output the program will generate? Also, if we wanted to change the behavior of the integer such that it gives us the total number of ExampleData objects that exist, what one Java keyword can we add to the ExampleData class, where, and why (1-3 sentences) would it work?
  3. (4 points, 2 points per part) You're given an ordered array of words.
    aardvark
    abacadabra
    abacus
    babble
    batch
    codify
    create
    1. Give an example of a search term (i.e., a word either in or not in the above database) which would be faster via linear search, and an example of a search term which would be faster via binary search. For each of the two search terms, show why the one search is faster than the other by enumerating the steps that would be taken. (You can assume that we're using the Java String.compareTo method to perform the inequality comparison, which does a lexicographical analysis of two words and returns negative, zero, or positive if the first word is comes before, is equal to, or comes after the second word.)
    2. Explain in 1-3 sentences how these performance results can be reconciled with the assertion that, in general, a binary search is "faster". You may use big-Oh notation if you want, although I'll accept an informal argument as well.

Programming problem

The manager of IdiotBank is desperate: they haven't computerized their systems yet, and his boss (the founder of the IdiotCo group of companies) is coming in 2 weeks to see their flashy new account mangement system. While the manager knows you can't write a complete system for scratch, he wants you to work up a quick demo to show off how he can easily track multiple accounts.

He asserts that the program only needs to handle 10 accounts at this point, so you plan to use an unordered list to store the information. Two pieces of information must be stored: the account holder's name (which is just the last name for this assignment, like "Doe"), and the balance of their account (such as 123.45). A command-line interface is all that's needed for now to manipulate this information.

You elect to write exactly three Java classes to fulfil the above requirements.

  1. (2 points) A class, called Account, that contains an individual account, i.e., it contains (at a minimum) the two fields described above, a constructor to assign values to the fields, and a toString() method to return a String that represents the information in the account. (Hint: you may want to add some additional methods to get the values of the fields, i.e., accessor methods, to make it easier for you to write the Database class.)
  2. (4 points) A class, called Database, that contains a list which will be composed of many of these individual accounts. You can hardcode the length of the array that this list is composed of (see above for a clue as to what the length should be). This class must have methods to support the following operations:
  3. (4 points) A class called App which contains one instance of the list and the main method. It takes input from the user (at a ">" prompt) and manipulates the list accordingly. Here are the commands:

A sample execution of the program would look like the following. Input is italicized.

$ java App
Welcome to the IdiotBank Account Management System!
> a Doe 500.23
> a Foo 240.25
> s Test
Not found
> s Doe
Doe: 500.23
> d
Doe: 500.23
Foo: 240.25
> r Doe
> t
240.25
> q
$

The intent of this programming problem is to practice your knowledge of lists and to get accustomed to writing multi-class programs from scratch. I'm not trying to "trick" you here -- the implementation is fairly straightforward. I will not give you invalid input.

Some notes: