5
\$\begingroup\$

This is my first time putting up a completed program onto the web and I was hoping to get some feedback on these areas:

  • Commenting (Too much, not enough or do I need to simplify anything)
  • Structure (I guess does it seem reasonably tidy, presentable and laid out well)
  • Naming (Are my variables well named, are sensible?)

These are the main areas that trouble me but any feedback is welcome.

I believe all those "Println" at the start are too much already and I could probably reduce the amount while keeping the same layout.

public class ShapeCalculator {

  public static void main(String[] args) {

    System.out.println("Please Enter a number beetween 1 and 9 to make a decision or 10 to Quit\n");
    System.out.println("For regular and irreglar shapes choose between 1 and 6 as shown below");
    System.out.println("1 = Equalatrial Triangle");
    System.out.println("2 = Square");
    System.out.println("3 = Pentagon");
    System.out.println("4 = Hexagon\n");
    System.out.println("5 = Rectangle");
    System.out.println("6 = Circle\n");
    System.out.println("For Solid shapes choose between 7 and 9 as shown below");
    System.out.println("7 = Cube");
    System.out.println("8 = Cylinder");
    System.out.println("9 = Sphere\n");
    System.out.println("Select 10 to Quit\n");

    Scanner scan = new Scanner(System.in); //looks for and takes user input

    loop: while (true) { //loop positioned here will allow flexibility of user decisions
      int decision = scan.nextInt(); //user input will be in the form of integers
      switch (decision) { //selection based user input

        case 1:
          // Decision 1 Equalatrial Triangle
          System.out.println("You selected Equalatrial Triangle"); //confirm user choice 
          System.out.println("Enter your number to represent the 3 sides"); //Prompt user for input
          double triSide = scan.nextDouble(); // get user input

          double triPerm = triSide * 3; //input Times 3 will get the perimeter

          //Below I am utilising Herons Formula to obtain my Area
          double triCalc = 1.0 / 2.0 * triPerm;
          double triArea = Math.sqrt(triCalc * (triCalc - triSide) * (triCalc - triSide) * (triCalc - triSide));

          //print out Perimeter and Area
          System.out.println("Your perimeter is : " + triPerm + "cm and your Area is : " + triArea + "cm Squared");

          break;
        case 2:
          // Decision 2 Square
          System.out.println("You selected Square");
          System.out.println("Enter your number to represent 1 side");
          double squareSide = scan.nextDouble();

          double squarePerm = squareSide * 4; //get perimeter answer
          double squareArea = squareSide * 2; //get Area answer

          System.out.println("Your perimeter is : " + squarePerm + " and your Area is : " + squareArea + "cm Squared");

          break;
        case 3:
          // Decision 3 Pentagon
          System.out.println("You selected Pentagon");
          System.out.println("Enter your number to represent the 5 sides"); //prompt user for input
          double penSide = scan.nextDouble();

          double penArea;
          penArea = (1.0 / 4.0) * Math.sqrt(5 * (5 + 2 * Math.sqrt(5))) * Math.pow(penSide, 2); //get Area answer

          double penPerm = 5 * penSide; //get perimeter answer

          System.out.println("Your perimeter is : " + penPerm + "cm and your Area is : " + penArea + "cm Squared");

          break;
        case 4:
          // Decision 4 Hexagon
          System.out.println("You selected Hexagon");
          System.out.println("Enter your number to represent the 6 sides");
          double hexSide = scan.nextDouble();

          double hexArea;
          hexArea = (1.0 / 3.0) * Math.sqrt(6 * (5 + 2 * Math.sqrt(6))) * Math.pow(hexSide, 2); //get Area answer

          double hexPerm = 6 * hexSide; //get perimeter answer

          System.out.println("Your perimeter is : " + hexPerm + "cm and your Area is : " + hexArea + "cm Squared");

          break;
        case 5:
          // Decision 5 Rectangle
          System.out.println("You selected Rectangle");
          System.out.println("Enter your First side Length number"); //prompt user for 1st side length
          double recSide1 = scan.nextDouble();
          double recPerm1 = recSide1 * 2; //get the first 2 side numbers

          System.out.println("Enter your Second side Length number"); //prompt user for 2nd side length
          double recSide2 = scan.nextDouble();
          double recPerm2 = recSide2 * 2; //get the second 2 side numbers

          double recArea = recSide1 * recSide2; //get Area answer
          double recPermAnswer = recPerm1 + recPerm2; //get Perimeter answer

          System.out.println("Your perimeter is : " + recPermAnswer + "cm and your Area is : " + recArea + "cm squared");

          break;
        case 6:
          // Decision 6 Circle
          System.out.println("You selected Circle");
          System.out.println("Enter the radius for your circle");
          double radius = scan.nextDouble(); //prompt user for input

          //calculation of circumference/perimeter below and get the answer
          double circPerm = radius * Math.PI; //Part 1
          double circumference = circPerm * 2; //part 2 and get answer

          double circleArea = Math.PI * (radius * radius); //get Area of the circle

          System.out.println("Your perimeter (circumference in this case) is : " + circumference + " and your Area is : " + circleArea);

          break;
        case 7:
          // Decision 7 Cube
          System.out.println("You selected Cube");
          System.out.println("Enter your number to represent 1 side/edge of your Cube");
          double cubeSide = scan.nextDouble(); //prompt user for input

          double cubeVol = cubeSide * cubeSide * cubeSide; //Get volume answer
          double cubeSurf = 6 * (cubeSide * cubeSide); //Get Surface Area answer

          //Print out Volume and Surface Area
          System.out.println("Your Volume is : " + cubeVol + " and your Surface Area is : " + cubeSurf);

          break;
        case 8:
          // Decision 8 Cylinder
          System.out.println("You selected Cylinder");
          System.out.println("Enter your Cylinder Radius");
          double cylinR = scan.nextDouble(); //User inputs Cylinder Radius

          System.out.println("Now Enter your Cylinder Height");
          double cylinH = scan.nextDouble(); //User inputs Cylinder Height

          double cylinVol = Math.PI * (cylinR * cylinR) * cylinH; //Get Volume

          double cylinA = 2 * Math.PI * (cylinR * cylinR); //Part 1 of Cylinder Area calculation
          double cylinB = 2 * Math.PI * cylinR * cylinH; //Part 2 of Cylinder Area calculation
          double cylinSurf = cylinA + cylinB; //add both parts together to get Area answer

          System.out.println("Your Volume is : " + cylinVol + " and your Surface Area is : " + cylinSurf);
          break;
        case 9:
          // Decision 9 Sphere
          System.out.println("You selected Sphere");
          System.out.println("Enter your Radius");
          double sphereR = scan.nextDouble();

          double sphereVol = (4.0 / 3.0) * Math.PI * (sphereR * sphereR * sphereR); //get volume

          double sphereSurf = 4 * Math.PI * (sphereR * sphereR); //get Area

          System.out.println("your volume is : " + sphereVol + " and your Surface Area is : " + sphereSurf);

          break;
        case 10:
          // Quit
          System.out.println("You decided to Quit");
          //Exit program
          System.exit(0);

          break loop;
        default:
          // Wrong decision
          System.out.println("Select a number between 1 and 9 to make a decision or 10 to Quit");
      }
    }
  }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to CR! Are you on Java 8? I must say, this is the first time I see the use of a label: in a review... \$\endgroup\$ – h.j.k. Jul 9 '15 at 1:06
3
\$\begingroup\$
  • Commenting (Too much, not enough or do I need to simplify anything)

    (shamelessly copying my opinion about comments from another answer with some slight modifications...)

    @rolfl posted an excellent answer to another Java question about two months ago regarding comments and Javadoc, and I feel you should definitely have a read at that. Essentially, comments should explain the why, not the how, and if you can avoid using them through the use of properly-named variables or easy-to-understand code constructs, that is generally the more favored approach outside of academic assignments.

    Looking at your comments in particular, most of them simply describe the how... // Decision 9 Sphere, // Quit, // Exit Program, // Wrong decision doesn't value-add your code because it is already obvious what those lines are doing. It's better to remove redundant comments so that they don't end up being potentially misleading in the future.

  • Structure (I guess does it seem reasonably tidy, presentable and laid out well)

    I can at least give a point for that. :) Even though it's one huge method, it is altogether tidy and laid out well. As mentioned in the earlier two answers, you can somewhat improve your code by breaking down into methods, or even an object-oriented (OO) approach to perform the same tasks.

  • Naming (Are my variables well named, are sensible?)

    You get another point here too. However, I will always prefer to spell out names in full: sphereR, sphereVol, sphereSurf can be rewritten as sphereRadius, sphereVolume, and sphereSurfaceArea. Shaving milliseconds from the extra processing required to automagically unravel short-formed names goes a long way in making your code readable and understandable. :)


Further improvements

Similar questions to yours have surfaced in the past before, where a menu is presented to a user in the command-line interface (CLI), an option is read, and then some specific operations are performed on additional inputs.

One cleaner solution for generating the menu part and to separate different handling per option is to itemize said options into an enum type containing the logic per enum value. The benefit of this is that you eliminate the need to type a bunch of System.out.println("1..."); statements, which will otherwise be done with a loop construct.

Then, to handle the varying inputs you require (some shapes require a single dimension input, others two), you can consider mapping each user prompt into a double input for the enum values. For example, if you need to prompt the user twice, a method can then return a two-element double[] array for you to perform your calculations.

Roughly putting both together...

public class MyClass {

    enum Shape {
        SQUARE("Enter side"),
        RECTANGLE("Enter long side", "Enter short side");

        final String[] prompts;

        private Shape(String... prompts) {
            this.prompts = prompts;
        }

        private void doCalculations(double[] inputs) {
            // this can either be a switch statement on the enum values,
            // or an abstract method that will be implemented individually
        }
    }

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            printMenu();
            Shape shape = Shape.values()[getIntegerInput(scanner)];
            shape.doCalculations(getInputs(scanner, shape.prompts));
        }
    }

    private static void printMenu() {
        // there's also a Stream-based Java 8 way too
        for (Shape shape : Shape.values()) {
            System.out.printf("%d) %s\n", shape.ordinal(), shape);
        }
    }

    private static double[] getInputs(Scanner scanner, String... prompts) {
        // there's also a Stream-based Java 8 way too
        double[] result = new double[prompts.length];
        for (int i = 0; i < prompts.length; i++) {
            System.out.println(prompts[i]);
            result[i] = getDoubleInput(scanner);
        }
        return result;
    }

    private static int getIntegerInput(Scanner scanner) {
        // this will return 1, 2, 3, etc. after validation
    }

    private static double getDoubleInput(Scanner scanner) {
        // this will return a double value after validation
    }
}

The other benefit of the enum type here is that you also have a clearer segregation of your calculation logic and the user input. Notice that Shape.doCalculations() only takes in a double[] array, so it also becomes almost trivial to do unit testing (if you replace the return value accordingly too).

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Your overall code is good, I do agree with Wenqin that a more class-oriented approach to the shapes could prove beneficial to the structure of the program.

Anyways, onto the review.

You are right about the use of your println statements in the beginning. This can be handled in a much easier way if you just make a String to store this and call it from a single println statement. That way if you make changes later, it's easier to maintain and it's definitely easier to reuse.

String prompt = "Please Enter a number beetween 1 and 9 to make a decision or 10 to Quit\n" + 
                    "For regular and irreglar shapes choose between 1 and 6 as shown below" + 
                    "1 = Equalatrial Triangle" +
                    "2 = Square" + 
                    "3 = Pentagon" +
                    "4 = Hexagon\n" + 
                    "5 = Rectangle" + 
                    "6 = Circle\n" +
                    "For Solid shapes choose between 7 and 9 as shown below" + 
                    "7 = Cube" + 
                    "8 = Cylinder" + 
                    "9 = Sphere\n" +
                    "Select 10 to Quit\n";

    System.out.println(prompt);

The biggest problem in your code is that you have literally everything in one method. This is a BIG maintainability concern and makes your code really difficult to reuse and debug if something ends up going wrong.

Break your code up into many small methods, this way if something breaks, or you find a bug, you know exactly where to look for the fix.

As a side note, look at how much easier it is to read! Think about what it would be like to read your code from someone else's perspective; would you think it's cluttered? Confusing? In your switch, this can be broken up into methods based on shape (unless you decide to create a class for each shape). This helps in maintainability also and builds on what I stated above.

switch (decision) { //selection based user input

    case 1:
      // Decision 1 Equalatrial Triangle
      System.out.println("You selected Equalatrial Triangle"); //confirm user choice 
      System.out.println("Enter your number to represent the 3 sides"); //Prompt user for input
      equilateralTriangle(scan);

      break;
    case 2:
      // Decision 2 Square
      System.out.println("You selected Square");
      System.out.println("Enter your number to represent 1 side");
      square(scan);

      break;
    case 3:
      // Decision 3 Pentagon
      System.out.println("You selected Pentagon");
      System.out.print("Enter your number to represent the 5 sides: "); //prompt user for input
      pentagon(scan);

      break;
    case 4:
      // Decision 4 Hexagon
      System.out.println("You selected Hexagon");
      System.out.print("Enter your number to represent the 6 sides: ");
      hexagon(scan);

      break;
    case 5:
      // Decision 5 Rectangle
      System.out.println("You selected Rectangle");
      System.out.print("Enter your First side Length number: "); //prompt user for 1st side length
      rectangle(scan);

      break;
    case 6:
      // Decision 6 Circle
      System.out.println("You selected Circle");
      System.out.print("Enter the radius for your circle: ");
      circle(scan);

      break;
    case 7:
      // Decision 7 Cube
      System.out.println("You selected Cube");
      System.out.print("Enter your number to represent 1 side/edge of your Cube: ");
      cube(scan);

      break;
    case 8:
      // Decision 8 Cylinder
      System.out.println("You selected Cylinder");
      System.out.print("Enter your Cylinder Radius: ");
      cylinder(scan);
      break;
    case 9:
      // Decision 9 Sphere
      System.out.println("You selected Sphere");
      System.out.print("Enter your Radius: ");
      sphere(scan);

      break;
    case 10:
      // Quit
      System.out.println("You decided to Quit");
      //Exit program
      scan.close();
      System.exit(0);

      break loop;
    default:
      // Wrong decision
      System.out.println("Select a number between 1 and 9 to make a decision or 10 to Quit");

Input validation
You do this for your switch statement, but not really anywhere else. While this doesn't make a huge impact on this particular program, it could on another one you write and you need to get into the habit of checking anything that you receive as input from the end-user.

Here is a simple example of what I mean:

private static void sphere(Scanner scan) {
  double sphereR = scan.nextDouble();
  if (sphereR < 0.0) {
      sphereR = 0;
      System.out.println("your volume is :" + sphereR + " and your Surface Area is : " + sphereR);
  }
  else {
      double sphereVol = (4.0 / 3.0) * Math.PI * (sphereR * sphereR * sphereR); //get volume
      double sphereSurf = 4 * Math.PI * (sphereR * sphereR); //get Area

      System.out.println("your volume is : " + sphereVol + " and your Surface Area is : " + sphereSurf);
  }

}


So what would happen if a user input a negative number? This would definitely give you output, but is it valid or even usable?

Overall, your code is alright. But try to avoid uneccessary spaces or excessive blank lines, it results in code that is tedious to scroll through as the program becomes longer.

Try to implement the DRY Principle as often as you can, it will result in elegant code that maximizes re-usability.

Putting my suggestions together and leaving some work for yourself, is the code below:

import java.util.Scanner;

public class ShapeCalculator {

  public static void main(String[] args) {
    String prompt = "Please Enter a number beetween 1 and 9 to make a decision or 10 to Quit\n" + 
                    "For regular and irreglar shapes choose between 1 and 6 as shown below" + 
                    "1 = Equalatrial Triangle" +
                    "2 = Square" + 
                    "3 = Pentagon" +
                    "4 = Hexagon\n" + 
                    "5 = Rectangle" + 
                    "6 = Circle\n" +
                    "For Solid shapes choose between 7 and 9 as shown below" + 
                    "7 = Cube" + 
                    "8 = Cylinder" + 
                    "9 = Sphere\n" +
                    "Select 10 to Quit\n";

    System.out.println(prompt);
    Scanner scan = new Scanner(System.in); //looks for and takes user input

    loop: while (true) { //loop positioned here will allow flexibility of user decisions
      int decision = scan.nextInt(); //user input will be in the form of integers
      switch (decision) { //selection based user input

        case 1:
          // Decision 1 Equalatrial Triangle
          System.out.println("You selected Equalatrial Triangle"); //confirm user choice 
          System.out.println("Enter your number to represent the 3 sides"); //Prompt user for input
          equilateralTriangle(scan);

          break;
        case 2:
          // Decision 2 Square
          System.out.println("You selected Square");
          System.out.println("Enter your number to represent 1 side");
          square(scan);

          break;
        case 3:
          // Decision 3 Pentagon
          System.out.println("You selected Pentagon");
          System.out.print("Enter your number to represent the 5 sides: "); //prompt user for input
          pentagon(scan);

          break;
        case 4:
          // Decision 4 Hexagon
          System.out.println("You selected Hexagon");
          System.out.print("Enter your number to represent the 6 sides: ");
          hexagon(scan);

          break;
        case 5:
          // Decision 5 Rectangle
          System.out.println("You selected Rectangle");
          System.out.print("Enter your First side Length number: "); //prompt user for 1st side length
          rectangle(scan);

          break;
        case 6:
          // Decision 6 Circle
          System.out.println("You selected Circle");
          System.out.print("Enter the radius for your circle: ");
          circle(scan);

          break;
        case 7:
          // Decision 7 Cube
          System.out.println("You selected Cube");
          System.out.print("Enter your number to represent 1 side/edge of your Cube: ");
          cube(scan);

          break;
        case 8:
          // Decision 8 Cylinder
          System.out.println("You selected Cylinder");
          System.out.print("Enter your Cylinder Radius: ");
          cylinder(scan);
          break;
        case 9:
          // Decision 9 Sphere
          System.out.println("You selected Sphere");
          System.out.print("Enter your Radius: ");
          sphere(scan);

          break;
        case 10:
          // Quit
          System.out.println("You decided to Quit");
          //Exit program
          scan.close();
          System.exit(0);

          break loop;
        default:
          // Wrong decision
          System.out.println("Select a number between 1 and 9 to make a decision or 10 to Quit");
      }
    }
  }


private static void sphere(Scanner scan) {
      double sphereR = scan.nextDouble();
      if (sphereR < 0.0) {
          sphereR = 0;
          System.out.println("your volume is :" + sphereR + " and your Surface Area is : " + sphereR);
      }
      else {
          double sphereVol = (4.0 / 3.0) * Math.PI * (sphereR * sphereR * sphereR); //get volume
          double sphereSurf = 4 * Math.PI * (sphereR * sphereR); //get Area

          System.out.println("your volume is : " + sphereVol + " and your Surface Area is : " + sphereSurf);
      }

}


private static void cylinder(Scanner scan) {
      double cylinR = scan.nextDouble(); //User inputs Cylinder Radius

      System.out.println("Now Enter your Cylinder Height");
      double cylinH = scan.nextDouble(); //User inputs Cylinder Height
      double cylinVol = Math.PI * (cylinR * cylinR) * cylinH; //Get Volume
      double cylinA = 2 * Math.PI * (cylinR * cylinR); //Part 1 of Cylinder Area calculation
      double cylinB = 2 * Math.PI * cylinR * cylinH; //Part 2 of Cylinder Area calculation
      double cylinSurf = cylinA + cylinB; //add both parts together to get Area answer

      System.out.println("Your Volume is : " + cylinVol + " and your Surface Area is : " + cylinSurf);
}


private static void cube(Scanner scan) {
      double cubeSide = scan.nextDouble(); //prompt user for input

      double cubeVol = cubeSide * cubeSide * cubeSide; //Get volume answer
      double cubeSurf = 6 * (cubeSide * cubeSide); //Get Surface Area answer

      //Print out Volume and Surface Area
      System.out.println("Your Volume is : " + cubeVol + " and your Surface Area is : " + cubeSurf);
}


private static void circle(Scanner scan) {
      double radius = scan.nextDouble(); //prompt user for input

      //calculation of circumference/perimeter below and get the answer
      double circPerm = radius * Math.PI; //Part 1
      double circumference = circPerm * 2; //part 2 and get answer
      double circleArea = Math.PI * (radius * radius); //get Area of the circle

      System.out.println("Your perimeter (circumference in this case) is : " + circumference + " and your Area is : " + circleArea);
}


private static void rectangle(Scanner scan) {
      double recSide1 = scan.nextDouble();
      double recPerm1 = recSide1 * 2; //get the first 2 side numbers

      System.out.println("Enter your Second side Length number"); //prompt user for 2nd side length
      double recSide2 = scan.nextDouble();
      double recPerm2 = recSide2 * 2; //get the second 2 side numbers
      double recArea = recSide1 * recSide2; //get Area answer
      double recPermAnswer = recPerm1 + recPerm2; //get Perimeter answer

      System.out.println("Your perimeter is : " + recPermAnswer + "cm and your Area is : " + recArea + "cm squared");
}


private static void hexagon(Scanner scan) {
      double hexSide = scan.nextDouble();
      double hexArea;
      hexArea = (1.0 / 3.0) * Math.sqrt(6 * (5 + 2 * Math.sqrt(6))) * Math.pow(hexSide, 2); //get Area answer
      double hexPerm = 6 * hexSide; //get perimeter answer

      System.out.println("Your perimeter is : " + hexPerm + "cm and your Area is : " + hexArea + "cm Squared");
}


private static void pentagon(Scanner scan) {
      double penSide = scan.nextDouble();
      double penArea;
      penArea = (1.0 / 4.0) * Math.sqrt(5 * (5 + 2 * Math.sqrt(5))) * Math.pow(penSide, 2); //get Area answer
      double penPerm = 5 * penSide; //get perimeter answer

      System.out.println("Your perimeter is : " + penPerm + "cm and your Area is : " + penArea + "cm Squared");
}


private static void square(Scanner scan) {
      double squareSide = scan.nextDouble();
      double squarePerm = squareSide * 4; //get perimeter answer
      double squareArea = squareSide * 2; //get Area answer

      System.out.println("Your perimeter is : " + squarePerm + " and your Area is : " + squareArea + "cm Squared");
}


private static void equilateralTriangle(Scanner scan) {
      double triSide = scan.nextDouble(); // get user input
      double triPerm = triSide * 3; //input Times 3 will get the perimeter

      //Below I am utilising Herons Formula to obtain my Area
      double triCalc = 1.0 / 2.0 * triPerm;
      double triArea = Math.sqrt(triCalc * (triCalc - triSide) * (triCalc - triSide) * (triCalc - triSide));

      //print out Perimeter and Area
      System.out.println("Your perimeter is : " + triPerm + "cm and your Area is : " + triArea + "cm Squared");
}
}
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Your code is good, but can be refactored to separate the shape calculation logic from the interface logic.

I've never coded java before so the code is just to give you a rouch architectural idea of what I am talking about:

public class ShapeCalculate {

  float equilateralTriangle(...inputs here....){

     double triPerm = triSide * 3; 

    double triCalc = 1.0 / 2.0 * triPerm;
    double triArea = Math.sqrt(triCalc * (triCalc - triSide) * (triCalc - triSide) * (triCalc - triSide));

    return triCalc
  }

  float square(...inputs here....){
    double squarePerm = squareSide * 4; //get perimeter answer
    double squareArea = squareSide * 2; //get Area answer

    return squareArea
  }

  float pentagon(...inputs here....){
    double penArea;
    penArea = (1.0 / 4.0) * Math.sqrt(5 * (5 + 2 * Math.sqrt(5))) * Math.pow(penSide, 2); //get Area answer

    double penPerm = 5 * penSide; //get perimeter answer

    return penPerm

  }

  float hexagon(...inputs here....){
    double penArea;
    penArea = (1.0 / 4.0) * Math.sqrt(5 * (5 + 2 * Math.sqrt(5))) * Math.pow(penSide, 2); //get Area answer

    double penPerm = 5 * penSide; //get perimeter answer

    return penPerm

  }

  float rectangle(...inputs here....){
    double recSide1 = scan.nextDouble();
    double recPerm1 = recSide1 * 2; //get the first 2 side numbers

    double recSide2 = scan.nextDouble();
    double recPerm2 = recSide2 * 2; //get the second 2 side numbers

    double recArea = recSide1 * recSide2; //get Area answer
    double recPermAnswer = recPerm1 + recPerm2; //get Perimeter answer

    return recPermAnswer 
  }

  float circle(...inputs here....){
    double circPerm = radius * Math.PI; //Part 1
    double circumference = circPerm * 2; //part 2 and get answer

    double circleArea = Math.PI * (radius * radius); //get Area of the circle
    return circleArea
  }

  float cube(...inputs here....){
    double cubeSide = scan.nextDouble(); //prompt user for input

    double cubeVol = cubeSide * cubeSide * cubeSide; //Get volume answer
    double cubeSurf = 6 * (cubeSide * cubeSide); //Get Surface Area answer
    return cubeSurf
  }

  float cylinder(...inputs here....){
    double cylinH = scan.nextDouble(); //User inputs Cylinder Height

    double cylinVol = Math.PI * (cylinR * cylinR) * cylinH; //Get Volume

    double cylinA = 2 * Math.PI * (cylinR * cylinR); //Part 1 of Cylinder Area calculation
    double cylinB = 2 * Math.PI * cylinR * cylinH; //Part 2 of Cylinder Area calculation
    double cylinSurf = cylinA + cylinB; //add both parts together to get Area answer

    return cylinSurf
  }

  float sphere(...inputs here....){
    double sphereR = scan.nextDouble();

    double sphereVol = (4.0 / 3.0) * Math.PI * (sphereR * sphereR * sphereR); //get volume

    double sphereSurf = 4 * Math.PI * (sphereR * sphereR); //get Area

    return sphereSurf
  }
}

public class CalculatorEngine{
  void initialize(){
    System.out.println("Type in the name of the shape you wish to calcualte\n");
    System.out.println("For regular and irreglar shapes choose between 1 and 6 as shown below");
    System.out.println("1 = Equalatrial Triangle");
    System.out.println("2 = Square");
    System.out.println("3 = Pentagon");
    System.out.println("4 = Hexagon\n");
    System.out.println("5 = Rectangle");
    System.out.println("6 = Circle\n");
    System.out.println("For Solid shapes choose between 7 and 9 as shown below");
    System.out.println("7 = Cube");
    System.out.println("8 = Cylinder");
    System.out.println("9 = Sphere\n");
    System.out.println("Select 10 to Quit\n");

    Scanner scan = new Scanner(System.in); //looks for and takes user input
    this.scan = scan
    this.shapeCalculate = new ShapeCalculate
  }
  void begin(){

    loop: while(true){
      decision = this.scan.nextInt()
      switch(decision){
        case "Equilateral Triangle":
          System.out.println("You selected Equalatrial Triangle"); //confirm user choice 
          System.out.println("Enter your number to represent the 3 sides"); //Prompt user for input
          double triSide = this.scan.nextDouble(); // get user input
          this.shapeCalcualte.equilateralTriangle(triSide)
        case "Square":
          System.out.println("You selected Square");
          System.out.println("Enter your number to represent 1 side");
          double squareSides = this.scan.nextDouble();
          this.shapeCalcualte.square()
          .......etc.......
      }
    }
  }


}

You could even refactor this further by creating a square class, a triangle class, a pentagon class which are all subclasses of a shape, and then call a permitter method on these shapes but that seems too over engineered for a perimeter calculator program.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.