hello again,
i am trying to loop through an ArrayList of type object. the objects have their own attributes and i'm trying to call a specific method that accesses a specific attribute. in this case, the objects refer to pets and i am trying to total up their leg count in separate java files. i'll have to call this method in my tester class.
here is PetTester.java
import java.util.ArrayList; /* This program tests the Pet superclass and its subclasses */ public class PetTester { public static void main(String [] args) { Cat spider = new Cat("Spider", "Alley","Black", "crystals", 7); Dog rover = new Dog("Rover", "Labrador","Chocolate", true, "large", 100); Dog tippi = new Dog("Tippi", "Poodle","Silver",false, "small", 15); Bird toucan = new Bird("Nismo", "toucan", 2, true); ArrayList<Pet> pets = new ArrayList<Pet>(); pets.add(spider); pets.add(rover); pets.add(tippi); pets.add(toucan); String spiderName = spider.getName(); String spiderLitterType = spider.getLitterType(); String spiderColor = spider.getColor(); String spiderBreed = spider.getBreed(); int spiderLegs = spider.getLegs(); System.out.println(spiderName + " who is of type " + spiderBreed + " and color " + spiderColor + " which uses " + spiderLitterType + " litter has " + " legs: "+ spiderLegs); String roverName = rover.getName(); boolean roverTraining = rover.getTrained(); String roverSize = rover.getSize(); String roverColor = rover.getColor(); String roverBreed = rover.getBreed(); int roverLegs = rover.getLegs(); System.out.println(roverName + " who is of type " + roverBreed + " and color " + roverColor + " and " + roverSize + " size which is trained " + roverTraining + " has " + " legs: "+ roverLegs); System.out.println(tippi.getName() + " who is of type " + tippi.getBreed() + " and color " + tippi.getColor() + " and " + tippi.getSize() + " size which is trained " + tippi.getTrained() + " weighs " + tippi.getWeight() + " has " + " legs: " + tippi.getLegs()); System.out.println(toucan.getName() + " is a " + toucan.getBreed() + " who has " + toucan.getLegs() + " legs, and " + toucan.canTalk() + " talk."); } }
now here is Pet.java where this method, totalLegs(ArrayList<Pet> pets) is actually created.
import java.util.ArrayList; public class Pet { String name; String breed; String color; int legs; int weight; boolean speaks; public Pet(String name, String breed, String color, int legs, int weight) { this.name = name; this.breed = breed; this.color = color; this.legs = legs; this.weight = weight; } public Pet(String name, String breed, String color, int weight) { this.name = name; this.breed = breed; this.color = color; this.legs = 4; this.weight = weight; } public Pet(String name, String breed, int legs, boolean speaks) { this.name = name; this.breed = breed; this.legs = 2; this.speaks = speaks; } public Pet() { this.name = ""; this.breed = "mutt"; this.color = "black"; this.legs = 4; this.weight = 0; } public String getName() { return name; } public String getBreed() { return breed; } public String getColor() { return color; } public int getLegs() { return legs; } public int getWeight() { return weight; } [B]public static int totalLegs(ArrayList<Pet> pets) { int totalLegs = 0; for (int i = 0; i < pets.size(); i++) { totalLegs = pets.getLegs(); }[/B] } }
how do i access their parameters and add them all up?