0

I'm trying to write a program that would be able to calculate the perimeter of either a circle, triangle, or rectangle. However the program doesn't run correctly, it just show the menu but I can't input anything.

public class GeometryDriver 
{

public static void main(String[] args) 
{


  double radius;
  double side1 = 0; 
  double side2 = 0;
  double side3 = 0; 
  double length =0;
  double width =0; 
  double perimeter = 0;
  String userChoice;

    Scanner keyboard = new Scanner(System.in);
    int option;


    System.out.println("Welcome to the Geometry Calculator!\n"
            + "In this program we will use a menu to decide what kind of shape we will create.\n"
            + "\n1.Create and Calculate Perimeter of a Circle"
            + "\n2. Create and Calculate Perimeter of a Rectangle"
            + "\n3. Create and Calculate Perimeter of a Triangle");



    String input;
    GeometricShape aShape = null;

    option = keyboard.nextInt();




    switch (option)
    {
        case 1:



            input = JOptionPane. showInputDialog(null, "Enter the radius: ");
            radius = Double.parseDouble(input);
            aShape = new GeometricShape(radius);
            perimeter = aShape.getPerimeter();


            aShape = new GeometricShape(radius);
            break;

        case 2:


            input = JOptionPane.showInputDialog(null, "Enter the length: ");

            length = Double.parseDouble(input);

            perimeter = aShape.getPerimeter();
            input = JOptionPane.showInputDialog(null, "Enter the width: ");
            width = Double.parseDouble(input);

            aShape = new GeometricShape (length, width);


            break;
        case 3:

            perimeter = aShape.getPerimeter();

            input = JOptionPane.showInputDialog(null, "Enter side 1: ");
            input = JOptionPane.showInputDialog(null, "Enter side 2: ");
            input = JOptionPane.showInputDialog(null, "Enter side 3: ");
            side1 = Double.parseDouble(input);
            side2 = Double.parseDouble(input);
            side3 = Double.parseDouble(input);

            aShape = new GeometricShape (side1, side2, side3);

            break;
        default:

                    }


      System.out.println(aShape + " has perimeter" + perimeter);
}

}

and...

public class GeometricShape 
{
    private double side1, side2, side3;
    private double radius; 
    private double length, width; 
    private boolean triangle = false;
    private boolean rectangle = false;
    private boolean circle = false;`


    public GeometricShape(double aSide1, double aSide2, double aSide3)
    {
        side1 = aSide1;
        side2 = aSide2;
        side3 = aSide3;
        triangle = true;
    }

    public GeometricShape(double aLength, double aWidth)
    {
        length = aLength;
        width = aWidth;
        rectangle = true;
    }


    public GeometricShape(double aRadius)
    {
        radius = aRadius;
        circle = true;
    }

    public double getRadius()
    {
        return radius;
    }

    public double getSide1()
    {
        return side1;
    }

    public double getSide2()
    {
        return side2;
    }

    public double getSide3()
    {
        return side3;
    }

    public double getLength()
    {
        return length;
    }

    public double getWidth()
    {
    return width;
    }

    public void setRadius(double aRadius)
    {
        radius = aRadius;
    }

    public void setSide1(double aSide1)
    {
        side1 = aSide1;
    }

    public void setSide2(double aSide2)
    {
        side2 = aSide2;
    }

    public void setSide3(double aSide3)
    {
        side3 = aSide3;
    }

    public void setLength(double aLength)
    {
        length = aLength;
    }

    public void setWidth(double aWidth)
    {
        width = aWidth;
    }

    public double getPerimeter()
    {

        double perimeter = 0;


        if (triangle == true)
        {
            perimeter = side1 + side2 + side3; 
        }

        else if (rectangle == true)
        {
            perimeter = (2* length) + (2*width);
        }

        else if (circle == true)
        {
            perimeter = 2* Math.PI * radius; 
        }  

    }

    public String toString()
    {
        return "The perimeter is: " + this.getPerimeter(); 
    }
}
Omal Perera
  • 2,971
  • 3
  • 21
  • 26
Amy
  • 11
  • 1

2 Answers2

1

New Answer So exciting news! It does work!

Consider this:

        Scanner keyboard = new Scanner(System.in);
        JOptionPane.showInputDialog(null, "Dialog Box 1: ");
        System.out.println("Write something");
        keyboard.nextInt();

It works fine right? However when you type this instead

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write something");
        keyboard.nextInt();
        JOptionPane.showInputDialog(null, "Dialog Box 1: ");

It seems to stop working. I challenge you to minimise the window of the console. Behind it, you will find your dialog box saying "DialogBox 1:". This is obviously not ideal as the user of your application will not be able to use it.

So one workaround you can use is to change the keyboard input to a dialog box as shown below:

String displayMessage = "Welcome to the Geometry Calculator!\n"
            + "In this program we will use a menu to decide what kind of shape we will create.\n"
            + "\n1.Create and Calculate Perimeter of a Circle"
            + "\n2. Create and Calculate Perimeter of a Rectangle"
            + "\n3. Create and Calculate Perimeter of a Triangle";



    String input;
    GeometricShape aShape = null;

    option = Integer.parseInt(JOptionPane.showInputDialog(displayMessage));

Further Information This question has been discussed a couple of times. JOptionPane.showMessageDialog is not showing and JOptionPane and Scanner input issue. The second explains a way of using this:

SwingUtilities.invokeLater(new Runnable() {

To solve the problem.

However, I recommend for simplicity sake you just stick using scanner OR dialog boxes

Stef2i
  • 101
  • 4
-1

With some slight modifications to your code, I can enter a radius. It's now all in one file named Main.java and still a nullpointer for a rectangle because the width is null.

import javax.swing.*;
import java.util.Scanner;

class GeometricShape {
    private double side1, side2, side3;
    private double radius;
    private double length, width;
    private boolean triangle = false;
    private boolean rectangle = false;
    private boolean circle = false;

    GeometricShape(double aSide1, double aSide2, double aSide3) {
        side1 = aSide1;
        side2 = aSide2;
        side3 = aSide3;
        triangle = true;
    }

    GeometricShape(double aLength, double aWidth) {
        length = aLength;
        width = aWidth;
        rectangle = true;
    }


    GeometricShape(double aRadius) {
        radius = aRadius;
        circle = true;
    }

    public double getRadius() {
        return radius;
    }

    public double getSide1() {
        return side1;
    }

    public double getSide2() {
        return side2;
    }

    public double getSide3() {
        return side3;
    }

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    public void setRadius(double aRadius) {
        radius = aRadius;
    }

    public void setSide1(double aSide1) {
        side1 = aSide1;
    }

    public void setSide2(double aSide2) {
        side2 = aSide2;
    }

    public void setSide3(double aSide3) {
        side3 = aSide3;
    }

    public void setLength(double aLength) {
        length = aLength;
    }

    public void setWidth(double aWidth) {
        width = aWidth;
    }

    public double getPerimeter() {

        double perimeter = 0;


        if (triangle == true) {
            perimeter = side1 + side2 + side3;
        } else if (rectangle == true) {
            perimeter = (2 * length) + (2 * width);
        } else if (circle == true) {
            perimeter = 2 * Math.PI * radius;
        }
        return perimeter;

    }


    public String toString() {


        return "The perimeter is: " + this.getPerimeter()
                ;
    }

}


public class Main {

    public static void main(String[] args) {
        {


            double radius;
            double side1 = 0;
            double side2 = 0;
            double side3 = 0;
            double length = 0;
            double width = 0;
            double perimeter = 0;
            String userChoice;

            Scanner keyboard = new Scanner(System.in);
            int option;


            System.out.println("Welcome to the Geometry Calculator!\n"
                    + "In this program we will use a menu to decide what kind of shape we will create.\n"
                    + "\n1.Create and Calculate Perimeter of a Circle"
                    + "\n2. Create and Calculate Perimeter of a Rectangle"
                    + "\n3. Create and Calculate Perimeter of a Triangle");


            String input;
            GeometricShape aShape = null;

            option = keyboard.nextInt();


            switch (option) {
                case 1:


                    input = JOptionPane.showInputDialog(null, "Enter the radius: ");
                    radius = Double.parseDouble(input);
                    aShape = new GeometricShape(radius);
                    perimeter = aShape.getPerimeter();


                    aShape = new GeometricShape(radius);
                    break;

                case 2:


                    input = JOptionPane.showInputDialog(null, "Enter the length: ");

                    length = Double.parseDouble(input);

                    perimeter = aShape.getPerimeter();
                    input = JOptionPane.showInputDialog(null, "Enter the width: ");
                    width = Double.parseDouble(input);

                    aShape = new GeometricShape(length, width);


                    break;
                case 3:

                    perimeter = aShape.getPerimeter();

                    input = JOptionPane.showInputDialog(null, "Enter side 1: ");
                    input = JOptionPane.showInputDialog(null, "Enter side 2: ");
                    input = JOptionPane.showInputDialog(null, "Enter side 3: ");
                    side1 = Double.parseDouble(input);
                    side2 = Double.parseDouble(input);
                    side3 = Double.parseDouble(input);

                    aShape = new GeometricShape(side1, side2, side3);

                    break;
                default:

            }


            System.out.println(aShape + " has perimeter" + perimeter);
            ;

        }
    }
}

Test

enter image description here

Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.

1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
3. Create and Calculate Perimeter of a Triangle
1
The perimeter is: 12.566370614359172 has perimeter12.566370614359172
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • I just realised the issue... The dialog box appears behind the window you are running it in as soon as you interact with the console. I will add a better answer. – Stef2i Jun 04 '17 at 03:53