1
Handout B
for
e01-mw
CS56 W20

FreeCandy.java

1
2
3
4
5
6
7
8
9
10
public class FreeCandy implements Edible {

    private int calories;
    
    public FreeCandy(int calories) { 
	this.calories = calories;
    }

    public int getCalories() {return this.calories;}
}

Product.java

1
2
3
4
5
6
7
8
9
10
11
12
public abstract class Product {
    String name;
    int price;
    
    public int getPrice() { return price; } 
    public String getName() {return name;}

    public Product(int price, String name) {
	this.price = price;
	this.name = name;
    }
}

Handout B, p. 2

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("Kiki");
	Dog d2 = new Dog("Logan");
	Dog d3 = new Dog("Max");
	Dog d4 = new Dog("Nicky");
	Dog d5 = new Dog("Olive");
	Dog d6 = d5;
	
	setBestInShow(d1);
	d2 = d3;
	d4 = d6;
	d6 = null;
	d5 = null;
	d4 = null;
	d3 = null;
	d2 = null;
	d1 = null;
	setBestInShow(null);
    }
}
End of Handout