Fundamentals of Computer Science II (CSC-152 2000F)


Exam 1: Java Fundamentals

Distributed: Friday, September 15, 2000
Due: 11 a.m., Friday, September 22, 2000
No extensions!

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

Code files:

You may want to use the online version of the exam to gain access to these files.

Preliminaries

There are ten problems on the exam. Seven of the problems (1, 2, 5, 6, 7, 8, and 10) are required. Each required problem is worth 15 points. Three of the problems (3, 4, and 9) are optional. Each optional problem is worth 2 points.

For convenience, the questions are divided into sections. 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 question, I'll give you three points of extra credit.

This examination is open book, open notes, open mind, open computer, open Web. Feel free to use all reasonable resources available to you except for other people and for exam and homework solutions. 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. Do not use exam or homework solutions you find on the Web.

This is a take-home examination. It is likely to take you about five to ten hours, depending on how well you've learned topics and how fast your work. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date. No late exams will be accepted.

You must include the following statement on the cover sheet of the examination. Plesae sign and date the statement. Note that the statement must be true; if you are unable to sign the statement, please talk to me at your earliest convenience.

I have neither given nor received help on this examination. I am not aware of any other students who have given or received help 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'll have no chance of finishing the exam.'' You may also summarize these policies.

Answer all of your questions electronically and turn them in in hardcopy. That is, you must write all of your answers on the computer and print them out. You should also email me a copy of your exam (a plain text file, please). Put your answers in the same order that the problems appear in. If you write ``have a happy day'' at the end of your exam, I'll give you two points 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 yo u 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

A. Correcting and Commenting Code

At the start of the semester, I handed out some standards for Java programming. Henry and Hermione Hacker don't think that they need to follow those standards. However, they've learned that I won't grade homework that doesn't meet my standards. Unfortunately, they don't know where to begin.

How bad is their code? Here's the first class that they turned in; an implementation of an UglyPoint class.


public class UglyPoint{
  public double distanceFromOrigin() {
    return Math.sqrt(this.x
      *this.x + this.y *
this.y);
}

    public UglyPoint(double x, double y) {
      this.x = x; this.y = y; 
    }

protected double x;

  public void set(double newX, double newY) {
    this.x = newX;
    this.y = newY;
  }

  public String toString()
  { return "(" + this.x + "," + this.y + ")"; } public double getX() {
    return this.x;
  }

  public double getY
  () {
    return this.y;
  }

    public UglyPoint() { this.x = 0; this.y = 0; } protected double y;

  public int getQuadrant()
  { if (
      (this.x > 0) && (this.y > 0)
         ) { return 1; } else
    if ((
      this.x < 0
    ) && (
      this.y > 0
       )) { return 2;
}else
    if((this.x < 0)&&(this.y<    0)
    ) { return 3;

}
    else
      if ((this.x > 0) && (this.y < 0)) { return 4; }
                   else {
return 0; }}}
    


1. Cleaning

Help them turn their UglyPoint.java into something that is more readable.

You should certainly add comments, fix the badly formatted code, add whitespace where appropriate, and reorganize the code so that similar things are near each other. Your goal is to create a class that is likely to meet my high standards of indentation, organization, uniformity, and commenting.

Make sure to use the existing code (which does work); do not rewrite from scratch.

2. Testing

Write a test program to ensure that their UglyPoint works correctly.

B. Definitions

3. Encapsulation

This problem is optional.

Find three definitions of encapsulation (other than my own). Suggest how they are similar and how they are different. Using those definitions, come up with your own definition of encapsulation.

4. Inheritance

This problem is optional.

Find three definitions of inheritance (other than my own). Suggest how they are similar and how they are different. Using those definitions, come up with your own definition of inheritance.

5. Polymorphism

Find three definitions of polymorphism (other than my own). Suggest how they are similar and how they are different. Using those definitions, come up with your own definition of polymorphism.

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

6. Initial Analysis

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 if the methods described above should throw an exception? When should it do so?

7. Coding

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.

D. Fractions

Fillip and Fiona Fictitious are fooling with the Fraction class that we defined in homework 1. They'd like to extend the class to provide more capabilities.

8. Simplification

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 Fraction by adding a simplify method to the class. They've also updated the constructor to use that method.

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

They've asked some math majors, and they say that to simplify a fraction, you need to find the greatest common divisor of the numerator and denominator and that you can use Euclid's algorithm to help find it.

The math majors have told them that Euclid said something like:

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.

So, Fiona and Fillip started 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 %.

9. Static methods

An optional problem.

Explain to the best of your ability why gcd is both static and protected.

10. An unforeseen complication

As they incorporate your helpful suggestions, Fillip and Fiona notice that something odd has happened to their class. While they haven't changed the add method, the result they get is different. For example, when they used to add 1/9 and 2/9, they got 3/9. Now they get 1/3. Explain why.

History

Friday, 15 September 2000

Sunday, 17 September 2000

Monday, 18 September 2000

Wednesday, 20 September 2000


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

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

Source text last modified Wed Sep 20 08:21:05 2000.

This page generated on Wed Sep 20 08:22:16 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu