Unformatted Attachment Preview
COMPUTER SCIENCE 12B (SPRING TERM, 2014)
PROGRAMMING IN JAVA
PROGRAMMING ASSIGNMENT 5
Overview:
In this assignment, you will explore the running time of the ArrayList and LinkedList
classes.
Part 1
Time to go on a treasure hunt! You have been given eight treasure maps (tmap1.txt, ..,
tmap8.txt), and have been instructed to find their treasure.
Each map is a long list of numbers, separated by spaces. In order to find the treasure, you start off
at the first number in the list. Then you go to the place that that number describes (for example, if
the first number was 9, you would next go to the 9th number in the list and see where it tells you
to go). Assume that the list is indexed at zero, so the number 1 actually points to the second
number in the list. In order to find the treasure you need to continue this process until you find
the treasure itself, the number zero. A very simple version of this problem is shown below.
52041365
If you trace it through, the path to the treasure would be 5, 3, 4, 1, 2, and the treasure is found at
location 2. Note that there are duplicates, and there may be multiple values of zero within your
array, so simply searching for 0 will not get you the correct result.
What you will be examining through this assignment is the relative running time of the
LinkedList class and the ArrayList class, both part of the Java Collections
framework. Your code will be solving each treasure map twice, once with each kind of list. You
should time each of the lists as they accomplish each task, and report that time. More on how to
do that is in the hint section. Your output should look like the following:
Part 1 has two sub-parts: a coding assignment, and a README_1 File. In the coding assignment
you should create a class TreasureHunter that implements (at a minimum) these static
methods.
importFileIntoList(File f, List l)
This method should take in a list and a file, and should return the list with all of the elements in
the file added to it.
findTreasureLocation(List)
This method should take in a list, and use it to find the treasure as described above.
findTreasurePathLength(List)
This method should take in a list, and use it to find the number of steps taken to reach the
treasure.
goForTreasureHunt(File f)
This method takes in a file, and uses that file to go on the treasure hunt in that file, and prints out
the output as shown above.
The README_1 file should include:
1. The short version of your results (how much time each took to find the treasure, and how
much time it took to load it into the list).
2. A summary of the running time of adding elements on to each list and going to a given
index within each list.
3. The locations that you found the treasure at, in increasing order:
, , …,
Some hints to get you started:
Hint 1: The method System.currentTimeMillis() will give you the current ‘clock time’
within your computer as a long. To save this time, use:
long l = System.currentTimeMillis();
This will save the time that this part of the code was run. Use it twice with subtraction to get the
time it took (in milliseconds) between any two given points in your code.
Hint 2: Do not write code to do the import for each file. Find a creative way (string
concatenation?)
to
loop
through
creating
your
files
and
calling
the
goForTreasureHunt(File f)method.
Hint 3: This assignment does not require Object Oriented Programing, and will be easier without
it.
Part 2
Warm Up First
Construct a LinkedList and an ArrayList with integers from one to two million. Delete the
first 100,000 numbers from both lists. Compare the running time. Write the result of the
comparison in README_2 file.
Onto the second part
In this part of the assignment you are given two text files. One called gibberish.txt, and
one file called deleteCodes.txt. The file deleteCodes.txt contains a list of numbers
representing locations that should be removed from gibberish.txt.
Read the gibberish.txt file into a list of Strings and the deleteCodes.txt into a list of
numbers. Then remove all the indices specified in the list of numbers from the list of Strings.
Remember, every time you delete a String from the list of gibberish.txt, all the locations
shift, so make sure you delete the indices in the order they appear in deletionCodes.txt,
otherwise you will get the wrong result.
Once you have deleted the Strings at all the specified locations, you can check your answer by
doing the following:
1. Take the results1 from the treasure hunt.
2. Multiply each answer by two.
3. In ascending order, output the Strings located at those indices.
For example, if the results from the treasure hunt are: 5,2,8 you would print out the
Strings at locations 4, 10, and 16 of the gibberish list.
Compare the time for when you use a LinkedList to store the gibberish text versus when
you use an ArrayList. Write the result of the comparison in README_2 file.
1
The results from treasure hunt added up should be 1,634,132. If the results from treasure hunt
do not add up correctly, you don’t have the right locations to index into in the gibberish list.
Practice problems for the exam (no need to submit them)
Give a big-O characterization, in terns of n, of the running time of the following
algorithms:
Algorithm A
Input: An array A storing n ≥ 1 integers
Output: The sum of the elements in A
s = A[0]
for i = 1 to n – 1{
s = s + A[i]
}
return s
Algorithm B
Input: An array A storing n ≥ 1 integers
Output: The sum of the elements at even cells in A
s = A[0]
for i = 2 to n – 1 by increments of 2{
s = s + A[i]
}
return s
Algorithm C
Input: An array A storing n ≥ 1 integers
Output: The sum of the prefix sums in A
s = 0
for i = 0 to n – 1{
s = s + A[0]
for j = 1 to i{
s = s + A[j]
}
}
return s
Algorithm D
Input: An array A storing n ≥ 1 integers
Output: The sum of the prefix sums in A
s = A[0]
t = s
for i = 1 to n – 1{
s = s + A[i]
t = t + s
}
return t
Algorithm E
Input: Arrays A and B each storing n≥1 integers
Output: The number of elements in B equal to the sum of prefix
sums in A
c = 0
for i = 0 to n-1{
s = 0
for j = 0 to n-1{
s = s+A[0]
for k = 1 to j{
s = s+A[k]
}
}
if B[i] = s then
c = c+1
}
return c
Submission:
Your Java source code should be submitted via Latte the day it is due, Sunday, March 23 at
11:59pm. For late policy check the syllabus.
Remember to submit along with your code the README files.
Grading:
You will be graded on
o External Correctness: The output of your program should match exactly what is
expected. Programs that do not compile will not receive points for external correctness.
o Internal Correctness: Your source code should follow the stylistic guidelines shown in
class. Also, remember to include the comment header at the beginning of your program.
o One-on-one interactive grading: By the end of the day that the assignment is due,
please make an appointment with your TA for an interactive 10-15 minute grading
session. You will receive an email notifying you of the name of the TA who has been
assigned to you for this assignment with further instructions on setting up the
appointment. (You will be meeting with a different TA for each assignment). One-on-one
interactive grading will help you improve your programming skills and avoid repeating
mistakes from one assignment to the next.