1
h06
CS56 F18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
5pm, 6pm, 7pm
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h06: Autobox/Autounbox (HFJ Ch 10)

ready? assigned due points
true Thu 10/11 04:00PM Mon 10/15 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Please review the material on wrapper types, auto-boxing and auto-unboxing in HFJ Chapter 10, then answer these questions.

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts (5pm, 6pm, 7pm) in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
  2. Given the following program below, determine whether each line of code involves boxing or unboxing, and check the boxes accordingly.

    If a line of code involves both, check both boxes. If it involves neither, check neither box.

    Points Line
    number
    Auto-Boxing Auto-Unboxing
    (5 pts) 4
    (5 pts) 5
    (5 pts) 6
    (5 pts) 7
    (5 pts) 8
    Points Line
    number
    Auto-Boxing Auto-Unboxing
    (5 pts) 9
    (5 pts) 10
    (5 pts) 11
    (5 pts) 12
    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(new Integer(2));
    	mylist.add(1);
    	mylist.set(0,3);
    	int a = mylist.get(0) + mylist.get(1);
    	mylist.add(7);
    	Integer b = mylist.get(2);
    	Integer c = b;
    	Integer d = a + c;
    	System.out.println("a=" + a + " b= " + b + " c=" + c + " d=" + d);
        }
    }
    
  3. (45 pts) Refer to the code for the 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.

    IF YOU ARE UNSURE HOW TO COMPLETE THIS, BRING IT TO CLASS. THE PROFESSOR WILL GO OVER THIS EXAMPLE IN CLASS, AND YOU MAY FILL IN YOUR ANSWERS (OR CHANGE THEM) BEFORE YOU TURN IN YOUR PAPER.

    Object Fill in line here
    (a) Fido  
    (b) Ginger  
    (c) Harry  
    (d) Izzy  
    (e) Jack  
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    public class Dog {
        
        private static Dog bestInShow = null;
        private String name;
        
        public static void setBestInShow(Dog b) {
    	bestInShow = b;
        }
        
        public static Dog getBestInShow() {
    	return bestInShow;
        }
        
        public Dog(String name) { this.name = name;}
        
        public static void main(String [] args) {
    	
    	Dog d1 = new Dog("Fido");
        	Dog d2 = new Dog("Ginger");
    	Dog d3 = new Dog("Harry");
    	Dog d4 = new Dog("Izzy");
    	Dog d5 = new Dog("Jack");
    	Dog d6 = d1;
    	
    	setBestInShow(d3);   
    	d1 = d4;             
    	d3 = d6;             
    	d1 = null;           
    	d2 = null;           
    	d3 = null;           
    	d4 = null;           
    	d5 = null;           
    	d6 = null;           
    	setBestInShow(null);
        }
    }