CS 1713-001 Introduction to Computer Science
Spring 2001 -- Final Exam
Tuesday, 8 May 2001, 8:00 am - 10:15 am

Directions: Use your own paper for answers. When you are done you may keep this exam sheet and should pick up an answer sheet.

  1. Consider the array declaration:
      public int[] nums = {34, 27, 56, 12, 89, 67, 23};
    
    Write a simple code segment that will find the largest value in this array.


  2. Write a code segment that will try rolling three dice at a time. Count the number of times you roll the three dice, and keep rolling until you get a number "one" (one spot) on each of the three dice. Print out how many times it took. You may assume that you have a class Dice with a static method roll() that will return the result of rolling one 6-sided die once.


  3. The Fibonacci Sequence consists of the numbers:
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, ...
    
    After you start with 0 and 1, each later number is just the sum of the two previous numbers.

    Write a program segment (no need for an enclosing class or main method) that will calculate and print the first 40 Fibonacci numbers in the following way:

    1. Create an array named fib of 40 ints.
    2. Store 0 and 1 into fib[0] and fib[1] just with simple assignment statements.
    3. Use a loop to store the remaining numbers in locations f[2], f[3], ..., f[39]
    4. Print all the numbers in the array.


  4. Consider the following Student class, with missing code to be filled in:
    // Student.java: student grade record
    public class Student implements Comparable{
       private String name; // In the form "LastName, FirstName"
       private int totalHours; // total number of hours completed
       private int totalGradePoints; // GPA = totalGradePoints / totalHours
       private double GPA; // grade point average
    
       // uses name, total hours taken, and total gradepoints as parameters
       public Student(String n, int initHours, int initGradePoints) {
          // Fill in for Part a.
       }
    
       // same as above, but replaces total gradepoints by grade point average
       public Student(String n, int initHours, double initGPA) {
          // Fill in for Part b.
       }
    
       public double getGPA() { // return the GPA
          return GPA;
       }
       
       // assume the students takes a single course and gets a grade
       public void updateRecord(int hours, int grade) {
          totalHours += hours; totalGradePoints += hours * grade;
          GPA = (double)totalGradePoints / totalHours;
       }
    
       public String toString() {
          return "Name: " + name + ", GPA: " + GPA +
                 ", Total Hours: " + totalHours;
       }
    
       public int compareTo(Object t) { // compare names in alphabetic order.
          // Fill in for Part d.
       }
    }
    
    1. Fill in code for the first constructor. (Calculate the total grade points = number of hours * GPA.)
    2. Fill in code for the second constructor. (Calculate the GPA as grade points / totalHours.)
    3. There are two constructors shown. How does java know which constructor to use?
    4. Fill in code for the method compareTo that will implement the Comparable interface. This compares the names in alphabetic order.
    5. Write code in a separate class or in a main method that will create 4 student classes with the following inital data:
      • "Wilson, Bob", 20, 3.4
      • "Sanders, Bruce", 35, 92
      • "Samson, John", 25, 3.2
      • "Weston, Robert", 40, 145
    6. Show how to invoke the getGPA method for the student named John Samson.
    7. Show how to compare the first two students above, using the compareTo method.


  5. Consider the following hierarchy of classes, with various details to be filled in:
    // Person.java: abstract base class
    public abstract class Person { 
       public abstract double getWages(); // abstract, so omit body
    } 
    // Employee.java: the class Employee public class Employee extends Person { protected String name; // in the form "Last, First" protected String address; // constructor public Employee(String n, String a) { // first name, then address // Fill in for Part a. } // convert the point into a String representation public String toString() { return "[" + name + ", " + address + "]"; } public double getWages() { // must be either faculty or staff for wages return 0.0; } }
    // Faculty.java: the class Faculty public class Faculty extends Employee { // inherits from Employee protected double salary; // Constructor public Faculty(double s, String n, String a) { // salary, name, address // Fill in for Part b. } // convert the Faculty class to a String public String toString() { // Fill in for Part c. } public double getWages() { return salary; } }
    // Staff.java: the class Staff public class Staff // Fill in for Parts d., e., f. }
    // Persons.java: test the Person-Employee-Faculty-Staff hierarchy public class Persons { public static void main (String[] args) { Person[] persons = new Person[10]; persons[0] = // Fill in for Part g. persons[1] = // Fill in for Part g. persons[2] = // Fill in for Part g. printPersons(persons); System.out.println(); } public static void printPersons(Person[] s) { for (int i = 0; i < s.length; i++) System.out.println(s[i]); } }
    1. Fill in the constructor for Employee above.
    2. Fill in the constructor for Faculty above, using the Employee constructor with super.
    3. Fill in the toString method for Faculty above, using the Employee toString with super.
    4. Write a Staff class that inherits from Employee (that is, extends Employee) The Staff class should have data fields double hourlyRate and int hoursWorked.
    5. Write a constructor for Staff that takes parameters the two new ones above, followed by the name and address.
    6. Write a toString method for Staff above, using the Employee toString with super.
    7. In the Persons class above, create new objects of each of the three kinds:
      • Employee (with name "Nobody, Nonce", and address "120 Worry Lane")
      • Faculty (with salary 2020, name "Blow, Joe", and address "222 Center St.")
      • Staff (with hourly rate 7.5, hours worked 40, name "Bonkers, Bruce", and address "1 Prime Ave").


  6. This problem asks you to write a Java program that will print the song ``99 bottles of beer on the wall.'' The numbers should be spelled out in English, and you should make use of several strings and arrays of strings. Here is a class with predefined declarations to use and missing parts to fill in (your answer should make use of the predefined strings):
    public class Bottles {
       private String[] ones =
          {"zero", "one", "two", "three", "four", "five",
          "six", "seven", "eight", "nine"};
       private String[] teens =
         {"Zero", "One", "Two", "Three", "Four", "Five",
          "Six", "Seven", "Eight", "Nine", "Ten",
          "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
          "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
       private String[] tens =
         {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
          "Sixty", "Seventy", "Eighty", "Ninety"};
    
       private String shortB = " bottles of beer.";
       private String longB = " bottles of beer on the wall.";
       private String wall = "What would happen if one should fall?";
    
    
       public void printSong() {
          // answer to part b. here
       }
    
       private String toEnglish(int n) {
          // answer to part a. here
       }
    }
    
    The song you are to print starts like this:
    Ninety-nine bottles of beer on the wall.
    Ninety-nine bottles of beer.
    What would happen if one should fall?
    Ninety-eight bottles of beer on the wall.
    
    Ninety-eight bottles of beer on the wall.
    Ninety-eight bottles of beer.
    What would happen if one should fall?
    Ninety-seven bottles of beer on the wall.
    
    Ninety-seven bottles of beer on the wall.
    ... (and so on until) ...
    zero bottles of beer on the wall.
    
    1. First write a helper method toEnglish that will convert its int input parameter (in the range from 0 to 99 inclusive) to English. [Hints: The helper method is similar to Project 1, but much easier since we are using arrays of strings and only need numbers up to 99. You should use the predefined arrays of Strings, and not try to write this method in the way that Project 1 was written. First handle an input parameter value n < 20 with the teens array. Remember, if the input parameter is n = 47 then n/10 will equal 4 and you can use the tens array. Similarly n%10 will equal 7 and you can use the ones array. Then concatenate.]

    2. Then write a method printSong() that will print the whole song. [Hints: Use a loop with a variable going from 99 down to 1. You must use a loop; you cannot suggest writing 99 segments of code.]

Points for each problem: 1-25, 2-25, 3-25, 4-40, 5-40, 6-40. (Free-5.)