1

I am having a hard time figuring out why it is, when I call a specific object from my arrayList, it does not return the output I desire? I am asked create 3 new objects and add them to the arraylist using the add method, call an invalid parameter with get at 4, and a valid one that is at 2, display the details then remove 4 and 2. The output I am getting from my main method is

Invalid index position
2
The current guests in Puss in Boots Cattery:
Garfield
Bob
John
Invalid index position
The current guests in Puss in Boots Cattery:
Garfield
John

I don't understand why I am getting just "2" as my output instead of the cats name "John" at which should be stored in 2?

package pkg4;

import java.util.ArrayList;

public class Cattery {  
    private ArrayList<Cat> catList;
    private String businessName;

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

    public void addCat(Cat newCat) {

        catList.add(newCat);
    }

    public void getCat(int index) {
        if ((index >= 0) && (index <= catList.size() - 1)) {
            Cat oneCat = catList.get(index);
            System.out.println(index);
        } else {
            System.out.println("Invalid index position");
        }
    }

    public void removeCat(int indexRemove) {
        if ((indexRemove >= 0) && (indexRemove <= catList.size() - 1)) {
            catList.remove(indexRemove);
        } else {
            System.out.println("Invalid index position");
        }
    }

    public void displayAllCats() {
        System.out.println("The current guests in Puss in Boots Cattery:");
        for (Cat catNames : catList) {
            System.out.println(catNames.getName());
        }
    }

    public static void main(String[] args) {
        Cattery allCats = new Cattery("Puss In Boots Cattery");
        Cat c1 = new Cat("Garfield", 2015, 10);
        Cat c2 = new Cat("Bob", 2020, 5);
        Cat c3 = new Cat("John", 2019, 9);
        allCats.addCat(c1);
        allCats.addCat(c2);
        allCats.addCat(c3);
        allCats.getCat(3);
        allCats.getCat(2);
        allCats.displayAllCats();
        allCats.removeCat(3);
        allCats.removeCat(1);
        allCats.displayAllCats();
    }
}

Class Cat

public class Cat {
    private String name;
    private int yearOfBirth;
    private int weightInKilos;

    public Cat(String inputName, int inputYearOfBirth, int inputWeigthInKilos) {
        setName(inputName);
        setYearOfBirth(inputYearOfBirth);
        setWeigthInKilos(inputWeigthInKilos);
    }

    public String getName() {
        return name;
    }

    public int getYearOfBirth() {
        return yearOfBirth;
    }

    public int getWeightInKilos() {
        return weightInKilos;
    }

    public void setName(String name) {
        if (name != null && !name.isEmpty()) {
            this.name = name;
        } else if (name == null) {
            throw new IllegalArgumentException("name cannot be null");
        } else if (name.isEmpty()) {
            throw new IllegalArgumentException("name cannot be an empty String");
        }
    }

    public void setYearOfBirth(int yearOfBirth) {
        if (yearOfBirth > 0) {
            this.yearOfBirth = yearOfBirth;
        } else {
            throw new IllegalArgumentException("year of birth cannot be negative");
        }
    }

    public void setWeigthInKilos(int weigthInKilos) {
        if (weigthInKilos > 0) {
            this.weightInKilos = weigthInKilos;
        } else {
            throw new IllegalArgumentException(" weight in kilos cannot be negative");
        }
    }
}
0

2 Answers 2

2

If you want to print the cat object, please override toString method in Cat class by the following:

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", yearOfBirth=" + yearOfBirth +
                ", weightInKilos=" + weightInKilos +
                '}';
    }

, And update this code to print cat object

public void getCat(int index) {
    if ((index >= 0) && (index < catList.size())) {
        Cat oneCat = catList.get(catList.get(index));                
        System.out.println(oneCat);
    }
    else{
        System.out.println("Invalid index position");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, exactly. 1+
2

you're printing the index.

public void getCat(int index) {
    if ((index >= 0) && (index < catList.size())) {
        Cat oneCat = catList.get(index);                
        System.out.println(index);  // **** here ****
    }
    else{
        System.out.println("Invalid index position");
    }
}

If you want to print the Cat then have System.out.println(oneCat);


Note that the method is flawed to begin with, in that it is a getter method and so it shouldn't print a Cat but rather return one:

public Cat getCat(int index) {
    if ((index >= 0) && (index < catList.size())) {
        Cat oneCat = catList.get(index);                
        return oneCat;
    } else{
        // you really should *throw* an exception here
    }
}

Then you could do in your main method:

try {
    System.out.println(allCats.getCat(2));
} catch (IllegalArgumentException e) {
    e.printStacktrace();
}

Some key points:

  • Your current code can divided into logical classes, those that hold the "model" of the issues that you're dealing with, here Cat, and UI or "user interface" classes, those dealing with getting information from the user and returning information to the same
  • Your logical classes should not have UI code within them, meaning you shouldn't be getting or returning input from or to the user within these classes. No println's, no Scanner, nothing. The only exception being if you're using System.out.println as a temporary "poor man's debugger" -- to print out the state of key variables as the program runs, with the ultimate plan of removing these statements in the finished product
  • The user input and output should be separate in your UI class, or for very simple programs like this, in the main method.
  • Any getter method should do just that -- get and return requested in formation. Yours is marked as void and prints out data which makes the method fairly useless in the long term scheme of things.
  • Give logical classes a public String toString() override that allows you to test and output an object's state. In the beginning, you will use these for your program's output, but later in your education they will be more for debugging purposes.

1 Comment

Thank you so much Hovercraft!! the toString() override did get me the desired output I wanted for my third object in the arrayList! Keep being awesome and educating us poor sleep deprived students :)

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.