[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
[CS152 2005S]
[CS152 2005F]
Distributed: Friday, February 24, 2006
Due: 11:00 a.m., Monday, March 6, 2006
No extensions.
This page may be found online at
http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Exams/exam.01.html
.
Contents
There are four problems on the exam. Some problems have subproblems. Each problem is worth twentyfive (25) points. The point value associated with a problem does not necessarily correspond to the complexity of the problem or the time required to solve the problem.
This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than as restricted by that limitation, you should feel free to use all reasonable resources available to you. As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately.
Although you may use the Web for this exam, you may not post your answers
to this examination on the Web (at least not until after I return exams
to you). And, in case it's not clear, you may not ask others (in person,
via email, via IM, by posting a please help
message, or in any
other way) to put answers on the Web.
This is a takehome examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.
I expect that someone who has mastered the material and works at
a moderate rate should have little trouble completing the exam in a
reasonable amount of time. In particular, this exam is likely to take
you about four to six hours, depending on how well you've learned topics
and how fast you work. You should not work more than eight hours
on this exam. Stop at eight hours and write There's more to life
than CS
and you will earn at least 75 points on this exam.
I would also appreciate it if you would write down the amount of time each problem takes. Each person who does so will earn two points of extra credit. Since I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they've spent at least five hours on the exam or completed the exam. (At that point, I may then change the exam.)
You must include both of the following statements on the cover sheet of the
examination. Please sign and date each statement. Note that the
statements must be true; if you are unable to sign either statement,
please talk to me at your earliest convenience. You need not reveal
the particulars of the dishonesty, simply that it happened. Note also that
inappropriate assistance
is assistance from (or to) anyone
other than Professor Rebelsky (that's me).
1. I have neither received nor given inappropriate assistance on this examination.
2. I am not aware of any other students who have given or received inappropriate assistance on this examination.
Because different students may be taking the exam at different times,
you are not permitted to discuss the exam with anyone until after I
have returned it. If you must say something about the exam, you are
allowed to say This is among the hardest exams I have ever
taken. If you don't start it early, you will have no chance of
finishing the exam.
You may also summarize these policies.
You may not tell other students which problems you've finished.
You may not tell other students how long you've spent on the exam.
You must both answer all of your questions electronically and turn in a printed version of your exam. That is, you must write all of your answers on the computer, print them out, number the pages, put your name on every page, and hand me the printed copy. You must also email me a copy of your exam by copying the various parts of your exam and pasting it into an email message. Put your answers in the same order as the problems. Please write your name at the top of each sheet of the printed copy. Failing to do so will lead to a penalty of two points.
In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code.
Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.
I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by emphasizing your answer and including a clear set of work that you used to derive the answer.
I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (2694410) or at home (2367445).
I will also reserve time at the start of classes next week to discuss any general questions you have on the exam.
Topics: Constructors, computing with BigInteger
values, divideandconquer strategies, estimation.
In the laboratory on building
your own classes, you experimented with a Fraction
class that included two fields, a numerator and a denominator.
/** The numerator of the fraction. Can be positive, zero or negative. */ BigInteger numerator; /** The denominator of the fraction. Must be nonnegative. */ BigInteger denominator;
In class, we considered how we might write a constructor that takes
a double
value as a parameter:
Fraction
slightly larger than the double
.
For example, you could round the double
up to the next
largest integer, and use that as a numerator and 1 as a denominator.
Fraction
slightly smaller than the
double
. Similarly, you could round the double
down to an integer.
We can do the repetition using recursion or iteration. We stop when the
two estimates are close enough
. (Close enough is probably
within 2^{10} or so.)
Implement and test this constructor.
Here are some values you might want to test:
Math.PI
You need only submit the commented code for the constructor.
Topics: Equality testing, comparing decimals, approximate equality.
In the reading on writing your
own classes, we considered a Vec2D
class that represents
vectors in twospace. That class had two fields:
/** The length of the Vector. */ double radius; /** The angle of the vector from the positive x axis. */ double theta;
That class lacks an equals(Object other)
method.
Write the equals
method. The method
should return true if other
is also a
vector in two space that is close to the current vector and return
false otherwise. Two vectors are close if the distance of their
points is within 1/100 of the average of their lengths.
Topics: userdefined classes, arithmetic, exceptions, conditionals, repetition/recursion
With Grinnell's recent raise in tuition, many students will now be pinching pennies to have enough to afford to return to Grinnell next year. Trudy and Trueheart Trustee suggest that, as long as we're in Iowa, these students use a Piggy Bank to store those funds. (Yes, it will need to be a pretty big Piggy Bank.)
Of course, now that it's the twentyfirst century, it makes sense to embed a microprocessor in the Piggy Bank that can keep track of how much is in the bank. Believe it or not, it's your responsibility to build that microprocessor.
For the purposes of this exam, you will write a class PiggyBank
that keeps track of how many of each coin are in the bank. (You
need only keep track of quarters, dimes, nickels, and pennies.)
Write and test a simple PiggyBank
class that includes one
zeroparameter constructor and the following methods:
void insertQuarter()
void insertDime()
void insertNickel()
void insertPenny()
void removeQuarter()
void removeDime()
void removeNickel()
void removePenny()
long getTotal()
String toString()
, which returns a string of the form "Q quarters, D dimes, N nickels, P pennies"
.
You can assume that the total number of any coin is never greater
than Integer.MAX_VALUE
. You can also assume that the
total value in the bank is never greater than Long.MAX_VALUE
.
You should represent the total value in cents, rather than in dollars.
You should throw an exception if the user attempts to remove a value of coin not in the bank.
After some time using your class, your fellow students suggest that
they would much rather deposit a bunch of coins
all at once, rather
than one by one. Hence, you must now write a void deposit(int
amount)
method.
Of course, for any particular amount, there are many ways to make up that amount. You should assume that students preferentially deposit the highestvalued coin for any value. In particular, use the following assumptions:
Note that we can apply these recursively. For example, for 73 cents:
Implement the deposit
method using those assumptions.
As the example suggests, I would recommend that you implement
deposit
recursively. That is, check if the number is
greater than 25. If so, increment the count of quarters by one, and
then deposit amount25
.
Make sure that you include some tests for this method in your exam. That is, run the tests, print them out, and copy and paste them into the email message you send to me.
Topics:: String matching, algorithm analysis, static fields, constants
In a homework assignment, I asked you to write a program that finds the closest match of a set of strings to a target string. As part of that program, you may have written a helper method that assigns a closeness to two strings.
Here's one strategy for evaluating how the cost of editing one string (the source) to convert it to another (the target). A string is a better match if this cost is lower.
INSERTION_COST
times the number of characters in target. That is, to convert the source to the target, we'll need to insert all the characters in the target.
DELETION_COST
times the number of characters in source. That is, we'll need to delete all the characters in source.
DELETION_COST
plus the cost of converting all but the first character of source to target.
INSERTION_COST
plus the cost of converting source to all but the first character of target.
In the last case, we're not sure which strategy to apply, so we try both and use whichever one works better.
Note that we are using constants for the cost of deleting a character from the source and the cost of inserting a character into the source in case those two actions cost different amounts.
Implement and test a method,
editCost(String source, String target)
that matches
the algorithm above.
Here are some simple examples
editCost("Sam", "Samuel")
: 3*INSERTION_COST
editCost("SamR", "Sam")
: 1*DELETION_COST
editCost("Samuel", "SamR")
: 3*DELETION_COST
+ 1*INSERTION_COST
While editCost
is fairly straightforward, it is not very
efficient. Update your method to keep track of the number of recursive
calls you make. (That is, create a static field that you increment each
time you call editCost
.)
i. How many calls do you make for two identical strings of length 5?
ii. How many calls do you make for two completely different strings, each of length 5?
iii. How many calls do you make for two strings of length 5 that differ only in the last character?
iv. How many calls do you make for a source of length six and a target of length five that is created by deleting the first character of the source?
Submit both your code and your answers to these questions.
These are some of the questions students have asked about the exam and my answers to those questions.
INSERTION_COST
and DELETION_COST
?Vec2D
code?Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. I limit such extra credit to five points.
Math.arctan
when
Java only supplies Math.atan
[ST, 1 point]
should returns truerather than
should return true. [TIA, 1 point]
Wednesday, 22 February 2006 [Samuel A. Rebelsky]
Fraction(double)
constructor and Vec2D.compareTo(Vec2D)
).
Thursday, 23 February 2006 [Samuel A. Rebelsky]
Monday, 27 February 2006 [Samuel A. Rebelsky]
Tuesday, 28 February 2006 [Samuel A. Rebelsky]
Monday, 6 March 2006 [Samuel A. Rebelsky]
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
[CS152 2005S]
[CS152 2005F]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue May 9 08:30:56 2006.
The source to the document was last modified on Mon Mar 6 08:26:39 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Exams/exam.01.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu