Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives Work with an ArrayList. Use static methods. Requirements Sample Outpu

ID: 3562148 • Letter: O

Question

Objectives

Work with an ArrayList. Use static methods.

Requirements

Sample Output

Here is what your program output should look like.

package cs2302.measures;

/**
* Created by Ben on 9/1/2014.
*/
public class Examples {

public static final Length len1 = new Length(2.3, "mm");
public static final ScaledLength scl1 = new ScaledLength(9.8, "mm", 1.2);
public static final Length len2 = new Length(3.4, "cm");
public static final ScaledLength scl2 = new ScaledLength(8.7, "cm", 2.3);
public static final Length len3 = new Length(4.5, "km");
public static final ScaledLength scl3 = new ScaledLength(7.6, "km", 3.4);
public static final Length len4 = new Length(5.6, "in");
public static final ScaledLength scl4 = new ScaledLength(6.5, "in", 4.5);
public static final Length len5 = new Length(6.7, "ft");
public static final ScaledLength scl5 = new ScaledLength(5.4, "ft", 5.6);
public static final Length len6 = new Length(7.8, "league");
public static final ScaledLength scl6 = new ScaledLength(4.3, "league", 6.7);
public static final Length len7 = new Length(8.9, "furlong");
public static final ScaledLength scl7 = new ScaledLength(3.2, "furlong", 7.8);
public static final Length len8 = new Length(9.0, "mil");
public static final ScaledLength scl8 = new ScaledLength(2.1, "mil", 8.9);
public static final Length len9 = new Length(0.1, "fermi");
public static final ScaledLength scl9 = new ScaledLength(1.0, "fermi", 9.0);

public static final Length[] allExamples = {
len1, scl1, len2, scl2, len3, scl3, len4, scl4, len5, scl5,
len6, scl6, len7, scl7, len8, scl8, len9, scl9,
};

}

package cs2302.measures;

import java.util.ArrayList;

/**
* Some Utilities for the Length family of classes.
* These mainly operate on collections of Length objects.
*
* Created by Ben on 9/1/2014.
*/
public class Utilities {

public static double getTotalValue(ArrayList list) {
double total = 0.0;
for(Length len : list) {
total += len.getValue();
}
return total;
}

/**
* Returns the maximum value of the Lengths in the list.
*
* @param list The list should be non-empty.
* @return The maximum value of the lengths in the list.
*/
public static double getMaximumValue(ArrayList list) {
double max = list.get(0).getValue();
for(Length len : list) {
max = Math.max(max, len.getValue());
}
return max;
}

/**
* Returns the minimum value of the Lengths in the list.
*
* @param list The list should be non-empty.
* @return The minimum value of the lengths in the list.
*/
public static double getMinimumValue(ArrayList list) {
double min = list.get(0).getValue();
for(Length len : list) {
min = Math.min(min, len.getValue());
}
return min;
}
}

Explanation / Answer

0 down vote accepted

Without information about Bar, or the actual error it complains about, we can't help much.

I'd presume its because the Bar.add method isn't static, or Bar isn't static.

or do you intend "Bar" to be a static list member of something else?

then your code would need a OtherClass.Bar.add(o) instead of just Bar.add(o);

0 down vote accepted

Without information about Bar, or the actual error it complains about, we can't help much.

I'd presume its because the Bar.add method isn't static, or Bar isn't static.

  // a class with a public static method that encapsulates a static list  public class Bar  {      static List<Foo> innerlist = new ArrayList<Foo>();        public static void add( Foo o )      {          innerList.add(o);      }  }  

or do you intend "Bar" to be a static list member of something else?

  // a class with a static member  public class OtherClass  {       public static List<Foo> Bar = new ArrayList<Foo>();  }  

then your code would need a OtherClass.Bar.add(o) instead of just Bar.add(o);