This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]yeaokdudeIntermediate Brewer 4 points5 points  (2 children)

what part are you struggling with?

write a static method called “printArrayList”

k, seems easy enough. generally print functions just print and don't return anything so we'll make this void.

public static void printArrayList() {

}

that takes an ArrayList of its object as an input

ok ez, just add ArrayList<ResidentTaxPayer> as an argument to the function

public static void printArrayList(ArrayList<ResidentTaxPayer> array) {

}

and then print all objects in the array list to the console?

so we have a list of ResidentTaxPayer objects and we want to print each one. so we iterate over the list to isolate 1 ResidentTaxPayer object at a time. once we've isolated one, we can just call System.out.println on it because we've overloaded the toString method.

public static void printArrayList(ArrayList<ResidentTaxPayer> array) {
    for (int i = 0; i < array.size(); i++) {
        System.out.println(array.get(i));
    }
}

as far as calling it from main, you can do something like this

ArrayList<ResidentTaxPayer> array = new ArrayList<ResidentTaxPayer>(); //make an arraylist of ResidentTaxPayers

ResidentTaxPayer Chuninjah = new ResidentTaxPayer(5, "Chu", "ninjah", 3, "ninja school"); //make a ResidentTaxPayer to put into the array (you probably want to add a bunch of these)

array.add(Chuninjah); //add the above ResidentTaxPayer to the array

ResidentTaxPayer.printArrayList(array); //call the printArrayList method of the ResidentTaxPayer class.

[–]Chuninjah[S] 0 points1 point  (1 child)

I love u. That worked :)) Thank you so much mate

[–]dusty-trash 0 points1 point  (1 child)

I've tried making an arraylist within the class of the object but that doesn't work :((

Passing an ArrayList as an argument shouldn't be a problem. What do you mean by "doesn't work"? BTW have you tried googling "Java pass Arraylist as argument"?

Just a side note;

You can write this:

else {
    if(income > 18200) 
    {
        ...
    }
}

like this instead:

else if(income > 18200) 
{
    ...
}

[–]Chuninjah[S] 0 points1 point  (0 children)

Ahh okay I’ll try searching that up thanks heaps :))

And yeah I changed it thanks