2013-02-25 70 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}

,那么你可以计算出你想要的一切。