95-771 Data Structures and Algorithms Fall
'01
Homework 0
Programming in Java
Part I.
The Problem Description
This
homework is designed to familiarize the student with the Java programming
language and with the compilation and use of Java packages.
The steps to
completing homework 0 are as follows:
public
boolean equals(IntArrayBag b) /* worst case run time O(m*n) where m and n are
the size of the bags.*/
public
String toString()
Part II
Some notes on using Java Packages under Windows '95
Using packages
with JDK 1.2.1 under Windows '95
Downloading and
Compiling IntArrayBag.java
Below are the
specific steps that I used to compile and test Michael Main's
IntArrayBag class
on my PC.
The IntArrayBag
code was taken from Michael Main's "Data Structures and
other objects using
Java".
The IntArrayBag
code may be downloaded from the web site:
http://www.cs.colorado.edu/~main/
The files
IntArrayBag.java and IntArrayBag.class were placed under the
directory
C:\edu\colorado\collections>
The file
IntArrayBag.java file was compiled with the command
C:\edu\colorado\collections>javac
IntArrayBag.java
Within the IntArrayBag.java
file, the first line reads:
package
edu.colorado.collections;
Notice that the
package name corresponds to the directory structure.
This file
containing my driver code is located at
C:\heinz\90-723\usebag\BagDemonstration.java
It was compiled with
the command
C:\heinz\90-723\usebag>javac
-classpath C:\ BagDemonstration.java
The C:\ specifies
where the search should begin for the file
C:\edu\colorado\collections\IntArrayBag.class
The resulting
.class file (BagDemonstration.class) was run with the
command
C:\heinz\90-723\usebag>java
-classpath C:\;. BagDemonstration
The dot is required
so that java will look in the current directory
as well as the C:\
directory for classes.
My Driver Code
before I added equals() and toString() to IntArrayBag.java
// Test a few
methods in the IntArrayBag class
import
edu.colorado.collections.IntArrayBag;
public class
BagDemonstration {
public static void
main(String[] args) {
IntArrayBag ages =
new IntArrayBag();
ages.add(89);
ages.add(76);
ages.add(55);
ages.remove(76);
ages.add(55);
ages.add(89);
System.out.println(ages.countOccurrences(89));
System.out.println(ages.countOccurrences(55));
System.out.println(ages.countOccurrences(7));
}
}
/* Output
2
2
0
*/