user2762979
user2762979

Reputation: 119

writing a method to find the maximum surface area in an array of shapes

I'm really new to Java and I have down an idea of what I need to do for these methods, I'm just trying to translate this into code haha I think I'm just confused on which object is calling methods and so on and so forth.

So I am trying to write a method called maxSurfaceArea that takes in an array of shapes that are given specific dimensions and it finds the shape that has the maximum surface area. So what I was trying to do was assign the first index that contains a shape to a currentSurfaceArea and a maxSurfaceeArea. Then the method needs to go to the next index of the array, check the surface area, if it is bigger than the previous maxSurfaceArea, then assign that new shape to maxSurfaceArea, and repeat all the until the end of the array. In this program, I am calling this method in the main method class but outside of that I have a class called Shape that doesn't have anything in it. Also a Circle, Triangle, Rectangle classes (each an individual class) that all extend from Shape. Also a Sphere class (extends from Circle), Cylinder class (extends from Circle), Prism class (extends from Rectangle), Tetrahedron class (extends from Triangle).

Each of the classes have their own .computeArea methods and they are output by a toString method that is called by the main class.

One of the things I am confused about is I know if I make maxSurfaceArea a boolean that it cannot be converted to a "Shape" because that's the type of array it's taking in. I just don't know. Any help would be much appreciated.

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() > )
    {

    }
  }

Upvotes: 2

Views: 2373

Answers (2)

Sterling Duchess
Sterling Duchess

Reputation: 2080

Why do you have maxSurfaceArea and currentSurfaceArea as boolean ? And what exactly is the maxSurfaceArea method soposed to do ? Print out the max Shape ?

Do this:

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());
}

Since you are trying to determine which one of the Shapes in shapesList array has the biggest surface area you can't really use boolean as boolean can only take true or false value.

Also you have an array of Shapes each value in your array is of type Shape and it cannot be assigned to either maxSurfaceArea or currentSurfaceArea as neither of them are of type Shape.

Second problem you have is when you loop the array and compare the variables there is really no way to compare an integer/double value returned from method computeArea() with maxSurfaceArea.

So you have 3 big problems here:
1) Incorrect variable types
2) Impossible value assignment
3) No proper comparison variable

Upvotes: 1

sbk
sbk

Reputation: 3936

Make your method a little more useful be returning the shape. Then the caller can do whatever it wants with it, in your case just print the area.

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

Upvotes: 3

Related Questions