Fund. CS II (CS152 2005F)

Exam 1: Java Fundamentals

Distributed:
Due: 11:00 a.m., Friday, 30 September 2005
No extensions.

This page may be found online at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005F/Exams/exam.01.html.

Contents

Preliminaries

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 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 80 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: Vectors, Revisited [40 points]

In our initial explorations of user-defined classes in Java, we started to write a two-dimensional vector class, Vec2D. Unfortunately, although we planned a wide variety of methods for that class, we implemented only a few. In this problem, you will complete the implementation and write a simple tester.

Problem 1a: A String-Based Constructor [10 points]

Topics: User-defined classes, constructors, simple parsing, testers.

Create the constructor Vec2D(String str), which builds a new Vec2D by parsing str, which should have the form "(double,double)".

Make sure that this constructor throws an exception if the string has the wrong form.

Problem 1b: Scaling Vectors [10 points]

Write a Vec2D scale(double amount) method that computes a new vector that represents the original vector scaled by amount.

Problem 1c: Rotating Vectors [10 points]

Write a Vec2D rotate(double angle) method that computes a new vector that represents the original vector rotated by angle radians.

Problem 1d: Testing Vectors [10 points]

Write an interactive tester for Vec2D. Your method should always have a current working vector and allow the user to enter a command and something to do with that vector. The legal options are:

After each step, you should print out the current working vector. When the program starts, the current working vector should be (1,0).

Here is a sample session (with slightly fudged results):

CWV: (1.0,0.0)
Command? rotate 3.1415
CWV: (-1.0,0.0)
Command? add (-2.0,2.5)
CWV: (-3.0,2.5)
Command? scale 4.5
CWV: (-13.5,11.25)
Command? set (1.5,1.5)
CWV: (1.5,1.5)
Command? quit
Bye!

Problem 2: MadLibs, Revisited [30 points]

Topics: User-defined classes, string manipulation, files, input and output

In your first homework assignment, you implemented a simple MadLibs program. You now know significantly more about Java, and can rewrite this program so that (a) it deals with an arbitrary number of lines and an arbitrary number of blanks per line and (b) it represents each MadLib as an object, rather than dealing with a series of strings.

Write and test a class, MadLib, that meets the following criteria:

For example, here's a program that plays MadLibs thrice using the file latetemp:

package username.exam1;

import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.File;

public class LateTemp
{
  public static void main(String[] args)
    throws Exception
  {
    PrintWriter pen = new PrintWriter(System.out, true);
    BufferedReader eyes = new BufferedReader(new InputStreamReader(System.in));
    File f = new File("latetemp");
    MadLib game = new MadLib(f);
    game.play(pen,eyes);
    game.play(pen,eyes);
    game.play(pen,eyes);
  } // main(String[])

} // class LateTemp

Problem 3: A Piggy Bank [30 points]

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 [15 points]

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 [15 points]

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 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.

Some Questions and Answers

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

How do I create a new vector using polar coordinates?
With an instruction like Vec2D zebra = Vec2D.polar(5, 3.14/4);
Is there any particular way you'd like us to test the PiggyBank problem? Can we just write a main method that adds a variety of values, removes a variety of values, and then checks the result, or should we write something that interacts with the user?
As long as you test comprehensively, I don't care what you do.
Why does getTotal() return a long instead of a double, given that the amount in the bank is likely to include some fractions of a dollar?
As I've suggested before, doubles are approximations. It's clearly bad to approximate the amount of money someone has deposited in a bank. Hence, have getTotal() return the total cents deposited.
Should we provide an exception for every method, or just those that you specifically ask for? I know that proper form would dictate providing exceptions for every method, but I didn't know exactly what you wanted. For example, for 3a do I need to have exceptions for the removeCoin and the toString and getTotal, or just the removeCoin methods)?
You should only have exceptions for methods that have a clear mode of failure. The remove methods can all fail if there aren't enough coins. Other than catastrophic failure of the computer, there are no reasons for toString or getTotal to fail.

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

Thursday, 22 August 2005 [ Samuel A. Rebelsky]

Friday, 23 August 2005 [Samuel A. Rebelsky]

Sunday, 25 August 2005 [Samuel A. Rebelsky]

Monday, 26 August 2005 [Samuel A. Rebelsky]

Tuesday, 27 August 2005 [Samuel A. Rebelsky]

Wednesday, 28 August 2005 [Samuel A. Rebelsky]

Friday, 30 August 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 Tue Dec 6 09:46:51 2005.
The source to the document was last modified on Fri Sep 30 09:51:32 2005.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005F/Exams/exam.01.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu