2013-02-25 93 views
0

我想用我所需要的所有方法編寫一個類,但我無法弄清楚如何使用這些方法確定所需的值。我遇到的問題是getAverageLength和getCount。編寫一個方法來計算長度/平均值

電流輸出:

The length of Line 1 is 1.0 
The length of Line 2 is 10.0 
There are 0 lines and the average length is 0 
The length of Line 1 is 1.0 
The length of Line 2 is 10.0 
The length of Line 3 is 7.0 
There are 0 lines and the average length is 0 

預期輸出:

The length of Line 1 is 1 
The length of Line 2 is 10 
There are 2 lines and the average length is 5.5 
The length of Line 1 is 7 
The length of Line 2 is 10 
The length of Line 3 is 7 
There are 3 lines and the average length is 8.0 

這是我使用我的主要方法的一部分。

public class TestParts { 

public static void main(String[] args) { 

    MyLine ml1 = new MyLine(); 
    MyLine ml2 = new MyLine(10); 
    System.out.println("The length of Line 1 is " + ml1.getLength()); 
    System.out.println("The length of Line 2 is " + ml2.getLength()); 
    System.out.println("There are " + MyLine.getCount() + 
    " lines and the average length is " + MyLine.getAverageLength()); 
    MyLine ml3 = new MyLine(7); 
    ml1.setLength(7); 
    System.out.println("The length of Line 1 is " + ml1.getLength()); 
    System.out.println("The length of Line 2 is " + ml2.getLength()); 
    System.out.println("The length of Line 3 is " + ml3.getLength()); 
    System.out.println("There are " + MyLine.getCount() + 
    " lines and the average length is " + MyLine.getAverageLength()); 
    } 
} 

而下面是我寫的計算值的單獨類。

class MyLine { 
private double getLength; 

MyLine() { 
    getLength = 1; 
} 

double getLength() { 
    return getLength; 
} 

MyLine(double setLength) { 
    getLength = setLength; 
} 

public void setLength(int i) { 
    getLength = getLength(); 
} 

public static int getCount() { 

    return 0; 
} 

public static int getAverageLength() { 

    return 0; 
} 

} 
+0

您的預期和實際產出是多少? – Jayamohan 2013-02-25 00:42:49

+0

我剛剛添加了實際和預期的問題。 – user2057847 2013-02-25 00:44:49

+0

你的setLength方法什麼都不做。事實上,你的班級有很多問題,我會放棄並重新開始。 – Bohemian 2013-02-25 00:58:16

回答

1

對於getCount,使由每個構造遞增static int

對於getAverageLength,請使用每個構造函數添加的行的總和作爲static int,並將其除以計數。

+0

感謝您的建議,但我不知道如何做到這一點。 – user2057847 2013-02-25 00:57:37

+0

在該類中,創建一個'static int count'和一個'static int sum'。然後,在每個構造函數中,在底部添加'count ++;'和'sum + = getLength;'。 – MathSquared 2013-02-25 00:59:34

0

代碼有幾個問題。 首先,建議記錄方法應該在評論中做什麼。 這會幫助你和其他人。 其次,這些方法:

public void setLength(int i) { 
    getLength = getLength(); 
} 

getLength私有成員變量可能是嚴重命名。 我懷疑意圖是一個名詞,如length,而getLength()是一種旨在返回當前length的方法。 此方法採用基本的int數據類型來設置類型double。 這是故意的嗎? 看起來像一個誤解。 此外,它沒有功能,因爲它只是將getLength(再次,應該是length)變量設置爲方法getLength()的返回值,而返回值又返回值getLength。這是一個補救邏輯和基本的數學題: A = 1 =的getLength()= A = 1

public static int getCount() { 

    return 0; 
} 

爲什麼不是零以外的任何其他有望從這個方法返回的?

公共靜態INT getAverageLength(){

return 0; 

}

同樣在這裏...基本邏輯問題。

我不會在論壇上發佈這類問題,因爲在提出問題之前,它缺乏基本的功課。

+0

0的佔位符只是爲了顯示該方法,認爲這很明顯。 – user2057847 2013-02-25 01:08:03

+0

該問題陳述了實際與預期結果:有0行,平均長度爲0 ...與預期:有2行,平均長度爲5.5。如果這些值明確地設置爲零,那麼預期的結果怎麼可能不是零? – 2013-02-25 20:42:09

0

build a HashMap<MyLine, Integer> Integer是MyLine的長度。

您只需將那些要計入該對象的MyLine對象。

{ml1:0, ml2:10, ml3:7}

,那麼你可以計算出你想要的一切。