1
Handout A
for
e01
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;
    }
}

LotteryPick and LottoPick

The classes LotteryPick and LottoPick are both used to represent a set of numbers that someone might choose for a lottery where numbered balls are chosen (e.g. 5 or 6 balls numbered 1-40, for example), and you win money depending on how many balls your picks match.

class LotteryPick

The class LotteryPick uses inheritance (extending java.util.ArrayList<Integer> to implement the constructors and methods shown below in the excerpt of the javadoc.

(Contrast this with LottoPick, which uses composition instead, and is discusssed on the bottom half of this page).

Inheritance Hierarchy (complete)

java.lang.Object
  java.util.AbstractCollection<E>
    java.util.AbstractList<E>
      java.util.ArrayList<java.lang.Integer>
        LotteryPick
All Implemented Interfaces: java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<java.lang.Integer>, java.util.Collection<java.lang.Integer>, java.util.List<java.lang.Integer>, java.util.RandomAccess

Constructors (complete)

LotteryPick(int[] nums) Initialize from int array.
LotteryPick(LotteryPick other) Initialize from another LotteryPick.
Example: LotteryPick myPicks(otherPicks);

Most important methods, with brief description

int numMatched(LotteryPick other) Return the number of values matched, and as side effect sorts both this LotteryPick and the other passed as a param
void sortNumbers() Sort the numbers in ascending order
boolean unique() Return true if there are no duplicates, and as side effect, sorts numbers

Important Note

A Be aware that this class inherits from java.util.ArrayList<Integer>. You have a summary of the javadoc for java.util.ArrayList<Integer> available to you on Handout B




class LottoPick

The class LottoPick uses composition (it “has-a” java.util.ArrayList<Integer> as a private data member) to implement the constructors and methods shown below in the excerpt of the javadoc.

Inheritance Hierarchy (complete)

java.lang.Object
  LottoPick

There are no implemented interfaces listed in the javadoc.

Constructors (complete)

Lotto(int[] nums) Initialize from int array.
LottoPick(LottoPick other) Initialize from another LottoPick.
Example: LottoPick myPicks(other);

Most important methods, with brief description

boolean equals(java.lang.Object o) Override equals to compare picks. Uses the implementation of equals from java.util.ArrayList<Integer>, thus order will matter.
java.util.ArrayList<java.lang.Integer> getPicks() return copy of wrapped ArrayList<Integer> (not reference to original)
int hashCode() returns hashCode value of wrapped ArrayList
static int numMatched(LottoPick first, LottoPick second) Return the number of values matched, and as side effect sorts numbers in both LottoPick objects
void sortNumbers() Sort the numbers in ascending order
boolean unique() Return true if there are no duplicates, and as side effect, sorts numbers

Important Note

Pay attention to the fact that the numMatched method is static in LottoPick.

End of Handout