Fund. CS II (CS152 2005S)

Exam 1: Java Fundamentals

Distributed: Monday, February 28, 2005
Due: 11:00 a.m., Friday, March 4, 2005
No extensions.

This page may be found online at



There are three problems on the exam. Some problems have subproblems. Different problems have different point values. 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 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.

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 80 points on this exam. I would 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. 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. 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 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. Doing so will earn you two points of extra credit.

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.

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.


Problem 1: Templates [30 points]

As you may have noted, many of our main classes have some similarities (e.g., we almost always create a PrintWriter and usually create a BufferedReader; most of the time, they use the keyboard and screen, at least in the first version).

In spite of this similarity, most of you seem to start writing each main class from scratch. Former CS152 students Ted and Teena Template suggest that there's an alternative: You can write a generic version of the main class (e.g., that includes all the things that a typical main class has and make a copy every time you need a new main class.

a. Write a good that includes all the things you think I expect to see in the typical main class.

Ted and Teena also suggest that they think many of you spend too much time looking through the various documentation for various instructions (e.g., creating a file) and suggest that you create a short reference that lists the common things you want to do and how to do them.

b. Pick five things that you have to look up (or that you think your classmates have to look up) and write a short reference.

By short, I mean that you should write a sentence or phrase for the purpose (Create a file for output) and then give a template of the code.

Problem 2: A Piggy Bank [40 points]

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 2a: Primary Methods [20 points]

Write a simple PiggyBank class that includes the following methods:

Problem 2b: Shaking the Bank [10 points]

As you analyze the problem some more, you realize that many Grinnell students will need to shake their Piggy Banks to remove some money for the essentials of everyday life. To accommodate such situations, you should write a method that randomly selects a few coins from the jar, computes the total value of those coins, removes them, and returns the total value.

Write this int shake() method.

Note that you may find either Math.random or java.util.Random useful.

Problem 2c: Compound Deposits [10 points]

After some time using your class, your clients suggest that they would much rather deposit a bunch of coins all at once. 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 use the following assumptions:

Note that we can apply these recursively. For example, for 73 cents:

Implement the deposit method using those assumptions.

Problem 3: A Fractional Calculator [30 points]

We've spent some time in the class implementing a Fraction class. Now that we've written the class, it's time to do something a little bit interesting with it. In particular, we can implement a simple calculator.

The calculator keeps track of the last value it calculated or that was entered. If the user has not stored any values, it uses 0.

Most typically, the user enters an operation, a carriage return, a new value, and another carriage return. The calculator then computes the results of applying that operation to the stored value and the new value. It stores that value and prints the result. The valid operations are add (plus sign, +), subtract (minus sign, -), multiply (star, *), and divide (slash, /).

For example, if the stored value were 1/2 and the user entered a plus sign, a carriage return, the value 2/3, and a carriage return, the calculator would add 1/2 and 2/3 (giving 7/6), remember the 7/6, and print the 7/6. If the user next entered a multiplication sign (a star) and 3, the calculator would multiply 7/6 by 3 (giving 7/2), remember the 7/2, and print the result.

There are a few special cases to consider:

For example, here is a sample session with the calculator. I've used a greater-than sign for a prompt, but you can use anything you deem appropriate.

> =
> +
> 3/4
> - 
> 1/2
> c
> 2/3
> +
> 1/2
> /
> 7
> 2/6
> Q

Note that we always store fractions in simplified form.

Write a main class, FractionalCalculator, that implements the calculator described above.

Some Questions and Answers

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

I note that in Problem 3, you print the first result as 0, rather than 0/1. Should we do the same?
Yes, that would be nice. If the fraction is in simplified form, the toString method need only check if the denominator is 1.
I note that in Problem 3, you permit fractions with no denominator as input. Must we do the same?
Yes. Determine whether a slash is in the input strign and, if it is not, use 1 as the denominator.
I note that in Problem 3, you only entered good responses. What should we do if the user enters bad responses.
Whatever you want.
Must we do a tester for problem 2?
No, but it will help.
Should we turn in our tester (if we write one) for problem 2?
Yes. In increases the odds of you getting at least partial credit.
Will we need to update our Fraction class to do problem 3?
Probably, since I've asked you to do other operations.
Should we turn in our updated Fraction class?
What does a few mean in Problem 2?
Anything reasonable you'd like it to mean. It should be greater than 0, assuming that there are coins in the bank.
What should shake do, if , say, there are no quarters?
Um, return only dimes, nickels, and pennies.
In problem 2, how should we report the amount in the bank?
In cents is fine.
Do we need to support negative fractions in problem 3?


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.



Friday, 25 February 2005 [Samuel A. Rebelsky]

Sunday, 27 February 2005 [Samuel A. Rebelsky]

Tuesday, 1 March 2005 [Samuel A. Rebelsky]

Wednesday, 2 March 2005 [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 Wed May 11 10:55:14 2005.
The source to the document was last modified on Wed Mar 2 11:04:12 2005.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,