Computer Science Fundamentals (CS153 2004S)

Exam 3: Java Fundamentals

Distributed: Wednesday, 14 April 2004
Due: 10:00 a.m., Wednesday, 21 April 2004
No extensions.

This page may be found online at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Exams/exam.03.html.

Contents

Preliminaries

There are five problems on the exam. Some problems have subproblems. Each full problem is worth twenty 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 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, or by posting a please help message) 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. 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 myself or our teaching assistant.

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.

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.

You should document all of your primary procedures. In most cases, a few sentences will suffice. In a few cases, I'll ask you to provide the full documentation (including parameters, purpose, value produced, preconditions, and postconditions). If you write helper procedures (and you may certainly write helper procedures) you should document those with a few short notes. When appropriate, you should include short comments within your code. You should also take care to format your code carefully.

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 this week and next to discuss any general questions you have on the exam.

Problems

Problem 1: Computing Averages

Carl and Carla Caffeinated have received an assignment to

Build a program that averages numbers entered on the command line. Your program should ignore values entered that are not numbers.

Their solution is Average.java, which follows.

/**
 * Compute the average of a list of numbers entered on the command
 * line.  For example, to compute the average of a, b, and c, you
 * would write
 *   % java Average a b c
 *
 * @author Carla Caffeinated
 * @author Carl Caffeinated
 * @author Samuel A. Rebelsky
 * @author YOUR NAME HERE
 * @version 1.1 of April 2004
 */
public class Average {
  public static void main(String[] args) {
    int sum = 0;	// The sum of all the values entered.
    int count = 0;	// The number of values entered.
    SimpleOutput out = new SimpleOutput();
    
    // Step through the values, adding each in turn.  Skip those
    // that aren't valid.
    for (int i = 0; i < args.length; i = i+1) {
      try {
        sum = sum + Integer.parseInt(args[i]);
        count = count + 1;
      }
      catch (NumberFormatException nfe) {
        // Skip it.
        i = i + 1;
      }
    } // for
  
    // Print the average.
    out.println("The average is: " + (sum/count));
  } // main(String[])
} // class Average

In all their tests, the program seems to work fine

% java Average 3 4 5
The average is: 4
% java Average 5 4 3
The average is: 4
% java Average 10 100
The average is: 55
% java Average -10 20
The average is: 5
% java Average -10 aardvark zebra 20
The average is: 5
% java Average 10.5 9.5 10
The average is: 10

Unfortunately, they've made the mistake of turning in their program without enough testing. They get their program back with a D and the note that

I'm sure that the average of 1, 1, and 3 is closer to 1.67 than 1. I'm also sure that the average of 0, 100.1, and 200.2 is not 0. Please think more about possible problems and errors.

The Caffeinateds return their code and, sure enough, when they try the examples, things work oddly.

% java Average 1 1 3
The average is: 1
% java Average 0 100.1 200.2
The average is: 0

a. Indicate the likely cause or causes of those errors.

b. Repair the parts of the code that caused the errors.

c. Depending on how you dealt with the previous errors, there is at least one (and possibly more) logical errors in their program which will lead it to behave inappropriately. Identify those errors and update the code so that it behaves correctly.

Problem 2: Expandable Arrays

Saul and Sarah Structure are upset that the arrays that come with Java are not dynamic. They would like to have a variant of arrays that automatically expand when necessary whenever you set a value. They have decided that the following methods are essential:

After further reflection, Saul and Sarah suggest that you should also support

Implement and test this new DynamicArray class.

Problem 3: Ordering Things

Andy and Andrea Analyst have been working with a number of problems that have to do with objects that can be put in order. For example, they've noted that they can order numbers, areas, and even students (who might be ordered by student id number).

At the same time, they've decided that there are times when you want to order the same kinds of objects in different ways. For example, you might order students by id number, by name, or by grade point average.

Putting all of their thoughts together, they've come up with a number of classes or interfaces they want to build.

a. Which of the methods described above should throw an exception? When should it do so?

b. Write code for Order, Student, OrderStudentsByGPA, and OrderStudentByID.

Problem 4: Selection Sort

As you may recall, the selection sort algorithm sorts an array by repeatedly stepping through subsections of the array, identifying the smallest element in the subsection, and swapping that element into the beginning of the subsection.

a. Implement that algorithm using DynamicArray and Order from the previous problems.

b. Test that algorithm using Student, OrderStudentByGPA, and OrderStudentByID from the previous problems.

Problem 5: Anonymous Inner Classes

Ben and Beth Beginner have mastered the basics of Java. They are now ready to learn some advanced techniques and have turned to you for help. They are particularly confused about the anonymous inner classes that I described briefly. To help them, you will clearly have to look up these things (in books, on the Internet, or ...).

a. Explain, in a few sentences, what they are.

b. Using any or all of the previous problems as a starting point, show how anonymous inner classes might be useful to the Java programmer.

Some Questions and Answers

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

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

Tuesday, 13 April 2004 [Samuel A. Rebelsky]

Wednesday, 14 April 2004 [Samuel A. Rebelsky]

Friday, 16 April 2004 [Samuel A. Rebelsky]

Monday, 19 April 2004 [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 Fri May 7 09:43:54 2004.
The source to the document was last modified on Mon Apr 19 10:40:14 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Exams/exam.03.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu