-2

Code no longer runs as I can't figure out how to get the scanner to run, but how do I get the methods to execute? Tips and help is appreciated. Thanks in advance.

For clarification, I want this code to run like a geometry calculator, however I do not really know how to call methods (which I think is the issue here)

import java.util.Scanner;
import java.lang.Math;


public class Geometry
{
        private static void printMenu()
    {
                System.out.println("This is a geometry calculator");
                System.out.println("Choose what you would like to calculate");
                System.out.println("1. Find the area of a rectangle");
                System.out.println("2. Find the perimeter of a rectangle");
                System.out.println("3. Find the perimeter of a triangle");
                System.out.println("Enter the number of your choice:");
    }

 public static void main (String [] args)
 {
  int choice;   //the user's choice
  double value = 0; //the value returned from the method
  char letter;  //the Y or N from the user's decision to exit
  double radius;  //the radius of the circle
  double length;  //the length of the rectangle
  double width;  //the width of the rectangle
  double height;  //the height of the triangle
  double base;  //the base of the triangle
  double side1;  //the first side of the triangle
  double side2;  //the second side of the triangle
  double side3;  //the third side of the triangle

  Scanner keyboard = new Scanner (System.in);
 }

    public static double rectangleArea(double length, double width, double value, Scanner keyboard) {
     System.out.print("Enter the length of the rectangle:  ");
     length = keyboard.nextDouble();
     System.out.print("Enter the width of the rectangle:  ");
     width = keyboard.nextDouble();
     value= length*width;
     System.out.println("The area of a rectangle is " + value);
     return value;
    }

    public static double rectanglePerimeter(double length, double width, double value, Scanner keyboard) {
     System.out.print("Enter the length of the rectangle:  ");
     length = keyboard.nextDouble();
     System.out.print("Enter the width of the rectangle:  ");
     width = keyboard.nextDouble();
     value= (2*length)+(2*width);
     System.out.println("The perimeter of the rectangle is " + value);
     return value;
    }

    public static double trianglePerimeter(double side1, double side2, double side3, double value, Scanner keyboard) {
     System.out.print("Enter the length of side 1 of the triangle:  ");
     side1 = keyboard.nextDouble();
     System.out.print("Enter the length of side 2 of the triangle:  ");
     side2 = keyboard.nextDouble();
     System.out.print("Enter the length of side 3 of the triangle:  ");
     side3 = keyboard.nextDouble();
     //call the perimeter method and store the result in the value variable
     value = (side1 + side2 + side3);
     System.out.println("The perimeter of the triangle is " + value);
     return value;

    //default:
     //System.out.println("You did not enter a valid choice.");
    }
}
Gianna Caruso
  • 113
  • 2
  • 3
  • 14

2 Answers2

3

I'm assuming you just want to encapsulate each of your cases in methods. Simply create a new method for each.

Example for case 2:

private static double rectanglePerimeter(Scanner keyboard) {
     System.out.print("Enter the length of the rectangle:  ");
     double length = keyboard.nextDouble();
     System.out.print("Enter the width of the rectangle:  ");
     double width = keyboard.nextDouble();
     double value= (2*length)+(2*width);
     System.out.println("The perimeter of the rectangle is " + value);
     return value;
}

In your case block you would invoke it like:

switch (choice)
   {
    case 1:
     value = rectangleArea(keyboard);
     break;
    case 2:
     value = rectanglePerimeter(keyboard);
     break;
    case 3:
     value = trianglePerimeter(keyboard);
     break;
}

You can then refactor some of the variables out of main, such as length and width

Grice
  • 1,345
  • 11
  • 24
0

Example method:

public static double calculateRectangleArea(Scanner keyboard){
    System.out.print("Enter the length of the rectangle:  ");
    double length = keyboard.nextDouble();
    System.out.print("Enter the width of the rectangle:  ");
    double width = keyboard.nextDouble();
    double value= length*width;
    System.out.println("The area of a rectangle is " + value);
    return value;
}

Example call:

case 1:
    value = calculateRectangleArea(keyboard);
    break;

Make sure to pass the Scanner. You can do these for the other two cases.

So it should look more like this in the end:

import java.util.Scanner;
import java.lang.Math;

public class Geometry
{
    private static void printMenu()
    {
        System.out.println("This is a geometry calculator");
        System.out.println("Choose what you would like to calculate");
        System.out.println("1. Find the area of a rectangle");
        System.out.println("2. Find the perimeter of a rectangle");
        System.out.println("3. Find the perimeter of a triangle");
        System.out.println("Enter the number of your choice:");
    }

    public static double calculateRectangleArea(Scanner keyboard){
        System.out.print("Enter the length of the rectangle:  ");
        double length = keyboard.nextDouble();
        System.out.print("Enter the width of the rectangle:  ");
        double width = keyboard.nextDouble();
        double value = length*width;
        System.out.println("The area of a rectangle is " + value);
        return value;
    }

    public static double calculateRectanglePerimeter(Scanner keyboard){
        System.out.print("Enter the length of the rectangle:  ");
        double length = keyboard.nextDouble();
        System.out.print("Enter the width of the rectangle:  ");
        double width = keyboard.nextDouble();
        double value = (2*length)+(2*width);
        System.out.println("The perimeter of the rectangle is " + value);
        return value;
    }

    public static double calculateTrianglePerimeter(Scanner keyboard){
        System.out.print("Enter the length of side 1 of the triangle:  ");
        double side1 = keyboard.nextDouble();
        System.out.print("Enter the length of side 2 of the triangle:  ");
        double side2 = keyboard.nextDouble();
        System.out.print("Enter the length of side 3 of the triangle:  ");
        double side3 = keyboard.nextDouble();
        //call the perimeter method and store the result in the value variable
        double value = (side1 + side2 + side3);
        System.out.println("The perimeter of the triangle is " + value);
        return value;
    }

    public static void main (String [] args)
    {
        int choice;   //the user's choice
        char letter;  //the Y or N from the user's decision to exit
        double value;
        Scanner keyboard = new Scanner (System.in);

        do
        {
            //call the printMenu method
            printMenu();
            choice = keyboard.nextInt();

            switch (choice)
            {
                case 1:
                    value = calculateRectangleArea(keyboard);
                    break;
                case 2:
                    value = calculateRectanglePerimeter(keyboard);
                    break;
                case 3:
                    value = calculateTrianglePerimeter(keyboard);
                    break;
                default:
                    System.out.println("You did not enter a valid choice.");
            }

            keyboard.nextLine(); //consumes the new line character after the number
            System.out.println("Do you want to exit the program (Y/N)?:  ");
            String answer = keyboard.nextLine();
            letter = answer.charAt(0);
        }while (letter != 'Y' && letter != 'y');
    }
}