怎么做我依靠对象中的某些属性

时间:2014-02-21 21:30:11

标签: java oop object attributes floating-point

您好我一直在尝试对每个对象中的浮点数进行计数,以便我可以得到所有浮点数的总和。浮动代表重量所以我试图做以下事情:我宣布一个重量总和为float sumOfWeight;和说sumOfWeight = ++weight;但是它没有用完,有什么建议吗?

  Lake   weirdLake = new Lake(15);
        weirdLake.add(new Fish(76, 6.1f));
        weirdLake.add(new Fish(32, 0.4f));
        weirdLake.add(new Fish(20, 0.9f));
        weirdLake.add(new Fish(30, 0.4f));
        weirdLake.add(new Fish(140, 7.4f));
        weirdLake.add(new Fish(15, 0.3f));
        weirdLake.add(new Fish(90, 5.9f));
        weirdLake.add(new Fish(120, 6.8f));
        weirdLake.add(new Fish(80, 4.8f));
        weirdLake.add(new Fish(42, 3.2f));
        weirdLake.add(new Fish(100, 5.6f));
        weirdLake.add(new Fish(45, 2.0f));
        weirdLake.add(new Fish(16, 0.2f));
        weirdLake.add(new Fish(30, 1.2f));
        weirdLake.add(new Fish(7, 0.1f));

这是我的鱼类:

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

    protected int  size;
    protected float  weight;

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

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

    public boolean canKeep() 
    {
        if(this.size > THROW_BACK_SIZE)
        {
          return true;
        }
        else
        {
        return false;
        }
    }

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

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

这是我的渔夫班:

import java.util.*;

public class Fisher   
{
  private String name;
  private Fish [] fishCaught;
  public int numFishCaught; 
  private int keepSize;
  public static int LIMIT = 10;

  public String getName() 
  {
    return this.name;
  }

  public int getNumFishCaught()
  {
    return this.numFishCaught;
  }

  public int getKeepSize()
  {
    return this.keepSize;
  }

  public Fisher(String n, int k)
  {
    name = n;
    keepSize = k;
  }

  public String toString()
  {
    return(this.name + " with " + this.numFishCaught + " fish");
  }
  private ArrayList<Fish> fishesCaught = new ArrayList<Fish>();


  public void keep(Fish fish) 
  {
    if(this.numFishCaught < LIMIT)
    {
      fishesCaught.add(fish);
      numFishCaught++;
    }
  }

  public boolean likes(Fish fish)
  {
    if(fish.size >= this.keepSize)
    {
      return true;
    }

    else 
    {
      return false;
    }
  }

  public void listThingsCaught() 
  {
    System.out.println(this.toString());

    for(Fish fish : fishesCaught)
    {
      System.out.println(fish.toString());
    }
  }

  public void goFishingIn(Lake lake)
  {
    Fish fish = lake.catchSomething();

    if(likes(fish))
    {
      this.keep(fish);
    }
    else
    {
      lake.add(fish);
    }
  }

  public void giveAwayFish(Fisher fisher, Lake lake)
  {
    for(Fish fish : fishesCaught)
    {
      if(fisher.likes(fish))
      {
        fisher.keep(fish);
      }
      else
      {
        lake.add(fish);
      }
    }
    fishesCaught.clear();
    this.numFishCaught = 0;
  }



}

和我的湖类

public class Lake 
{
    private Fish[]  catchableThings;
    private int   numThings;

    public Lake(int capacity) 
    {
        catchableThings = new Fish[capacity];
        numThings = 0;
    }

    public int getNumCatchableThings() { return numThings; }
    public boolean isFull() { return numThings == catchableThings.length; }
    public String toString() { return "Lake with " + numThings + " catchable things"; }

    // Add the given thing to the lake
    public void add(Fish aCatchableThing) 
    {
        if (numThings < catchableThings.length)
            catchableThings[numThings++] = aCatchableThing;
    }

    // Choose a random thing to be caught in the lake and return it
    public Fish catchSomething() 
    {
        if (numThings == 0) return null;

        int index = (int)(Math.random() * numThings);
        Fish f = catchableThings[index];
        catchableThings[index] = catchableThings[numThings-1];
        catchableThings[numThings-1] = null;
        numThings--;
        return f;
    }

    // List all things in the lake
    public void listAllThings() 
    {
        System.out.println("  " + this + " as follows:");
        for (int i=0; i<numThings; i++)
            System.out.println("    " + catchableThings[i]);
        System.out.println();
    }
}

3 个答案:

答案 0 :(得分:3)

请在您添加重量的位置张贴您的逻辑。

sumOfWeight = ++weight会将sumOfWeight重置为++weight,而不是将weight添加到sumOfWeight

public class Lake {

    private float sumOfWeight;

    public void add(Fish fish) {
        ...
        sumOfWeight += fish.getWeight();
    }

    public void remove(Fish fish) {
        ....
        sumOfWeight -= fish.getWeight();
    }

    public float getLakeWeight() {
        return this.sumOfWeight;
    }
}

答案 1 :(得分:0)

请你发布你的类的结构,以便我们可以看到你的类中定义的成员变量的类型。尝试将sumOfWeight声明为静态varibale,以保持实例化类的所有对象的权重计数。

答案 2 :(得分:0)

您想要sumOfWeight = sumOfWeight + weight;

你写的是&#34;加1加重&#34;然后将sumOfWeaght设置为该值。