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 problemFreeCandy.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 ; 
    } 
} 
 
class java.util.ArrayList<E> 
  The following excerpts from the javadoc for java.util.ArrayList<E> may be
helpful to you in completing this exam.
  Inheritance Hierarchy (complete) 
  java.lang.Object
  java.util.AbstractCollection<E>
    java.util.AbstractList<E>
      java.util.ArrayList<E>
    
      
        
          All Implemented Interfaces: 
          Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess 
        
          Direct Known Subclasses: 
          AttributeList, RoleList, RoleUnresolvedList 
       
    
   
  Constructors (complete) 
  
    
      
        
          ArrayList()Constructs an empty list with an initial capacity of ten. 
         
        
          ArrayList(Collection<? extends E> c)Constructs a list containing the elements of the specified collection, 
         
        
          ArrayList(int initialCapacity)Constructs an empty list with the specified initial capacity. 
         
       
    
   
  Most important methods, with brief description 
  
    
      
        
          booleanadd(E e)Appends the specified element to the end of this list. 
         
        
          voidadd(int index, E element)Inserts the specified element at the specified position in this list. 
         
        
          voidclear()Removes all of the elements from this list. 
         
        
          Eget(int index)Returns the element at the specified position in this list. 
         
        
          intindexOf(Object o)Returns the index of the first occurrence of the specified element in this list,  
         
        
          booleanisEmpty()Returns true if this list contains no elements. 
         
        
          intlastIndexOf(Object o)Returns the index of the last occurrence of the specified element in this list, 
         
        
          Eremove(int index)Removes the element at the specified position in this list. 
         
        
          booleanremove(Object o)Removes the first occurrence of the specified element from this list, if it is present. 
         
        
          Eset(int index, E element)Replaces the element at the specified position in this list with the specified element. 
         
        
          intsize()Returns the number of elements in this list. 
         
        
          voidsort(Comparator<? super E> c)Sorts this list according to the order induced by the specified Comparator. 
         
       
    
   
  Additional methods, listed by method signature only. 
  
    
      
        
          boolean addAll(Collection<? extends E> c)boolean	addAll(int index, Collection<? extends E> c) 
        
          Object   clone()boolean  contains(Object o) 
        
          void	   ensureCapacity(int minCapacity)void forEach(Consumer<? super E> action) 
        
          Iterator<E> iterator()ListIterator<E>  listIterator() 
        
          ListIterator<E> listIterator(int index)boolean removeAll(Collection<?> c) 
        
          boolean removeIf(Predicate<? super E> filter)protected void removeRange(int fromIndex, int toIndex) 
        
          void replaceAll(UnaryOperator<E> operator)boolean retainAll(Collection<?> c) 
        
          Spliterator<E>  spliterator()List<E> subList(int fromIndex, int toIndex) 
        
          Object[] toArray()<T> T[] toArray(T[] a) 
        
          void    trimToSize()  
         
       
    
   
  Methods inherited from: 
  
    
      
        
          class java.util.AbstractListequals, hashCode 
        
          class java.util.AbstractCollectioncontainsAll, toString 
        
          class java.lang.Objectfinalize, getClass, notify, notifyAll, wait, wait, wait 
        
          interface java.util.ListcontainsAll, equals, hashCode 
        
          interface java.util.CollectionparallelStream, stream 
       
    
   
 
	End of Handout