1
e03
CS56 w18
DO NOT WRITE IN THIS AREA! Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu

EXAM: e03: Final Exam

ready? date points
true Wed 03/21 12:00PM

You may not collaborate on this exam with anyone. If you need to use the restroom, you must leave your cell phone with the exam proctor before leaving the room.

  • Write your name at the top of this page AND EVERY ODD NUMBERED PAGE.
  • Double check that you turned in ALL pages; look for "End of Exam" on the last page.
  • This exam is closed book, closed notes, closed mouth, cell phone off.
  • You are permitted one sheet of paper (max size 8.5x11") on which to write notes.
  • This sheet will be collected with the exam, and might not be returned.
  • Please write your name on your notes sheet.

  1. (15 pts) On handout C there is a class Dog with a main that creates some Dog objects.

    Your job: figure out after which line of main() each of the following Dog objects is eligible for garbage collection.

    If an object is still not eligible for garbage collection when the last line of main is reached, write “never”. Each answer should be a line number, or the word never.

    Object Fill in line here
    (a) Ace  
    (b) Buddy  
    (c) Coco  
    (d) Daisy  
    (e) Eddie  
  2. (10 pts) Given the following program:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    import java.util.ArrayList;
    public class BoxUnbox {
        public static void main(String [] args) {
    	ArrayList<Integer> mylist = new ArrayList<Integer>();
    	mylist.add(1);
    	mylist.add(new Integer(4));
    	Integer a = mylist.get(0) + mylist.get(1);
    	int b = mylist.get(0);
    	Integer c = a + b;
    	Integer d = b;
    	Integer e = mylist.get(mylist.get(0));
    	System.out.println("a=" + a + " b= " + b + " c=" + c);
    	System.out.println("d=" + d + " e= " + e);
        }
    }

    What is the output? Indicate by filling in the blanks:

    a=____ b= ___ c=____
    d=____ e= ____
    
  3. (16 pts) With the same program, indicate for each line whether the code involves auto-boxing, auto-unboxing, both or neither. If a line of code involves both, check both boxes. If it involves neither, check neither box.

    Code auto-boxing auto-unboxing

    ArrayList<Integer> mylist = new ArrayList<Integer>();

    mylist.add(1);

    mylist.add(new Integer(4));

    Integer a = mylist.get(0) + mylist.get(1);

    int b = mylist.get(0);

    Integer c = a + b;

    Integer d = b;

    Integer e = mylist.get(mylist.get(0));

  4. For this question, you need page 1 of handout A with code for these files: Beverage.java, Edible.java, Food.java, FreeCandy.java and Product.java. These are classes used by a grocery store known as “Trader Bobs”.

    Some of these methods will compile and run, while others will not.

    Indicate, for each method, whether it compiles or not, and if it does compile, the output when invoked. in context of the classes on handout A and assuming the methods appear inside this class:

    public class TraderBobs {
     // methods appear here
    }
    
    Will it compile?Output when called (only if it compiles)
    Yes
    No
    1. (3 pts)
        public static void TB01 () {
          Edible o = new Beverage(89,"Diet Coke",0,12.0);
          System.out.println("o: " + o.getFluidOunces());
        }
      
    2. (3 pts)

        public static void TB02 () {
          Food r = new Food(99,"Almonds",100,0.63);
          System.out.println("r: " + r.getName());
        }
      
    3. (3 pts)

        public static void TB03 () {
          Edible t = new FreeCandy(30);
          System.out.println("t: " + t.getPrice());
        }
      
    4. (3 pts)

        public static void TB04 () {
          Product u = new Product(299,"Ziploc Bags");
          System.out.println("u: " + u.getName());
        }
      
  5. (4 pts) For this question, and all the remaining ones on this exam, you should refer to page 1 of handout A which shows an excerpt of the javadoc for two classes.

    Both solve the same problem in different ways. They both try to represent a set of set of numbers that someone might choose in a lottery drawing.

    • LotteryPicks uses inheritance (is-a)
    • LottoPicks uses composition (has-a)

    Also refer to page 2 of handout B and handout C where you can find unit tests, and both sides of handout D where you can find the full source code, with a few lines left out.

    It is those lines that you need to fill in on the remainder of this exam, starting with these lines from LotteryPick.

    In all of these problems, if you are stuck, look through the unit tests, the javadoc, and the full listing for clues. Also, consider skipping the problem and coming back to it, after answering an easier one.

    YOU MAY ONLY ADD CODE WHERE THERE ARE BLANKS.

    YOU MAY NOT ADD ANY EXTRA CODE. ONLY ONE LINE OF CODE PER BLANK.

        public LotteryPick(LotteryPick other) {
    	// use superclass constructor
    	// in form ArrayList(Collection<? extends E> c) 
    
    
            _____________________________
        }
    
    
  6. (4 pts) Fill in code for this constructor from LotteryPick.

    YOU MIGHT NOT NEED ALL THE BLANKS.

    DO NOT ADD CODE ANYWHERE EXCEPT IN THE BLANKS.

    ONLY ONE LINE OF CODE PER BLANK.

        public LotteryPick(int [] nums) {
    
    
            ____________________________
    	for (int i: nums) {
    
    
               ________________________
    	}
    
    
    	_______________________
        }
    
    
  7. (4 pts) Fill in code for this method from LotteryPick.

    DO NOT ADD CODE ANYWHERE EXCEPT IN THE BLANK.

    ONLY ONE LINE OF CODE PER BLANK.

        public void sortNumbers() {
    	// do it with one line of code,
    	// taking advantage of the fact that
    	// java.lang.Integer implements Comparable<Integer>
    
    
            ___________________________________
        }
    
    
  8. (6 pts) Fill in code for this method from LotteryPick.

    YOU MIGHT NOT NEED ALL THE BLANKS.

    DO NOT ADD CODE ANYWHERE EXCEPT IN THE BLANKS.

    ONLY ONE LINE OF CODE or EXPRESSION PER BLANK.

        public boolean unique() {
    	this.sortNumbers();
    	
    	for (______________; _________________; ____________) {
    	    if (this.get(i) == this.get(i-1))
    
                    __________________
    	}
    	
    	_____________________
        }
    
    
  9. (5 pts) Fill in code for this method from LotteryPick.

    YOU MIGHT NOT NEED ALL THE BLANKS

    DO NOT ADD CODE ANYWHERE EXCEPT IN THE BLANK.

    ONLY ONE LINE OF CODE PER BLANK.

        public int numMatched(LotteryPick other) {
    	if (!this.unique()) {
    	    throw new IllegalArgumentException("this contains duplicates");
    	}
    	if (!other.unique()) {
    	    throw new IllegalArgumentException("other contains duplicates");
    	}
    
    	// both now sorted; merge the two together, sort and count duplicates
    	LotteryPick merged = new LotteryPick(this);
    	merged.addAll(other);
    	merged.sortNumbers();
    	
            ____________________
    	
    	for (int i=1; i<merged.size(); i++) {
    	    if (merged.get(i)==merged.get(i-1))
    		dups ++;
    	}
    
            ___________________
        }
    }
    
  10. (4 pts) NOTE: WE NOW TURN TO THE OTHER CLASS. We are no longer looking at LotteryPick, which uses inheritance.

    We are looking at LottoPick, which uses Composition.

    In all of these problems, if you are stuck, look through the unit tests, the javadoc, and the full listing for clues. Also, consider skipping the problem and coming back to it, after answering an easier one.

    YOU MAY ONLY ADD CODE WHERE THERE ARE BLANKS.

    YOU MAY NOT ADD ANY EXTRA CODE. ONLY ONE LINE OF CODE PER BLANK.

    Fill in the blank in this constructor

        public LottoPick(LottoPick other) {
    	// make sure to do a deep copy, not a shallow one
    	// that is, don't make both LottoPicks refer to the same ArrayList
    	// use one line of code
    
    
            _____________________________________________
        }
    
    
  11. (4 pts) Fill in code for this constructor from LotteryPick.

    YOU MIGHT NOT NEED ALL THE BLANKS.

    DO NOT ADD CODE ANYWHERE EXCEPT IN THE BLANKS.

    ONLY ONE LINE OF CODE PER BLANK.

        public LottoPick(int [] nums) {
    
           ______________________________
    	for (int i: nums) {
    
    
    	    ____________________
    	}
    
    
            ________________________
        }
    
    
  12. (4 pts) Fill in code for this method from LottoPick.

    DO NOT ADD CODE ANYWHERE EXCEPT IN THE BLANK.

    ONLY ONE LINE OF CODE PER BLANK.

        public void sortNumbers() {
    	// do it with one line of code,
    	// taking advantage of the fact that
    	// java.lang.Integer implements Comparable<Integer>
    
           ___________________________________________
        }
    
    
  13. (6 pts) Fill in code for this method from LottoPick.

    YOU MIGHT NOT NEED ALL THE BLANKS.

    DO NOT ADD CODE ANYWHERE EXCEPT IN THE BLANKS.

    ONLY ONE LINE OF CODE or EXPRESSION PER BLANK.

    
        public boolean unique() {
    
    
    	_____________.sortNumbers();
    
    
    	for (int ___________; _______________; __________) {
    	
    	    if (picks.get(i) == picks.get(i-1))
    
                   _________________
    	}
    
            _______________
        }
    
    
    
  14. (6 pts) Fill in code for this method from LottoPick.

    YOU MIGHT NOT NEED ALL THE BLANKS

    DO NOT ADD CODE ANYWHERE EXCEPT IN THE BLANK.

    ONLY ONE LINE OF CODE PER BLANK.

    BE CAREFUL: this is a static method, unlike it’s counterpart in LotteryPick.

        public static int numMatched(
        	   LottoPick first,
        	   LottoPick second) {
    	if (!first.unique()) {
    	    throw new IllegalArgumentException
    	    	  ("this contains duplicates");
    	}
    	if (!second.unique()) {
    	    throw new IllegalArgumentException
    	    	  ("other contains duplicates");
    	}
    
    	// both now sorted; merge the two together, sort and count duplicates
    	// HINT: LOOK AT THE IMPLEMENTATION in LotteryPick
    
    
    	LottoPick merged = ______________
    
    
            _________________________________;
    
    	merged.sortNumbers();
    
    
            __________________
    	for (int i=1; i<merged.picks.size(); i++) {
    	    if (merged.picks.get(i)==merged.picks.get(i-1))
    		dups ++;
    	}
    
    
            ____________________
        }
    
    
    

This page is intentionally blank

End of Exam