Question:
What is wrong with my Java program? I can't compile it?
My Clone
2010-11-13 02:35:07 UTC
Can someone help with my program please? The program is for computing the area and perimeter of a circle whose radius is given through user input. The following is my program:

public class Circle
{
private double radius, area, perimeter;
static final double PI = 3.14;

public double getRadius()
{
return radius;
}

public double computeArea(double radius)
{
area = PI * radius * radius;
return area;
}

public double computePerimeter(double radius)
{
perimeter = 2 * PI * radius;
return perimeter;
}

public void printArea()
{
System.out.println("Area: " + area);
System.out.println();
}

public void printPerimeter()
{
System.out.println("Perimeter: " + perimeter);
System.out.println();
}
}

import java.util.Scanner;
public class Question1
{
public static void main(String[] args)
{
double radius, area;
int option = 0;
Scanner sc = new Scanner(System.in);

System.out.println("| 1. Create a new cirlce |");
System.out.println("| 2. Print area |");
System.out.println("| 3. Print circumference |");
System.out.println("| 4. Quit |");
System.out.println("==============================");

do {
System.out.print("Choose option (1-3): ");
option = sc.nextInt();

if (option == 1)
{
System.out.println("Enter the radius to compute the area and circuference: ");
radius = sc.nextDouble;

Circle.computeArea(radius);
Circle.computePerimeter(radius);

System.out.println("A new circle is created.");
}

else if (option == 2)
{
System.out.println("Area of circle");
System.out.println("Radius: " + getRadius);
Circle.printArea();
}

else if (option == 3)
{
System.out.println("Perimeter of circle");
System.out.println("Radius: " + getRadius);
Circle.printPerimeter();
}

else if (option == 4)
{
System.out.println("Invalid option!");
}

while (option != 7);

System.out.println("Thank you!!");
}
}
}

If someone can tell me why it is not compiling, I'd appreciate it a lot. Because it can't compile, I can't figure out if it works either. Please help, thanks!
Five answers:
DX
2010-11-13 04:09:59 UTC
The line :

System.out.println("================

is missing a quote, closing parenthesis, and a semicolon.



When calling methods, you need parenthesis for the parameter list, even if there are no parameters to input:

radius = sc.nextDouble should be nextDouble()

System.out.println("Radius: " + getRadius) should be getRadius()



About the following lines:

Circle.computeArea(radius);

Circle.computePerimeter(radius);

...

Circle.printArea();

....

Circle.printPerimeter();



You can't call your methods like this. You either have to make all your methods static, like

public static computeArea(double radius) {...}

or instantiate a new Circle object within your main method, and then call your circle methods on the instantiated object. You'll need to make a Circle constructor to do this. So:

In the Circle class, add:



public Circle() {}



(preferably after yours field declarations, and before your methods just for style)

Near the top of Question1's main method, write:



Circle c = new Circle();



Then change those old lines to:

c.computeArea(radius);

c.computePerimeter(radius);

...

c.printArea();

....

c.printPerimeter();



If this is for a class, and you're new to Java and programming in general, I'd recommend the second approach over making your methods static. The latter way is probably what your teacher is looking for.



In:

System.out.println("Radius: " + getRadius);

...

System.out.println("Radius: " + getRadius);



it should be c.getRadius()







Edit:

Also, what the person above said about the misplaced do-while bracket.
Jeff
2010-11-13 03:44:17 UTC
You misplaced the closing brace for the do-while loop. Try replacing your last 10 lines of code with this:



else if (option == 4)

{

System.out.println("Invalid option!");

}



} while (option != 7);



System.out.println("Thank you!!");

}

}



Do tell me what happens.



Good luck!



EDIT:

I noticed that you made some function calls without placing the parentheses. Here are those code segments:



radius = sc.nextDouble; //should be sc.nextDouble()



System.out.println("Radius: " + getRadius); //should be getRadius()



Hope this helped.
?
2010-11-13 02:41:40 UTC
System.out.println("| 1. Create a new cirlce |");

System.out.println("| 2. Print area |");

System.out.println("| 3. Print circumference |");

System.out.println("| 4. Quit |");

System.out.println("================…



you Could be missing bracket?
2016-12-16 01:02:47 UTC
i'm assuming you have the JRE put in and open up a command console, form javac and cant discover it. properly if the java direction wasn't added to the direction variable, you wont be waiting to execute it in basic terms like that, replace your dir in the console to the single in the programfiles Java a million.x or perhaps if. detect the javac there and there you bypass.
Jaywalker
2010-11-13 02:41:14 UTC
It's better if you write down the error compiler is giving you. I can see, for example, that you are writing "import java.util.Scanner" in the middle of your file. Try moving it to the top (if all this code is coming from a single .java file). That is, your "import" line should be the first line in your code.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...