theGuy05
theGuy05

Reputation: 437

Making an array of objects

I have made a class called Fish:

public class  Fish {

    private String species;
    private int size;

    //Constructor
    public Fish(int x, String s) {
        size = x;
        species = s;
    }

    public String getSpecies() { return species; }

    public int getSize() { return size; }

    public String toString() {
        return String.format("A %dcm %s", size, species);
    }
}

And I have also started to make a class called pond that is meant to have an attribute called 'fish' that holds an array of Fish objects. I am unsure of how to do this. Here is my attempt so far. I am

public class Pond {
    private int capacity;
    private Object[] fish; //This is what I am trying to initialize. list of Fish. 
    private int numFish;

    //Capacity Constructor 
    public Pond(int n, int c) {
        n = numFish;
        c = capacity; 
    }

    public int getNumFish() { return numFish; }

    public boolean isFull() {
        boolean isFull = false;
        if (numFish >= capacity) {
            isFull = true;
        }
        else {
            isFull = false;
        }
        return isFull;
    }

    public String toString() {
        return String.format("Pond with %d fish", numFish);
    }

    public void add(Fish aFish) {
        if (numFish <= capacity) {
            numFish += 1;
            fish.add Fish;
        }
        else {
            numFish += 0;
        }
    }
}

Upvotes: 2

Views: 362

Answers (4)

Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27255

You need to use ArrayList instead of Array since an ArrayList can grow according to requirements.

Take a look at this code.Should help you:

public class Fish {
String name;


public Fish(String name) {

    this.name=name; 

}

public String toString() {
    return name;
}
}

And then:

import java.util.*;

public class Pond {

ArrayList<Fish> fishInPond = new ArrayList<>();


public void addFish(Fish e) {

    fishInPond.add(e);
}

public void showFishes() {
    for (int i= 0; i<fishInPond.size();i++) {
        fishInPond.get(i);
   } 
    System.out.println("Fishes in my pond: " + fishInPond);
}


    public static void main(String[]args) {
        Pond myPond = new Pond();
        myPond.addFish(new Fish("Tilapia"));
        myPond.addFish(new Fish("cat fish"));

        myPond.showFishes();
    }

}

Upvotes: 0

Nigel Tufnel
Nigel Tufnel

Reputation: 11534

In the Pond constructor you're assigning private fields to constructor arguments. I think it should be the other way around:

public Pond(final int n, final int c) {
    numFish = n;
    capacity = c;
}

A side note: declaring Pond constructor arguments final would prevent these kind of error at compile time.

Also, if you want to expand fish array at runtime then array is not the best choice of the container type. ArrayList<Fish> is a better choice as it can expand at runtime.

Upvotes: 1

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Following is invalid -

fish.add aFish;

with arrays you do

fish[numFish] = aFish; //increment numFish after this

You also need to initialize your array

fish = new Fish[capacity];

in your constructor.

Upvotes: 1

peter.petrov
peter.petrov

Reputation: 39477

Change this:

private Object[] fish;

as follows:

private Fish[] fish;

i.e. these are fishes and not just any
kinds of objects (they not mammals e.g.).

Upvotes: 1

Related Questions