1
IC01 Handout A
for
ic01
CS56 W18

Beverage.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Beverage extends Product implements Edible {

    private int calories;
    private double fluidOunces;
    
    public Beverage(int price, String name, 
		int calories, double fluidOunces) {
	super(price, name);
	this.calories = calories;
	this.fluidOunces = fluidOunces;
    }

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

Edible.java

1
2
3
4
/** something that can be eaten */
public interface Edible {
    public int getCalories();
}

Food.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Food extends Product implements Edible {

    private int calories;
    private double weight;
    
    public Food(int price, String name, 
		int calories, double weight) {
	super(price, name);
	this.calories = calories;
	this.weight = weight;
    }

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

Code for

TraderBobs problem

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;
    }
}
End of Handout