CMPUT 102: Structural Programming and Data Structures
Term Exam 2: Monday March 13th, 2000
Instructor : Osmar Zaïane
Section : B1
Version : A

The answers are shown in red.


Section 1: Multiple choices [32 points] (4 points each)

Circle exactly ONE choice as the best answer to each question.
  1. Which of the following will execute in Java the body of the loop at least once:
    a) while loop;
    b) for loop;
    c) do-while loop;
    d) until loop;
    e) none of the above.

  2. Which of the following types CANNOT be put into a vector:
    a) Vector
    b) Turtle object as defined in the course assignment 1;
    c) Adventurer object as defined in the Adventure game program;
    d) Integer;
    e) None of the above.

  3. What is the value of the variable b after the execution of the following snippet of Java code if the variable a has the value 7 and the variable b has initially the value 3?
          switch(a) { 
            case 2: b=10; break;
       	case 4: 
       	case 7: b = 8;
       	case 9: b=10;break;
       	default: b = 9;
          }
    
    a) 9;
    b) 8;
    c) 10;
    d) 3.

  4. What is the value of the variable b after the execution of the previous snippet of Java code if the variable a has the value 4?:
    a) null;
    b) 10;
    c) 8;
    d) 9;
    e) will generate a run time error.

  5. Which one of the following statements will assign the 4th element of the vector aVector to the variable myElement?
    a) myElement=(Element)aVector.elementAt(4);
    b) myElement=(Element)aVector.elementAt(myElement);
    c) myElement=(Element)aVector.elementAt(5);
    d) myElement=(Element)aVector.elementAt(3);

  6. How many stars (*) will be printed after the following snippet of Java code is executed?
         
             for(i=0; i<5; i++) {
          	    j=i;
          	    while (j<5) {
                   System.out.print("*");
                   j=j+1;
                }
             }
    
    a) 5;
    b) 25;
    c) 15;
    d) 12;
    e) 4.

  7. What is the output of this statement:
    System.out.print("abcdefghijk".substring(3,6));
    a) defg
    b) abcdef
    c) cdefgh
    d) def

  8. Which ONE of the following statement-types DOES NOT require a boolean expression?
    a) while statement;
    b) switch statement;
    c) for statement;
    d) if-else statement;
    e) they all require a boolean expression.

Section 2: Simple code segments [6 points]

Write a method newSum that, given a double, sum, will return the same double if sum is less than 200, will return 80% of sum if sum is greater than 400, and 95% of sum otherwise.
public double newSum (double sum) {
    if (sum < 200) return sum;
    else if (sum > 400) return (sum * 0.8);
    else return (sum * 0.95);
}

Section 3: Writing code [42 points]

You are asked to write a small Java program that manages CD collections. The program will use 2 Java classes, CompactDisc and CollectionCD, to represent CDs and collections of CDs respectively.

The class CompactDisc has two instance variables: title and value. The constructor reads a string and a double from the keyboard and initialises the instance variables. The class has also a public method getValue() and a public method getTitle() that return the value of the CD and the title of the CD respectively.

The class CollectionCD has a vector as an instance variable and four instance methods: display(), getTotalValue(), nbValuable(), and addCD(). The constructor creates an empty vector. The method display() prints all the CDs one per line, printing the title followed by the value (in the same order they were entered). The method getTotalValue() returns the total value of all the CDs in the collection. The method nbValuable returns the number of CDs in the collection than have a value higher than a given amount (given as a parameter). The method addCD() receives an object CompactDisc as an argument and adds it in the collection.

Write the classes CompactDisc and CollectionCD and write a small test program that asks for the number of CDs in the collection then iterates with a for loop to read all CDs (title and value) then prints the whole collection and its total value as well as the number of CDs that have a value higher than $20.

For the sake of simplicity, do not do input validation. There is no need to check for errors when information is read from the keyboard. Just assume that the user will enter valid values.

Here is an example of the program execution:

How many CDs? 3
Title? Asza
Value? 25.95
Title? Vox Divine Rites
Value? 30.00
Title? Espana Antigua
Value? 18.55

Asza $25.95
Vox Divine Rites $30.00
Espana Antigua $18.55
Total Value=$74.50
2 CDs with value more than $20.00
  1. [12 points] Write the class CompactDisc.
    public class CompactDisc {

          /*Instance Variables*/
            private String title;
            private double value;

          /*Constructor*/
          public CompactDisc(){
            System.out.print("Title? ");
            this.title=Keyboard.in.readString();
            System.out.print("Value? ");
            this.value=Keyboard.in.readFloat().doubleValue();
          }

          /*Instance Methods*/

          public double getValue(){
            return this.value;
          }

          public String getTitle(){
            return this.title;
          }
    }

  2. [18 points] Write the class CollectionCD.
    import java.util.*;
    public class CollectionCD {

          /*Instance Variables*/
            private Vector collection;

          /*Constructor*/
          public CollectionCD(){
            this.collection = new Vector();
          }
          public void addCD(CompactDisc myCD){
            this.collection.addElement(myCD);
          }

          public double getTotalValue(){
            double totalValue=0;
            CompactDisc myCD;
            int i=0;
            while (i<this.collection.size()) {
                myCD=(CompactDisc)this.collection.elementAt(i);
                totalValue = totalValue + myCD.getValue();
                i=i+1;
            }
            return totalValue;
          }

          public int nbValuable(double value){
            CompactDisc myCD;
            int valuableCDs=0;
            int i=0;
            while (i<this.collection.size()) {
                myCD=(CompactDisc)this.collection.elementAt(i);
                if (myCD.getValue() > value) valuableCDs = valuableCDs + 1;
                i=i+1;
            }
            return valuableCDs;
          }

          public void display(){
            CompactDisc myCD;
            int i=0;
            double theValue;
            while (i<this.collection.size()) {
                myCD=(CompactDisc)this.collection.elementAt(i);
                System.out.print(myCD.getTitle());
                System.out.print(" $");
                theValue=Math.round(myCD.getValue()*100)/100;
                System.out.println(theValue);
                i=i+1;
            }
          }
    }

  3. [12 points] Write the test program that uses CollectionCD and CompactDisc classes to read a certain number of CDs then displays the collection, its total value and the number of CDs with a value greater than $20.
    public class myTest {
       /* Reads the number of CDs in collection then iterates to read all CDs and their values and displays the whole collection and the total value */

          public static void main (String args[]) {
            int nbCDs;
            CollectionCD myCDs;
            int i;
            double collectionValue;

            myCDs = new CollectionCD();
            System.out.print("How many CDs? ");
            nbCDs = Keyboard.in.readInteger().intValue();
            for(i=0;i<nbCDs;i++){
                myCDs.addCD(new CompactDisc());
            }
            myCDs.display();
            System.out.print("Total Value=$");
            collectionValue=Math.round(myCDs.getTotalValue()*100)/100;
            System.out.println(collectionValue);
            System.out.print(myCDs.nbValuable(20));
            System.out.println("CDs with value more than $20.00");
          }
    }

Section 5: Tracing code [20 points]

Consider the following Java class:
public class myQuestion {

       /* instance variables */

	private int a;
	private int b;
	private Stack s;

	public myQuestion(int x, int y) {

		this.a = x;
		this.b = y;
		this.s = new Stack();
	}

	public void a1 (int ms, int mn) {

		int a=12;
 		int b=ms+mn;

		if (a<b) a= mn;
		else a=ms;
       	        System.out.println(a);
		this.a = ms;
		this.s.push("ms");
	}

	public  void a2(int ms, int mn) {
		int i=0;

		while (i<ms) {
			this.a=this.a+mn;
			this.b=this.b+1;
			i=i+1;
		}
		System.out.println(this.a);
		System.out.println(this.b);
		this.s.push("mn");
	}

	public void a3 (int ms, int mn) {
		int i;

		for(i=ms;i>0;i--) {
            	   if (i<mn) this.a = this.a + 1;
		   else this.b = this.b + 1;
		}
		System.out.println(this.a); 
		System.out.println(this.b); 
		this.s.push("ab");
                System.out.println(this.s);
	}
}

a ) [10 points] What is the output of the program segment:

myQuestion q;
q=new myQuestion(0,0);
q.a1(12, 8);
q.a1(8,12);
q.a2(3,2);
q.a3(4,2);

8
12
14
3
15
6
["ms", "ms", "mn", "ab"]

b) [10 points] What is the output of the program segment:

myQuestion q;
q=new myQuestion(1,2);
q.a1(12, 5);
q.a2(1,8);
q.a1(10,12);
q.a3(5,3);

5
20
3
12
12
6
["ms", "mn", "ms", "ab"]