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
- (3 points) Explain, in a few sentences, why the concept of
abstraction is important when designing and implementing
data structures.
- (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;
}
}
-
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.)
-
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?
- (4 points, 2 points per part)
You're given an ordered array of words.
aardvark |
abacadabra |
abacus |
babble |
batch |
codify |
create |
- 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.)
- 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.
-
(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.)
-
(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:
-
Add: Adds a new account to the list, with two
parameters (name and balance). This method returns a
boolean, which is true if the account was successfully
added and false if the account was not successfully added
(for example, if the array is full). You do not
need to search for duplicates.
-
Remove: Given a name as a parameter, remove an
account from the list. Return a boolean indicating
success, similar to the add function.
-
Search: Given a name as a parameter, return the
Account object that contains that name.
-
Dump: Print out all the accounts. No parameters,
and no return values.
-
Total: Return the total amount of assets in the
bank, e.g., a total of all the accounts' balances.
Does not take any parameters.
-
(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 <name> <amount>: Add an account for
name with amount as the balance, or print out
"database full" if the array is full;
- r <name>: Remove the account associated
with name, or print out "not found" if it's not
found;
- s <name>: Find and print out the
information associated with name. Print out
"not found" if it's not found;
- d: Dump the entire database;
- t: Print out the total assets in the bank;
- q: Quit the program.
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:
-
The book spends a lot of time arranging programs like this,
and in fact, I strongly recommend you check out the program
detailed on pages 66-69 in chapter 2; it's very similar in
structure to this program, and may be a useful
springboard if you don't feel comfortable with starting from a
blank editor. I believe the book's homepage even has the code
downloadable so you don't have to type it out, if you want to
play with it.
-
You cannot use collections, like ArrayList or Vector,
in this assignment. In fact, we will avoid all "java.util"
data structures for the majority of the class, as the goal is
to become familiar with data structures by writing them
yourself.
-
In order to handle the input, you need to be familiar with a
few Java classes: BufferedReader (to read the line),
StringTokenizer (a useful tool to break up a line into
individual "tokens", and the Double class (which has a way of
parsing the numbers into a double format). What I suggest is
that you first get the program working without
interactive user input, and then focus on the input aspects,
all of which should fit straight inside your main()
method. Use the Javadocs (API documentation; see Resources page) to your benefit
in learning these classes!