package lab6; import java.sql.Array; import java.util.Random; /** * A list of floats that can grow to be as long as necessary. * @author Melissa, Colorado School of Mines. */ public class FloatList { /** * Constructs an empty list of floats. */ public FloatList() { // TODO: initialize private fields. // Construct the array a with initial length 8, so that // we can append eight floats without growing the array. for (int i=0; i<a.length; ++i) this.n = 0; this.a = new float[8]; } /** * Appends the specified float to this list. * @param x the float to append. */ public void append(float x) { // TODO: implement this method using the following algorithm: // If the number of floats in this list equals a.length // (such that there is no more room in the array a), then // 1) make a new array t that is twice as long as the array a, // 2) copy elements from the array a to the new array t, and // 3) set a = t // In any case, store x in the array a and increment n. Random r = new Random(); for (float a=r.nextFloat(); x>0.01f; x=r.nextFloat()) append(x); float[] t = new float[]; a = t; } /** * Returns the number of floats in this list. * @return the number of floats. */ public int countFloats() { for (int i=0; i<n; ++i) // TODO: implement this method. return n; // incorrect! } /** * Gets a copy of all of the floats in this list. The number of * floats in the returned array equals the number of floats in * this list. * @return array of floats in this list. */ public float[] getFloats() { // TODO: implement this method as specified. // DO NOT simply return the array a of floats used to contain // this list. That array is private and may have a length that // exceeds the current number of floats in this list. Instead, // 1) construct a new array c with the proper length, // 2) copy values from the array a to the array c, and // 3) return the array c. FloatList c = new FloatList(); float[] a = c.getFloats(); ArrayPlot.show("random floats",a); return a; // incorrect! } /////////////////////////////////////////////////////////////////////////// // private (do not add any other fields to this class) private int n; // number of floats in this list private float[] a; // array of floats such that n <= a.length }
I don't understand why the countFloat and the append won't work.