Question

Hi everyone at the moment, my toString() looks like this:

public String toString () 
    {
        return ("A " + size + "cm " + weight +  "kg Fish" );
    }

which prints out something like this: "A 16cm 0.2kg Fish"

I am looking for it print out something like this: "A 16cm 0.2kg Bass", where Bass inherits its attributes from a NonEndangeredFish class which inhertis its attributes from a Fish class. The to String is currently located in the Fish class. This is the Fish class:

public abstract class Fish 
{
    // Any fish below this size must be thrown back into the lake
    public static int  THROW_BACK_SIZE = 18; 
    public static float WEIGHT_LIMIT = 10;

    protected float weight;
    protected int  size;


    public Fish(int aSize, float aWeight) 
    {
        size = aSize;
        weight = aWeight;
    }


    public boolean isDesirableTo(Fisher f) 
    {
        if(canKeep() && f.numFishCaught < f.LIMIT && this.weight + f.sumOfWeight < WEIGHT_LIMIT)
        {
          return true;
        }
        else
        {
        return false;
        }
    }

    public abstract boolean canKeep(); 

    public int getSize() { return size; }
    public float getWeight() { return weight; }

    public String toString () 
    {
        return ("A " + size + "cm " + weight +  "kg" );
    }

this is the superclass. An EndangeredFish class and a NonEndangeredFish class extends the Fish class (which are both abstract). Then a Perch class and Bass class extend the NonEndangeredFish class and the AtlanticWhiteFish class and the AuroraTrout class extends the Endangered class. my question is how would I use a class name in a toString()? I tried declaring a string in the Perch, AuroraTrout, AtlanticWhiteFish and Bass called name and using it in the toString and it did not work, then I tried declaring a name (a String) in the fish class and then making a constructor with three parameters but that didnt work so Im really stuck. This is how a fish is added:

public class FishingTestProgram2 
{

    public static void main(String [] args) 
    {
        // Create a big lake with 15 fish
        Lake   weirdLake = new Lake(15);
        weirdLake.add(new AuroraTrout(76, 6.1f));
        weirdLake.add(new Perch(32, 0.4f));
        weirdLake.add(new Bass(20, 0.9f));
        weirdLake.add(new Perch(30, 0.4f));
        weirdLake.add(new AtlanticWhiteFish(140, 7.4f));
        weirdLake.add(new Bass(15, 0.3f));
        weirdLake.add(new Bass(90, 5.9f));
        weirdLake.add(new Bass(120, 6.8f));
        weirdLake.add(new AtlanticWhiteFish(80, 4.8f));
        weirdLake.add(new AuroraTrout(42, 3.2f));
        weirdLake.add(new Bass(100, 5.6f));
        weirdLake.add(new Bass(45, 2.0f));
        weirdLake.add(new Perch(16, 0.2f));
        weirdLake.add(new Bass(30, 1.2f));
        weirdLake.add(new Perch(7, 0.1f));

Any help is appreciated Thank You!

Was it helpful?

Solution

do it like this:

public String toString () 
{
    return ("A " + size + "cm " + weight +  " " + this.getClass().getSimpleName() );
}

OTHER TIPS

I suspect that they were supposed to overwrite toString() in every concrete class (I'm not saying that it's better):

class Bass {

   ...
   public String toString() {
      return super.toString() + " Bass"; 
   }

   ...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top