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


Exam 1: Java Fundamentals

This page may be found online at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Exams/exam.01.html.

The code files can be found online at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Examples/Exam1/.

Distributed: Friday, September 17, 1999
Due: 10 a.m., Friday, September 24, 1999
No extensions!

Policies

There are three problems on the exam, each with an equivalent point value (35 points). Note that the different questions are not necessarily of the same complexity or length.

This examination is open book, open notes, open mind, open computer, open Web. Feel free to use any and all 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 four hours. 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. I will make a solution key available soon after the exam.

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 definitely the hardest exam I have ever taken.'' You may also summarize these policies.

Answer all of your questions electronically. That is, you must write all of your answers on the computer and print them out. Please put your answers in the same order that the problems appear in.

I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by highlighting 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

A. Computing Averages

Carl and Carla Caffeinated have decided to combine ideas that we've recently seen (or will see) and have built a program that averages values typed on the command line. Here's the class, Average, that they've come up with.


import SimpleOutput;

/**
 * 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.0 of September 1999
 */
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 grade is: " + (sum/count));
  } // main(String[])
} // class Average



In all their tests, the program seems to work fine

% ji Average 3 4 5
The average grade is: 4
% ji Average 5 4 3
The average grade is: 4
% ji Average 10 100
The average grade is: 55
% ji Average -10 20
The average grade is: 5
% ji Average -10 aardvark zebra 20
The average grade is: 5
% ji Average 10.5 9.5 10
The average grade 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 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''.

% ji Average 1 1 3
The average grade is: 1
% ji Average 0 100.1 200.2
The average grade is: 0

1. Basic Errors

Your first step is to help the Caffeinateds deal with the errors that their instructor has identified.

2. Other Errors

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.

B. A Registration System

As you may know, the registrar's office recently upgraded to a new system. Suppose that the registrar had instead decided to hire us to build the new system. We would, of course, begin that task by identifying important objects in the system.

1. A Student Class

List the fields and methods you might expect to find in the Student class. Don't worry about implementing the methods; just summarize their purpose. If you use another class as the type of a field, make sure that you describe that class in your answer to B.2.

2. Core Objects

What are the other core data classes you might expect to find in a registration system? Don't worry about the objects involved in the interface. Rather, consider the underlying objects that store the important information, such as Student. For each class, list the name and a summary of its purpose and content.

3. A Transcript Story

Write a short narrative describing the objects and methods that might be involved in the operation ``Print T. Smith's Transcript''. Your narrative might begin with something like the following.

After connecting to the system and entering account and password, the registrar finds the object, current, in class Student that corresponds to T. Smith. The registrar then calls current.toString() to generate a printable version of the transcript. The toString() method first calls getGraduationYear() (also in class Student) ...

C. Correcting and Commenting Code

Our friends the Caffeinateds have realized that they don't know how to indent and comment code. Please turn their Book.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. Use the existing code (which does work); do not rewrite from scratch.


public class Book {
  public int getNumChapters() {
    return this.numChapters; 
  }
  public Book() { 
    this.chapters = new String[20]; 
    this.numChapters = 0; 
  }
  public String getTitle() { 
    return this.title; 
  }
  protected String title;

// Needs significant formatting help!
public String getAuthor() { return
this.author; } protected String author; public String
toString() { String
stuff = "\"" + 
this.title + "\" by " 
+ this.author + "\n";
  for (
int i = 0;
i < numChapters; ++i) { if (chapters[i] != null) {
stuff = stuff + "  " + (i+1) + ": " + chapters[i] + "\n"; 
                          }
}
return stuff; } int numChapters;

  public Book(int numChapters) {
    this.chapters = new String[numChapters]; 
    this.numChapters = 0; 
  }
  public Book(String author, String title) {
    this.author = author; 
    this.title = title; 
    this.chapters = new String[20]; 
    this.numChapters = 0; 
  }

  protected String[] chapters;
  
  public void addChapter(String chapterName)
    throws Exception
  {
    if (numChapters >= chapters.length) { 
      throw new Exception();
    }
    chapters[numChapters] = chapterName; 
    numChapters = numChapters + 1;
  } 

// Needs more help
public String
  getChapter(int chapterNum) 
throws Exception { chapterNum=chapterNum - 
1; if (chapterNum >= numChapters) 
{ throw new Exception(); } if
(chapterNum < 0) { throw new Exception(); }
  if
(chapters[chapterNum] == null)            {
  throw new Exception();                  }
  return chapters[chapterNum]; } }


You may find it helpful to use the following test program.


import SimpleOutput;

/**
 * Test our sample book class.  Does not yet test all issues
 * (such as zero-parameter constructors).
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of September 1999
 */
public class BookTester {
  public static void main(String[] args) 
    throws Exception
  {
    SimpleOutput out = new SimpleOutput();
    Book eij = new Book("Sam Rebelsky", "Experiments in Java");
    // Print the basic information.
    out.println("* Test: Print basic information");
    out.print(eij.toString());
    // Set two chapters.
    out.println("\n*Test: Set two chapters");
    eij.addChapter("J1: An Introduction to Java");
    eij.addChapter("J2: Objects and Classes");
    out.print(eij.toString());
    // Get an valid chapter.
    out.println("\n*Test: Get valid chapter");
    out.println("Chapter 2: " + eij.getChapter(2));
    // Get an invalid chapter.
    out.println("\n*Test: Get invalid chapter");
    try {
      out.println("Chapter 4: " + eij.getChapter(4));
    }
    catch (Exception e) {
      out.println("As expected, that failed.");
    }
    // Add too many chapters
    out.println();
    for (int i = eij.getNumChapters(); i < 25; ++i) {
      try {
        out.print("*Test: Adding chapter " + i + "...");
        eij.addChapter("Untitled");
        out.println("okay");
      }
      catch (Exception e) {
        out.println("failed");
      } // caught "too many chapters" exception
    } // add a lot of chapters
    // And see what we have
    out.println("\n*Final version:");
    out.println(eij.toString());
  } // main(String[])
} // class BookTester


History

Tuesday, 10 August 1999

Thursday, 16 September 1999

Friday, 17 September 1999


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.math.grin.edu/~rebelsky/Courses/CS152/99F/Exams/exam.01.html

Source text last modified Fri Sep 17 08:48:04 1999.

This page generated on Fri Sep 17 08:48:32 1999 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu