Fund. CS II (CS152 2006S)

Exam 1: Java Fundamentals

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

Preliminaries

There are four problems on the exam. Some problems have subproblems. Each problem is worth twenty-five (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 take-home 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 (269-4410) or at home (236-7445).

I will also reserve time at the start of classes next week to discuss any general questions you have on the exam.

Problems

Problem 1: Constructing Fractions

Topics: Constructors, computing with BigInteger values, divide-and-conquer 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 non-negative.  */
  BigInteger denominator;

In class, we considered how we might write a constructor that takes a double value as a parameter:

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:

You need only submit the commented code for the constructor.

Problem 2: Comparing Vectors in Two-Space

Topics: Equality testing, comparing decimals, approximate equality.

In the reading on writing your own classes, we considered a Vec2D class that represents vectors in two-space. 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.

Problem 3: A Piggy Bank

Topics: user-defined 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 twenty-first 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.)

Problem 3a: Primary Methods

Write and test a simple PiggyBank class that includes one zero-parameter constructor and the following methods:

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.

Problem 3b: Compound Deposits

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 highest-valued 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 amount-25.

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.

Problem 4: Cost of Approximate String Matching

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.

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.

Problem 4a: Implement editCost

Implement and test a method, editCost(String source, String target) that matches the algorithm above.

Here are some simple examples

Problem 4b: The Cost of Computing the Cost of Editing

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.

Some Questions and Answers

These are some of the questions students have asked about the exam and my answers to those questions.

Do we have to turn in the exam all at once, or can we turn it in a piece at a time?
You can do whatever you want, but I'll only grade the last thing you send me.
Why doesn't my code compile?
Because you've used incorrect syntax.
Why doesn't my code work correctly?
Because you seem to have misunderstood how to express what you want it to do.
Why don't I get any output from my program?
You probably have an infinite loop.
Why do I get a stack overflow?
You have nonterminating (or very deep) recursion.
What values should I use for INSERTION_COST and DELETION_COST?
Whatever you'd like, within reason. 2 and 3 are good values.
How sophisticated does the user interface for piggybanks have to be?
You will not lose points for a non-sophisticated interface, but you may gain points for a really nice one.
Can we copy your Vec2D code?
Yes.

Errors

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.

 

History

Wednesday, 22 February 2006 [Samuel A. Rebelsky]

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]

 

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 ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu