-2

The following code causes an exception and I'm not entirely sure why, any ideas?

Exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Circle.CircleApp.main

Code

class CircleApp {

    public static void main(String[] args) {
        double rd = Double.parseDouble(args[0]);
        System.out.println("Circle radius = " + rd);

        Circle circle1 = new Circle(rd);

        double cir = circle1.calCircumference();
        double area = circle1.calArea();

        System.out.println("Circumference = " + cir);
        System.out.println("Area = " + area);
    }
}

class Circle {

    private double r;

    Circle(double rd) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Circle(double r) {

    }

    double calCircumference() {
        return 2 * Math.PI * r;
    }

    public double calArea() {
        return Math.PI * r * r;
    }
}
Neil
  • 14,063
  • 3
  • 30
  • 51
Nightfall
  • 19
  • 6

3 Answers3

0

It seems that you are lacking really super basic understanding of Java. The real answer here is: step back, and do more studying.

But lets give some details:

  • You receive that exception, because you are not passing the expected double number to your program when running it. See here for what that means.
  • In your specific case, you are using NetBeans - see here how to pass arguments in that case.

The point is: your code expects to find a double number, so you can simply pass any kind of number to see what will happend.

What will happen is: another exception, because of

throw new UnsupportedOperationException("Not supported yet.");

But your code is calling exactly that method. So, instead, you need:

public class Circle {
  private double r;
  public Circle(double r) {
    this.r = r;
  }

Then: this here:

public void Circle(double r) {
}

is absolute nonsense. This doesn't even compile.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

If you run your program from command line, use something like that:

java CircleApp <double number>

For example:

java CircleApp 123.456

If you use an ide, you should configure your run settings. There has to be a field named program arguments or something similar.

Guybrush
  • 710
  • 7
  • 12
0

Use this to compile your code:

javac CircleApp.java

java CircleApp 10.0

10.0 can be replaced with any double number of your own

mbhargav294
  • 389
  • 1
  • 3
  • 15