Computer Science Fundamentals (CS153 2003S)

Exam 3: Java Fundamentals

Distributed: Friday, 11 April 2003
Due: 10 a.m., Tuesday, 22 April 2003
No extensions!

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

Contents

Preliminaries

There are four problems on the exam. Some problems have subproblems. Each full problem is worth twenty-five 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. If you write down the amount of time you spend on each problem and the total time you spend on the exam, I'll give you two points of extra credit.

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. It is likely to take you about five to ten hours, depending on how well you've learned topics and how fast you work. 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 three points of extra credit to the first two people who honestly report that they've spent at least seven hours on 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. 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 them in in hardcopy. That is, you must write all of your answers on the computer, print them out, and hand me the printed copy with your name and the page number on every page. If you fail to write your name on every page, I will penalize you five points. 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. Make sure that your solution conforms to the format for laboratory writeups (except that you should not specify the location of your file).

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 fully document all of the primary procedures (including parameters, purpose, value produced, preconditions, and postconditions). If you write helper procedures (and you may certainly write helper procedures) you should document those, too, although you may opt to write less documentation. 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, 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 this week and next to discuss any general questions you have on the exam.

Problems

Problem 1: Defining Polymorphism

Paula and Paul Perplexed have pondered my definition of polymorphism, the ability to define a procedure once and call it on a variety of related values and feel strangely unsatisfied. They don't quite understand my definition and they'd like to hear alternatives.

Find four definitions of polymorphism (other than my own), at least two of which must be taken from traditional printed sources (e.g., dictionaries, textbooks, journal articles). Suggest how they are similar and how they are different. Using those definitions, come up with your own definition of polymorphism.

Problem 2: Things You Can Compare

Andy and Andrea Analyst have been working with a number of problems that have to do with things 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 things 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 these things should be classes and which should be interfaces? In each case, explain your decision with a sentence or two.

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

c. Write code for Student and OrderStudentsByGPA.

Note that you only need to include the methods and fields appropriate for this assignment.

You need not write working code for this part of the assignment. Simply convince me that you could do so if given sufficient time.

Problem 3: Simplification

Fillip and Fiona Fictitious are fooling with the Rational class that we defined together. Fillip and Fiona are somewhat frustrated that their fractions fail to appear in simplified form. For example, when they add 1/5 and 3/10, they get 25/50 rather than 1/2. Similarly, when they multiply 2/3 and 9/4, they get 18/12 rather than 3/2.

Hence, they've decided to extend Rational by adding a simplify method to the class. They've also updated the constructor to use that method.

  public Rational(int num, int denom)
  {
    this.numerator = num;
    this.denominator = denom;
    this.simplify();
  } // Rational(int, int)

Fiona and Fillip aren't sure how to simplify rational numbers, so they ask their friends, Myra and Myron Mathemagician. Myra and Myron reply that You need to find the greatest common divisor of the numerator and denominator. You can use Euclid's method to find the GCD.

The Mathemagicians also realize that their friends may not know this legendary algorithm, so they repeat it.

The greatest common divisor of two positive numbers, X and Y, with X larger than Y, is either (1) Y, if Y evenly divides X, or (2) the greatest common divisor of Y and the remainder you get when you divide X by Y, if Y does not evenly divide X.

Fiona and Fillip start their implementation with.

  public void simplify()
  {
    int common;	// The greatest common divisor
    int num = this.numerator;   // Substitute for numerator.  See below.
    int denom = this.denominator;
    // Compute the greatest common divisor.
    // If numerator or denom is negative, make it positive for the
    // purposes of GCD, since Euclid said "two positive numbers"
    if (num < 0) {
      num = -num;
    }
    if (denom < 0) {
      denom = -denom;
    }
    common = gcd(num, denom);
    // Divide numerator and denominator by the gcd.
    this.numerator = this.numerator / common;
    this.denominator = this.denominator / common;
  } // simplify()

Unfortunately, that's about as far as they can get. They know how to set up the gcd method, but can't tell how to turn Euclid's claim into code. Since 1 divides everything, they've used it for their stub.

  protected static int gcd(int x, int y)
  {
    // STUB
    return 1;
  } // gcd(int,int)

Fill in the body of the gcd method. Make sure that your method works correctly.

Note: In Java, you can compute remainders with %.

Problem 4: Exponentiation

Irma and Irving Iterator are irritated at the efficient recursive exponentiation procedure that you should recall from our work in Scheme. They say,

Every recursive procedure can be implemented iteratively. Let's rewrite the recursive exponentiation procedure iteratively, so that it is not only equally efficient in Big-O terms, but also more efficient in practical terms since it avoids a large number of procedure calls.

Unfortunately, they're not quite sure how to actually implement this procedure. Guess what? It's your responsibility. Irma and Irving find a hint on the Web: To compute bn

Define variables x, y, and z such that xyz = bn. Each time through the loop, decrease y while updating x and z so that the equality is maintained.

Errors

These are the errors observed by students. Since I have threatened to take off for grammatical or spelling errors, I give the class one point of extra credit for each such error they notice in this exam. Such extra credit is capped at five points.

Questions and Answers

Should we document using Javadoc or the six P's?
Both, of course. That is, write the six P's in a Javadoc block.
For problem 4 (exponentiation) do we need to use the hint that you gave us?
No, as long as your implementation is not recursive and runs in O(log2n) time.
For problem 4 (exponentiation) do we need to handle negative exponents?
No.

 

History

Wednesday, 9 April 2003 [Samuel A. Rebelsky]

  • Created.

Thursday, 10 April 2003 [Samuel A. Rebelsky]

  • Finishing touches.

Sunday, 13 April 2003 [Samuel A. Rebelsky]

  • Added errors sections and fixed two errors.
  • Added questions and answers section.

Tuesday, 15 April 2003 [Samuel A. Rebelsky]

  • Fixed a typo in the question and answer section.

 

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 6 09:19:06 2003.
The source to the document was last modified on Mon Apr 28 23:56:04 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Exams/exam.03.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu