2

私はJavaが初めてで、これらのメソッドに対して何をする必要があるのか​​ わかりません。これをコードに変換しようとしているだけです。どのオブジェクトがメソッドを呼び出しているかなどについて混乱していると思います。など。

したがって、特定の寸法が指定された形状の配列を取り、最大表面積を持つ形状を見つける maxSurfaceArea というメソッドを作成しようとしています。だから私がしようとしていたのは、形状を含む最初のインデックスを currentSurfaceArea と maxSurfaceeArea に割り当てることでした。次に、メソッドは配列の次のインデックスに移動し、表面積をチェックして、前の maxSurfaceArea よりも大きい場合はその新しい形状を maxSurfaceArea に割り当て、配列の最後まですべてを繰り返す必要があります。このプログラムでは、メイン メソッド クラスでこのメソッドを呼び出していますが、その外側には何も含まれていない Shape というクラスがあります。また、すべて Shape から拡張される Circle、Triangle、Rectangle クラス (それぞれ個別のクラス)。また、Sphere クラス (Circle から拡張)、

各クラスには独自の .computeArea メソッドがあり、メイン クラスによって呼び出される toString メソッドによって出力されます。

私が混乱していることの 1 つは、maxSurfaceArea を「シェイプ」に変換できないブール値にするかどうかを知っていることです。それは、それが取り込んでいる配列のタイプであるためです。どんな助けでも大歓迎です。

public static Shape[] shapeList;

public static void main (String[] args)
{  
  shapeList = new Shape[8];
  shapeList[0] = new Sphere(7.4);
  shapeList[1] = new Prism(5,4.5,4);
  shapeList[2] = new Tetrahedron(4,5);
  shapeList[3] = new Cylinder(3.14, 4.5);
  shapeList[4] = new Prism(1, 2, 3);
  shapeList[5] = new Tetrahedron(2.34, 3.56);
  shapeList[6] = new Sphere(2.5);
  shapeList[7] = new Cylinder(6.7, 3.3);
  for (int i = 0; i < shapeList.length; i++)
  {
    System.out.println(shapeList[i].toString());
  }
}

public static void maxSurfaceArea(Shape[] shapeList)
{
  boolean maxSurfaceArea, currentSurfaceArea;
  for (int i = 0; i < shapeList.length; i++)
  {
    currentSurfaceArea = (shapeList[i]); 
                maxSurfaceArea = (shapeList[i])
    if (shapeList[i].computeArea() > )
    {

    }
  }
4

2 に答える 2

3

形状を返すことで、メソッドをもう少し便利にします。次に、呼び出し元はそれを使って何でもできます。あなたの場合は、その領域を印刷するだけです。

/**
 * Return the Shape that has the largest surface area.  If two shapes
 * have the same area, the first in the list is returned.
 * If empty array is passed, null is returned.
 */
public static Shape maxSurfaceArea(Shape[] shapeList)
{
  Shape max;
  for (Shape shape : shapeList) {
    if (max == null) {
      // first time through
      max = shape;
    } else if (shape.computeArea() > max.computeArea()) {
      max = shape;
      /* If really worried about performance you could store the result of
       * max.computeArea() in a local var and use that in the next loop.
       */
    }
  }
  return max;
}
于 2013-09-10T00:26:53.207 に答える
1

maxSurfaceArea と currentSurfaceArea を boolean として持っているのはなぜですか? maxSurfaceArea メソッドは正確には何をするのですか? 最大形状を出力しますか?

これを行う:

public static void maxSurfaceArea(Shape[] shapeList)
{
    // Here we will store the max area 
    double max = 0;

    // Here we will store the index of max Shape from the array
    int index;

    // Now we loop the array and check each of our Shapes
    foreach(int i = 0; i < shapeList.length; i++)
    {

        // Now we check if current Shape size is bigger
        // Than current max
        if(shapeList[i].computeArea() > max)
        {
            // If it's greater than we store it
            max = shapeList[i].computeArea();
            index = i;
        }
    }

    // Now you can print out the largest Shape
    System.out.println(shapeList[index].toString());
}

配列内のどのシェイプがshapesList最大の表面積を持っているかを判断しようとしているため、ブール値は真または偽の値しかとれないため、実際にはブール値を使用できません。

また、配列内の各値が Shape 型であり、どちらにも割り当てることができない、maxSurfaceAreaまたはcurrentSurfaceAreaどちらも Shape 型ではないため、Shape の配列があります。

2番目の問題は、配列をループして変数を比較するときに、メソッドから返された整数/倍精度値を と比較する方法が実際にないことcomputeArea()ですmaxSurfaceArea

したがって、ここには 3 つの大きな問題があります:
1) 変数の型が正しくない
2) 値の代入が不可能
3) 適切な比較変数がない

于 2013-09-10T00:22:03.747 に答える