【问题标题】:writing a method to find the maximum surface area in an array of shapes编写一种方法来查找形状数组中的最大表面积
【发布时间】:2013-09-09 23:56:09
【问题描述】:

我真的是 Java 新手,我知道我需要为这些方法做什么,我只是想把它翻译成代码哈哈我想我只是对哪个对象正在调用方法感到困惑以此类推。

所以我正在尝试编写一个名为 maxSurfaceArea 的方法,该方法接收一组具有特定尺寸的形状,并找到具有最大表面积的形状。 所以我试图做的是将包含形状的第一个索引分配给 currentSurfaceArea 和 maxSurfaceeArea。然后该方法需要转到数组的下一个索引,检查表面积,如果它大于前一个 maxSurfaceArea,则将该新形状分配给 maxSurfaceArea,并重复所有操作,直到数组结束。 在这个程序中,我在主方法类中调用此方法,但在此之外,我有一个名为 Shape 的类,其中没有任何内容。还有一个 Circle、Triangle、Rectangle 类(每个都是一个单独的类),它们都从 Shape 扩展而来。还有一个球类(从圆形延伸),圆柱类(从圆形延伸),棱柱类(从矩形延伸),四面体类(从三角形延伸)。

每个类都有自己的 .computeArea 方法,它们由主类调用的 toString 方法输出。

我感到困惑的一件事是,我知道如果我将 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() > )
    {

    }
  }

【问题讨论】:

  • 你能完成你包含的代码吗?它没有完全完成。不要要求我们为您完成代码:向我们展示您的尝试,告诉我们您希望它做什么,以及您看到它做什么,然后我们可以告诉您哪里出错了。跨度>
  • 你想为maxSurfaceArea()返回什么? Shape?地区? void 返回类型在这里没有意义。

标签: java arrays


【解决方案1】:

让你的方法更有用一点是返回形状。然后调用者可以对它做任何事情,在你的情况下,只需打印该区域。

/**
 * 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;
}

【讨论】:

    【解决方案2】:

    为什么你有 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 数组中的哪一个形状具有最大的表面积,因此您不能真正使用布尔值,因为布尔值只能取真值或假值。

    还有一个 Shapes 数组,数组中的每个值都是 Shape 类型,并且不能分配给 maxSurfaceAreacurrentSurfaceArea,因为它们都不是 Shape 类型。

    您遇到的第二个问题是,当您循环数组并比较变量时,实际上无法比较从方法computeArea() 返回的整数/双精度值与maxSurfaceArea

    所以你这里有 3 个大问题:
    1) 变量类型不正确
    2) 不可能的赋值
    3) 没有合适的比较变量

    【讨论】:

    • 哈哈哈!很抱歉,我不小心发布了一个根本不完整的先前编辑。我真的很抱歉!但是非常感谢你!一个简单的问题,所以每个子类的 .computeArea 都不同,所以当我只输入 shapeList[i].computeArea() 时,它说该方法对于类 Shape 是未定义的。我应该让 Shape 为空。当每个子类的索引不同时,我如何能够为数组的每个索引调用 computeArea?如果这一切都有意义?
    猜你喜欢
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多