public int getTotalWeight() { int totalWeight = 0; for (Object eachObject: this.objects) totalWeight += eachObject.getWeight(); return totalWeight; }
public int getTotalWeight() {
int totalWeight = 0;
for (Object eachObject: this.objects)
totalWeight += eachObject.getWeight();
return totalWeight;
}
public int getTotalWeight() {
int totalWeight = 0;
for (Weighable eachObject: this.objects)
totalWeight += eachObject.getWeight();
return totalWeight;
}
public final int getWeight()
public interface Weighable {
public int getWeight();
}
#1public class Elephant2 implements Weighable { private int weight; public Elephant2(int aWeight) { this.weight = aWeight; }#2public int getWeight() { return weight; } }
public class Zoo2 { private String name; private List<Weighable> objects; public Zoo2(String aName) { this.name = aName; this.objects = new ArrayList<>(); } public void add(Weighable aObject) { this.objects.add(aObject); } public int getTotalWeight() { int totalWeight = 0; for (Weighable eachObject: this.objects) totalWeight += eachObject.getWeight(); return totalWeight; } public String toString() { return String.format("%s -- Total Weight of Objects = %d", this.name, getTotalWeight()); } }
public int getTotalWeight() { int totalWeight = 0; for (Weighable eachObject: this.objects) totalWeight += eachObject.getWeight(); return totalWeight; }