Summer 2001 Midterm
Evening Section

Tuesday June 26th, 2001
Duration: 60 minutes

Aids Allowed: The Java API: An Introduction for Students, July 2000 edition.

First Name: ______SOLUTION_________    Last Name: _________________________

Student ID: ____________________________

Instructor:                         Andria Hunter

Circle tutor's name:           Dave Hill         Michael McGuffin

Do not turn this page until you have received the signal to start. In the meantime, please fill out the identification section above, and read the instructions below carefully.


This test consists of 4 questions on 8 pages (including this one). When you receive the signal to start, please make sure that your copy of the test is complete.

Answer each question directly on the test paper. There has been enough space left on this test paper to answer each question, so if you find yourself using much more room than what is available, you're probably missing something.

Be aware that unreadable answers will be given a mark of zero, so please write legibly.

Question 1: ________ /  8

Question 2: ________ / 17

Question 3: ________ / 10

Question 4: ________ /  5

Final Mark: ________ / 40

Question 1

 (a)   [4 marks]

Write a public method called largest that has three int parameters and returns the parameter that contains the largest value.

	public static int largest (int a, int b, int c) {
		if (a > b && a > c) {
			return a;
		} else if (b > c) {
			return b;
		} else {
			return c;

 (b)   [4 marks]

Write a public method called check that takes two parameters (an int and a String) and returns true if and only if all of the following conditions are true:

Be careful about the order that you check these conditions, so that you do not cause an exception. If you use an if statement on this subquestion, you will lose one mark. For full marks, solve it with only a return statement.

	public static boolean check (int a, String s) {
		return s != null && s.length() >= 4 &&
			s.indexOf('e') < 0 && a == s.length();

Question 2

 (a)   [11 marks]

Complete the missing methods in the class below, according to the comments that have been provided.

// This class is used to model a river raft.
class RiverRaft {
	private String name;	// name of this raft
	private int numPeople;	// number of people currently on raft
	private int capacity;	// maximum number of people the raft can hold

	// RiverRaft constructor: construct a new river raft object
	// that has name 'n' and can hold a maximum capacity of 'c' people.
	public RiverRaft (String n, int c) { = n;
		this.capacity = c;

	// isFull: This method returns 'true' if there is no room for
	// any more people on this raft.  It returns false otherwise.
	public boolean isFull () {
		return numPeople == capacity;

	// equals: This method compares this river raft object to the
	// river raft object 'otherRaft' that is passed as a parameter.
	// It turns 'true' if the river rafts are the same, or 'false'
	// otherwise.  River raft objects are considered the same if
	// they have the same name, capacity, and current number of
	// people on the raft.
	public boolean equals (RiverRaft otherRaft) {
		return this.numPeople == otherRaft.numPeople && &&
		this.capacity == otherRaft.capacity;

	// addPeople: this method attempts to add 'p' people to this river
	// raft.  There are three different things that may happen:
	//  1. There is room to add all 'p' people to the raft.  This
	//      method adds the 'p' people to the raft and prints an
	//      informative message.
	//  2. There is not enough room to add any of these 'p' people
	//      to the raft.  No people are added to the raft and
	//      an informative message is printed.
	//  3. There is only room to add some of the 'p' people to the
	//      raft.  This method adds as many people as it can to fill
	//      up the raft.  It prints two messages - how many people could
	//      not fit and how many people were added.
	public void addPeople (int p) {
		if (numPeople + p <= capacity) {
			numPeople += p;
			System.out.println (p + " added to raft.");
		else if (isFull()) {
			System.out.println ("Not enough room to add any people.");
		else {
			int add = p - (numPeople + p - capacity);
			numPeople += add;
			System.out.println (add + " added to raft.");
			System.out.println (p-add + " could not fit.");

	// toString: This method returns a string that represents all of
	// the information for this river raft.  The name, capacity, 
	// current number of people, and whether it is full.  This method
	// has been completed for you.
	public String toString () {
		return "Name: " + + "\n" +
			   "Capacity: " + this.capacity + "\n" +
			   "Current People: " + this.numPeople + "\n" +
			   "Is Full: " + this.isFull();


 (b)   [3 marks]

Complete the Question2 class provided below. It uses the RiverRaft class from part (a). The comments provided below explain what your code must do.

public class Question2 {
	private static void main (String[] args) {

		// Create a river raft object called 'rr1'.  This river raft
		// is called "Titanic" and it can hold a maximum of 6 people.
		RiverRaft rr1 = new RiverRaft ("Titanic", 6);

		// Create a river raft object called 'rr2'.  This river raft
		// is called "Unsinkable" and it can hold a maximum of 5 people.
		RiverRaft rr2 = new RiverRaft ("Unsinkable", 5);

		// Determine if 'rr1' and 'rr2' are the same, by calling the
		// equals method.  This has already been completed for you.
		if (rr1.equals (rr2)) {
		    System.out.println ("River Rafts are the same");

		// Attempt to add 3 people to the Titanic river raft, 'rr1'.
		rr1.addPeople (3);

		// Attempt to add 4 more people to the Titanic river raft, 'rr1'.
		rr1.addPeople (4);

		// Print the contents of the object that 'rr1' refers to, by
		// calling the toString method on this river raft.
		System.out.println (rr1.toString());

 (c)   [3 marks]

Write the exact output that is produced by the last statement of the main method, which calls the toString method. Write your output in the box below:


      Name: Titanic
      Capacity: 6
      Current People: 6
      Is Full: true

Question 3 [10 marks]

For the program below, show what each System.out.println statement will produce as output by writing your output to the right of each System.out.println statement in the main method. Hint: For the part that uses the MyClass class, it may help to draw the memory model.

class MyClass {
	private static int v = 2;
	private char a;

	public MyClass (char a) {
		this.v += 2;
		this.a = a;

	public void set (char a, int b) {
		this.a = a;
		v += b;

	public boolean comp (MyClass m) {
		v += MyClass.v;
		return this.a != m.a;

	public String toString() {
		return "a: " + this.a + " v: " + v;

public class Question3 {
	public static void main(String args[]) {

		int a = 3;
		double b = a + 5.6;
		System.out.println (a);				3
		System.out.println ((int) b);			8

		System.out.println (2 + 3 + "Hi");		5Hi
		System.out.println ("Bye" + 2.5 + 3);		Bye2.53

		String s = "everything";
		char c = 'i';
		System.out.println (s.substring(s.indexOf(c)));	ing
		System.out.println (s.charAt(s.toUpperCase().length()-3)); i

		MyClass m1 = new MyClass ('X');
		System.out.println (m1);			a: X v: 4

		MyClass m2 = new MyClass ('Y');
		m2.set ('X', a);
		System.out.println (m2);			a: X v: 9

		System.out.println (m1.comp(m2));		false
		m1 = m2;
		System.out.println (m2.comp(m1));		false

Question 4 [5 marks]

On the next page, you are given a partial memory model diagram for the program below. Your task is to show the final state of the memory model diagram when the '@@' statement below finishes executing.

Show your update to the method stack and to the object space by writing on the memory model diagram that has been provided for you on the next page.

public class Question4 {
	public static void main (String[] args) {
		Dog snoopy = new Dog ("Snoopy", 3);
		Master joan = new Master ("Joan", snoopy);
		joan.giveBone (5);

class Master {
	private String name;
	private Dog dog;

	public Master (String name, Dog dog) { = name; = dog;

	public void giveBone (int numBones) { (numBones);

class Dog {
	private String name;
	private int bones;

	public Dog (String name, int bones) { = name;
		this.bones = bones;

	public void buryBone (int howMany) {
		this.bones += howMany;	// @@ <-- SHOW AFTER THIS EXECUTES
		System.out.println ("Just burried " + howMany);