0

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();
}
}
2
  • 3
    Try calling "here kitty", though tapping its food bowl may work better :-). Commented Nov 15, 2014 at 1:07
  • Call cats.add(new Cat(...))? Commented Nov 15, 2014 at 1:09

2 Answers 2

1

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

public void addCat(Cat name)
{
    cats.add(name);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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());
    }
}

3 Comments

@ajb Milo is a gluttonous cat.
I'm more worried about the number of cats "ready for consumption". Do you regularly eat cats ???
@StephenC You mean you have never had cat? :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.