简体   繁体   中英

How do I design my geometric figures class hierarchy?

Main purpose of this task is to calculate volumes and surface areas of three dimensional geometric shapes like, cylinders, cones.

In Java Language

Design your classes as below introducing:

  • an Interface named “GeometricShapes”
  • an abstract class named “ThreeDShapes”
  • two child classes of ThreeDShapes: Cylinders and Cones.
  • One test class names “TestShapes”

Get the output for volumes and surface areas of cylinders and cones along with respective values of their appropriate input variables. Try to use toString() method and array. Your classes should be designed with methods that are required for Object-Oriented programming.

So Far I Have:

package Assignment2;  

    public interface GeometricShapes {

      public void render();
      public int[] getPosition();
      public void setPosition(int x, int y);

    }

   package Assignment2;

   public abstract class ThreeDShapes implements GeometricShapes
   {

      public int[] position;
      public int[] size;

      public ThreeDShapes()
      {

      }

      public int[] getPosition()
      {
         return position;
      }

      public void setPosition(int x, int y)
      {

         position[0] = x;
         position[1] = y;

      }

   }

   package Assignment2;

   public class Cylinders extends ThreeDShapes
   {

      public Cylinder()
      {
      }

      public void render()
      {
      }


   }

I don't think this is right and I do not know how to fix it. :( Please help.

Most of the problem solving task is to understanding what is asked from you. Teacher wants to make you show that you know how interfaces, abstract classes and classes are connected, and how do thy contribute to the whole object instance.

There are few things that are assumed from you, but you would just be better of rereading notes provided for you - assuming, that you did not pay attention in the class. My experience shows, that struggling student do need a bit more then just advice to get going - if you need help, find a person who is ace'ing the class and just ask assistance.

But for now, to get you going, this is what part of the assignement could look like

package Assignment2;

import java.util.Arrays;

public class task {
    public static interface GeometricShapes {
        public double getArea();
        public double getVolume();
    }

    public static abstract class ThreeDShapes implements GeometricShapes {
        protected double a, h;
    }

    public static class Cones extends ThreeDShapes {
        @Override public double getArea() {
            return Math.PI * this.a * Math.sqrt(a * a + h * h);
        }

        @Override public double getVolume() {
            return (1d / 3d) * Math.PI * Math.pow(a, 2) * a * h;
        }

        @Override public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("Cone [a=").append(a).append("; ").append("h=")
                    .append(h).append(" ( area=").append(this.getArea())
                    .append("; volume=").append(this.getVolume()).append(" )]");
            return builder.toString();
        }
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new ThreeDShapes[] { 
            new Cones() {{ a = 3; h = 4;}}, 
            new Cones() {{ a = 4; h = 5;}}
        }));
    }
}

Output:

[
  Cone [a=3.0; h=4.0 ( area=39.98594644342529; volume=113.09733552923255 )], 
  Cone [a=4.0; h=5.0 ( area=71.08612701053386; volume=335.1032163829112 )]
]

Hint #1 - the reqs say this: "Get the output for volumes and surface areas of cylinders and cones ..." . So obviously you need API methods to calculate the volume and surface area of a shape, and you need test code to construct sample 3D shapes, and call the methods that calculate the volume.

Hint #2 - the reqs say this: "try to use toString() method" . So obviously, you need to implement toString() on your classes.

Hint #3 - the reqs do not talk about rendering shapes, and you don't need the location of a shape to calculate its volume.

So, lets look at you error messages, they are quite clear saying what is wrong.

task.java:25: task.Cones is not abstract and does not override abstract method getVolumeCylinder() in task.GeometricShapes
      public static class Cones extends ThreeDShapes 
                    ^
task.java:58: task.Cylinder is not abstract and does not override abstract method getVolumeCylinder() in task.GeometricShapes
      public static class Cylinder extends ThreeDShapes
                    ^

So, your interface GeometricShapes defines a method getVolumeCylinder() . (Why?) This means, every (non-abstract) class implementing this interface needs to have this method, too (if not some superclass is already implementing this).

Why did you put these methods in the Interface? What does your GeometricShapes interface represent? A collection of shapes? A single shape (then the name should be in singular, not plural)?

Think about this first, then you'll get the answer about which methods are useful here, and how to structure your subclasses.

task.java:64: cannot find symbol
symbol  : variable cylinderBase
location: class task.Cylinder
            return cylinderBase * cylinderHeight;
                   ^

This error message (and all the following ones) mention variables that are not declared anywhere. Think about where you are using these variables, and of what object they would be properties. Then declare them in this object, and organize giving the right values to them.

task.java:126: cannot find symbol
symbol  : class Cylinders
location: class task
               new Cylinders() 
                   ^

This last message complains because you are trying to create an object of class Cylinders , and there is no such class. (You have a class named Cylinder , though.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM