Chapter 6: ArrayLists – DataSet

DataSet class should look like this:

import java.util.ArrayList; 
/**
   This class computes the average of a set of data values.
*/
public class DataSet
{
   /**
      Constructs an empty data set.
   */
   public DataSet()
   {
      create ArrayList
      /*  write your code here  */
   }

   /**
      Adds a data value to the data set
      @param x a data value
   */
   public void add(double x)
   {
    /*  write your code here  */
   }

   /**
      Gets the average of the added data.
      @return the average or 0 if no data has been added
   */
   public double getAverage()
   {
      /*  write your code here  */
   }

   private ArrayList data;
}

ArrayLists with Generic Types

The type ArraList identifies an array list of bank accounts.

The angle brackets around the BankAccount type tell you that BankAccount is a type parameter.

Use an ArrayList whenever you want to collect objects of type T.

However, you cannot use primitive types as type parameters — there is no ArrayList or ArrayList.

ArrayList accounts = new ArrayList();
accounts.add(new BankAccount(100));
accounts.add(new BankAccount(105));
accounts.add(new BankAccount(102));

An Object of a Wrapper Class

Wrapper objects can be used anywhere that objects are required instead of primitive type values. For example, you can collect a sequence of floating-point numbers in an ArrayList.

Conversion between primitive types and the corresponding wrapper classes is automatic. This process is called auto-boxing (even though auto-wrapping would have been more consistent).

For example, if you assign a number to a Double object, the number is automatically “put into a box”, namely a wrapper object.
Double d = 29.95; // auto-boxing; same as Double d = new Double(29.95);

Wrapper objects are automatically “unboxed” to primitive types.
double x = d; // auto-unboxing; same as double x = d.doubleValue();

ArrayList data = new ArrayList();
data.add(29.95);
double x = data.get(0);

Classwork:
1. Change DestinysChild and Recipe classes to use parameter types.

2. Create a class DataSet of BankAccounts. The constructor creates an ArrayList. Write the test class to add, delete and print bank accounts.

3. Create a class Purse that contains an ArrayList of Coin(s). Write the test class to add, delete, and print coins.