Review for Quiz 4

One-page version suitable for printing.

Question 4-1

Consider the following class SemesterEnrollment to represent the enrollments of students in a semester.

public class SemesterEnrollment {
    private int num_students;
    private String[] names;
    private int[] total_enrolled;

    public SemesterEnrollment(int max_students) {
        num_students = 0;
        names = new String[max_students];
        total_enrolled = new int[max_students];
    }

    public void enroll(String name, int credits) {
        for(int i = 0; i < num_students; i++) {
            if(names[i].equals(name)) {
                total_enrolled[i] += credits;
                return;
            }
        }
        names[num_students] = name;
        total_enrolled[num_students] = credits;
        num_students++;
    }

    public int getCreditsTaken(String name) {
        for(int i = 0; i < num_students; i++) {
            if(names[i].equals(name)) {
                return total_enrolled[i];
            }
        }
        return 0;
    }
}
Write another class Registrar containing the single class method run(), which when executed interfaces with the user to enter and query the number of credits enrolled in by various students. This method should be able to handle up to 100 different students. Here is a sample description of how your program should work.
What to do? add Alice 2
What to do? add Bob 4
What to do? add Alice 4
What to do? add Spot 5
What to do? add Bob 1
What to do? query Bob
5
What to do? query Alice
6
  : (This program continues asking questions indefinitely)
Your Registrar class must use the Enrollments class to track the information for each student.

Solution

Question 4-2

Write a program that reads a line from the user and says ``ok'' if the user's line represents a valid integer, and ``bad'' otherwise.

Type something. -56a2
bad
Use Integer.parseInt(s), which converts the string s to an int; if the string isn't formatted correctly, the class method throws a NumberFormatException.
import csbsju.cs160.*;
public class Example {
    public static void main(String[] args) {



    }
}

Solution

Question 4-3

Consider the following two class definitions.

class Test {
  public int a;
  public Test() {
    a = 8;
  }
  public Test(int x) {
    a = x + 4;
  }
}
class Example extends Test {
  public int b;
  public Example() {
    b = 0;
  }
  public Example(int y) {
    super(y + 2);
    b = 1;
  }
}
What will the following sequence of statements print?
Example foo = new Example();
IO.println(foo.a);
IO.println(foo.b);
Example bar = new Example(8);
IO.println(bar.a);
IO.println(bar.b);

Solution

Question 4-4

Consider the following two class definitions.

class X {
  public double g(double x) {
    return 3.0 * f(x);
  }
  public double f(double x) {
    return x;
  }
}
class Y extends X {
  public double f(double x) {
    return 5.0 * x;
  }
}
What will the following sequence of statements print?
Y y = new Y();
IO.println(y.f(2.0));
X x = y;
IO.println(x.f(2.0));
IO.println(x.g(2.0));

Solution