简体   繁体   中英

How do you call methods into an object array?

I have two classes. A class called Cat, that holds the cat names, birth year and weight in kilos. I have a class called Cattery that is an array. I want to input cats from the Cat class into the array. Each cat will have its own name, birth year and weight in Kilos. How do I do this? Thank you.

public class Cat {

    private String name;
    private int birthYear;
    private double weightInKilos;

/**
 * default constructor
 */ 
public Cat() {   
}

/**
 * @param name
 * @param birthYear
 * @param weightInKilos
 */
public Cat(String name, int birthYear, double weightInKilos){   
    this.name = name;
    this.birthYear = birthYear;
    this.weightInKilos = weightInKilo
}

/**
 * @return the name.
 */
public String getName() {
    return name;
}

/**
 * @return the birthYear.
 */
public int getBirthYear() {
    return birthYear;
}

/**
 * @return the weightInKilos.
 */  
public double getWeightInKilos() {
    return weightInKilos;
}

/**
 * @param the name variable.
 */
public void setName(String newName) {
    name = newName; 
}

/**
 * @param the birthYear variable.
 */
public void setBirthYear(int newBirthYear) {
    birthYear = newBirthYear;
}

/**
 * @param the weightInKilos variable.
 */
public void setWeightInKilos(double newWeightInKilos) {
    weightInKilos = newWeightInKilos;
} 
}

The array class.

import java.util.ArrayList;

public class Cattery {

    private ArrayList<Cat> cats;
    private String businessName;

/**
 * @param Cattery for the Cattery field.
 */

public Cattery() {
    cats = new ArrayList<Cat>();
    this.businessName = businessName;
}

/**
 * Add a cat to the cattery.
 * @param catName the cat to be added.
 */
public void addCat(Cat name)
{
    Cat.add(getName());
}

/**
 * @return the number of cats.
 */
public int getNumberOfCats()
{
    return cats.size();
}
}

just edit the "addCat" method to pass object from argument to your ArrayList.

public void addCat(Cat name)
{
    cats.add(name);
}

It seems that what you are trying to do is add cats to the cats list you declared in your Cattery . Another answer already notes the correction needed - your addCat method must be modified then to actually put the cats in the list. Note that you're not adding the cat name, but an actual Cat object. It also seems like you want the Cattery to have a business name. You can pass that in to the constructor.

public Cattery(String businessName) {
    cats = new ArrayList<Cat>();
    this.businessName = businessName;
}
...
public void addCat(Cat cat)
{
    cats.add(cat);
}

Here's an example of how you may create your cattery and subsequently add cats. You must love cats.

class CatApp{
    public static void main(String[] args) {
        Cattery cattery = new Cattery("The Delicious Cats Business");
        cattery.addCat(New Cat("Milo", 3, 388.87));
        cattery.addCat(New Cat("Otis", 2, 1.4));
        System.out.println("Total number of cats ready for consumption = " 
            + cattery.getNumberOfCats());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM