CSC 212L – Data Structures

advertisement
CSC 212L – Data Structures
Mastery Exam
Directions:
1)
This exam is to be done individually and is open book and open notes, but closed
Internet. Any communication between students is considered de facto cheating
and any students involved will automatically fail this lab. Any questions you may
have should be directed to the instructor.
2)
Read the instructions for each problem carefully. Questions explicitly state the
code you need to write. If you are unsure about whether you need to write
something, ask the instructor.
3)
You should start by going to
http://cs/~hertzm/csc212f06/MasteryExam You will find 2
subfolders there: question1, and question2. Each of these directories has
.class files provided by the instructor (including the file for a class named Tester
that tests your solution) and any needed data files. You may be asked to write
Java files from scratch.
4)
Each question is independent and you can solve the problems in any order you
wish. Since the questions are weighted equally, you may wish to solve the
easiest/quickest problems first.
5)
To use the jar file in Eclipse:
* Start by importing the jar file into your project
* Right click on the jar file and select Build Path  Add to Build
Path
6)
To run the Tester class from within Eclipse:
* Click on the plus sign to open the jar file
* Click on the plus sign to open the package labeled “(default package)”
* Right click on the Tester.class and select: Run As  Java
Application
7)
There are different levels of completion of each question: it compiles, it runs but
crashes, it runs without crashing but gets the wrong answer, and it runs and gets
the correct answer. These are worth 20%, 15%, 15%, and 40%, respectively.
This means that execution alone is worth 90% of the credit. The remaining 10%
of the problem grade will be earned for submitting a formal testing plan with at
least 1 test (10%). Commenting your code is appreciated, but will not be graded.
You can only earn credit for code you modify and submit.
8)
At the end of class, e-mail your .java file solutions to me.
Question #1 (use files from the question1 directory)
In this question, you will complete a class that implements a Map using an unordered
Sequence. In particular, you will be implementing the get() method and adding a
toString() method. For full credit on this problem, you will need iterate (e.g., use
an iterator) through the elements of the Sequence. Working solutions that do not use an
Iterator can receive a maximum of 80% on this problem. The algorithms you will
implement are:
algorithm get(K key)
Iterate through each Entry in seq
K entKey = current entry’s key
V entValue = current entry’s value
if entKey.equals(key)
return entValue
end if
end loop
return null
algorithm toString()
String str = “”;
Iterate through each Entry in seq
E ent = current entry
str = str + ent.toString()
end loop
return str
You will fill in these methods in the MapMaster class I am giving you. The method
signatures are already written; you only need to replace the TODO comment with your
code. You do NOT need to write anything other than the two method bodies.
The Tester class contains a main method that generates an interesting Map of data and
then uses your methods to print out results. The instructor’s solution printed out:
First test passes
Second test passes
Third test passes
Fourth test passes
String representation of Map:
Report -> minus the 'T'
Colbert -> Stephen
The -> usually listed last
on -> on
'The Word' -> usually amusing
by -> purchase <sic>
inspired -> blantantly stolen
Bit -> concept
Question #2 (use files from the question2 directory)
Write a class PalindromeString that uses recursion to determine whether a
String is a palindrome – would be read the same either forwards or backwards.
Algorithmically a String str is a palindrome if and only if:
str.charAt(i) == str.charAt(str.length() – (i + 1)), for all i <
str.length()
Your class does not need to implement any interfaces, define any fields, or implement a
constructor. It must implement the following method, however:
public boolean isPalindrome(String str)
isPalindrome() should return true if str is a palindrome, and return false
otherwise. You may implement other private methods as you see fit.
The Tester class contains a main method that creates several instances of String and
then uses your class to test if they are valid. The instructor’s solution printed out:
1st test passes:
2nd test passes:
palindrome
3rd test passes:
4th test passes:
palindrome
'otto' is a palindrome
'madam I'm adam' (less spaces and apostrophe) is a
'madam I am adam' (minus spaces) is not a palindrome
'I can't believe this is not a palindrome' is not a
Download