2014-02-21 73 views
0

嗨,我一直試圖保持每個對象的浮點數,以便我可以得到所有浮點數的總和。浮動代表重量,所以我試圖做到以下幾點:我宣佈的重量總和爲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(); 
    } 
} 
+0

通常,魚不漂浮... –

回答

3

請發表你的邏輯在那裏你正在增加重量。

sumOfWeight = ++weight將重置sumOfWeight++weight而不是增加weightsumOfWeight

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; 
    } 
} 
+0

拳頭? :D我希望你的意思是魚;) – pL4Gu33

+0

@ pL4Gu33更正:) –

+1

而偉大的代碼,這並不能說明新手做錯了什麼。 – ErstwhileIII

0

能否請您發表您的類的結構使我們可以看到哪些類型的類定義的成員變量。嘗試將sumOfWeight聲明爲靜態varibale,以保持實例化類的所有對象的權重。

+0

當然,我剛剛發佈它! – user3330114

+0

你想在哪裏計算sumOfWeight ... Lake或Fisher ..或兩者? – Kakarot

+0

它並不重要,但我猜漁民會更好 – user3330114

0

你想sumOfWeight = sumOfWeight + weight;

你寫的是「加1,重」,然後設置sumOfWeaght該值是什麼。

相關問題