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;
        }
    }
}
Melquiades
  • 8,496
  • 1
  • 31
  • 46
theGuy05
  • 417
  • 1
  • 7
  • 22
  • http://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) – JB Nizet Jan 28 '14 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. – peter.petrov Jan 28 '14 at 22:27
  • @peter.petrov: thanks. I learnt something today. – JB Nizet Jan 28 '14 at 22:29

4 Answers4

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

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
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.

Community
  • 1
  • 1
Nigel Tufnel
  • 11,146
  • 4
  • 35
  • 31
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.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
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();
    }

}

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
  • 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 ? – theGuy05 Jan 29 '14 at 17:10
  • And maybe perhaps make each fish have it's own line when printed out? – theGuy05 Jan 29 '14 at 17:16
  • There are a few ways you can do this, i suggest you read [this](http://stackoverflow.com/questions/7854084/java-printing-linkedlist-without-square-brackets) – Ojonugwa Jude Ochalifu Jan 29 '14 at 17:58