CS 1713-001 Introduction to Computer Science
Fall 2003 -- Final Exam

Directions: Use your own paper for answers. Do not spend too much time on any one question.

  1. Consider the array declaration: Write a simple code segment (with a loop) that will add up the square of each array entry. When you are done, print your answer. (Your program should not directly use the number 7, the number of elements in the array, but should work unchanged if one added 19 to the end of the above array.)


  2. Consider the array declaration: Write a simple code segment (with a loop) that will find the smallest value (first in alphabetic order) in this array. Your loop should use the length field of the array, and not the constant 12. (Remember that you cannot use < to compare two strings.)


  3. European Roulette wheels are divided into 37 evenly spaced slots numbered 0, and 1 through 36. The zero slot is green and the other numbers are either red or black, with 18 of each kind. A ball is rolled and ends up in one slot. Thus the outcome of each play is a number from 0 to 36 inclusive (37 possible numbers). One possible bet is on either EVEN or ODD. If the ball lands in 0, you lose whichever way you bet.

    Write a code segment (not a complete program) that will simulate playing Roulette over and over again, each time betting on ODD (so that you win if one of the 18 numbers 1, 3, 5, ..., 35 comes up). Each time you will bet one dollar. You start with an initial stake of 100 dollars. If you win, your stake increases by $1, and if you lose it decreases by $1. Simulate play until either you have doubled your stake (to $200), or until you have lost all your money. Count the number of times you have to play till you finish in this way.

    [You may assume that you have a class Wheel with a static method roll() that will return the result of rolling Roulette: an integer from 0 to 36 inclusive. Also, an int n is odd in case n%2 == 1.]

     

     

  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; // total number of grade points
       private double GPA; // grade point average = totalGradePoints / totalHours
    
       // uses name, total hours taken, and total gradepoints as parameters
       public Student(String n, int initHours, int initGradePoints) {
          // Fill in for Part a. here
       }
    
       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 using GPA.
          // Fill in for Part b. here
       }
    }
    
    // Students.java: test the Student class
    public class Students {
       public static void main (String[] args) {
           // four students, make up 4 variable names
           // fill in for part c. here  
           // invoke getGPA method for John West
           // fill in for part d. here
           // create an array names students to hold 4 student classes
           // fill in for part e. here
           // insert the 4 student classes into the array
           // fill in for part f. here
           // find student with highest GPA
           // fill in for part g. here
       }
    }
    
    1. Fill in code for the constructor. (Calculate the GPA as grade points / totalHours.)
    2. Fill in code for the method compareTo that will implement the Comparable interface. You should compare the GPAs in numeric order as doubles.
    3. Write code in the main method inside the separate Students class above that will create 4 student classes with the following inital data:
      • "Hazy, Bob", 20, 68
      • "Bowen, Bruce", 35, 92
      • "West, John", 25, 80
      • "Brown, Robert", 40, 145
    4. Show how to invoke the getGPA method for the student named John West.
    5. Declare an array of Student classes that has four array entries.
    6. Insert the four students into the array.
    7. Write a loop that will use the compartTo method of the Student class to find the student in the array with the highest GPA.


  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. here }
    // 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. here persons[1] = // Fill in for Part g. here persons[2] = // Fill in for Part g. here // loop to print each element of persons array // Fill in for Part h. here } }
    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. Then the getWages method should return the hourly rate times the hours worked.
    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").
    8. Finally, write a loop that will print each person's data from the array (just using the toString method of each class.

Points for each problem: 1-25, 2-25, 3-30, 4-40, 5-40.