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;
    }
}

Partial source code for Factors.java

import java.util.ArrayList;

public class Factors extends ArrayList<Integer> {

    public Factors(int [] nums) {
    

       ___________________________________________

       for (int i: nums) {

             ______________________________________

	}

       ___________________________________________
    }

    /* Precondition: this object contains a list of factors computed from some number.  
       All but the last is guaranteed to be a prime number, and the factors are in non-decreasing
       order (they stay the same, or go up, from lowest index to highest index.)   

       Postcondition: If the last element is a prime number,
        return the same ArrayList, unchanged.

       Otherwise, take the last element, factor out the lowest prime factor, and replace 
       the last element with two elements; the prime factor, and the product of the remaining
        factors. 
     */

    public void addNextFactor() {
	  int lastElement = 

		  ____________________________________
	  
	  // in special case where last element is 4, need <= lastElement/2 not < 2
	  for (int i=2; i<=lastElement/2; i++) {
	    if (lastElement % i == 0) {
	     	this.replaceLastFactor(i);
		    this.addFactor(lastElement/i);
		    return;
	    }
	  }
    }

	// NOTE: This method is complete and correct.
	// You don't need it except to understand the context of the other code
	
    public static Factors primeFactors(int num) {
	  Factors f = new Factors(new int []{num});
	  int prevSize = 0;
	  while ( f.size() != prevSize) {
	    prevSize = f.size();
	    f.addNextFactor();
	  }
	  return f;
    }

    /** replace last factor with the value i */
    public void replaceLastFactor(int i) {

		_________________________________
    }

    /**  add factor to end of list  */
    public void addFactor(int f) {

	  _____________________________	
    }

}

End of Handout