2

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;
        }
    }
}
3
  • docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html. BTW, I can't see any question. Just two notes: if it's an array of fishes, it should be of type Fish[], and be named fishes (array ==> multiple fishes ==> plral form) Commented Jan 28, 2014 at 22:25
  • 1
    @JBNizet fish is also plural for fish, I think (not a native speaker here). But yes, in this context fishes is probably better. Commented Jan 28, 2014 at 22:27
  • @peter.petrov: thanks. I learnt something today. Commented Jan 28, 2014 at 22:29

4 Answers 4

1

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.).

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

0

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

}

3 Comments

Thanks a bunch! This helped me a lot. Now would there be any way to remove the square brackets surrounding the listed fish? For example instead of displaying [Tilapia, cat fish] it would display Tilapia, cat fish ?
And maybe perhaps make each fish have it's own line when printed out?
There are a few ways you can do this, i suggest you read this

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.