Area of Circle Program - Exception in thread "main" java.lang.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 5369 Views - Last Post: 31 July 2011 - 06:25 PM Rate Topic: -----

#1 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 03:39 PM

First of all I am a newbie to the Java programming language so please bare with me if my questions are not up to par and also please forgive me if my coding is lacking, for that is why I am here. To learn more and become better experienced with Java programming. I have an assignment this week that asks:

Write a Java application to calculate the circumference and the area of a circle. Your application must have two classes. The Circle class will have two methods: the circumference method and the area method. Each method will take a double input parameter as the value of the radius, perform its calculation, and return a double result.

The CircleTest class will have a main method that uses a JOptionPane to get the value of the radius from the user. The main method will create an object of the Circle class and test its methods. The main method will also use a JOptionPane for output. Use Math.PI and the Math.pow method in your calculations.

Limit your output to three digits after the decimal point by using the String.format method. Add exception handling to your program so that, if the user inputs something other than a string of digits, your program catches the exception and gives the user another chance to input the value of the radius.

Below is my attempt to create the code but am having some compilation errors that I cannot resolve. Any assistance would be greatly appreciated.

package circle;
import javax.swing.JOptionPane;

public class CircleTest extends Circle1 {
	
	public static void main (String[] args)
	{
	String input;
	double radius;
	
	
	// Create new object
	Circle1 myCircle = new Circle1();
	
	// Get Radius
	input = JOptionPane.showInputDialog("Please enter radius.");
	radius = Integer.parseInt(input);
	
	// Limit output to three digits after the decimal point
	String circumference = String.format("%.3f",circumference);
	String area = String.format("%.3f",area);
	
	// Display results
	JOptionPane.showMessageDialog(null, "Circumference = " + circumference + "\nArea = " + area);

	}
public class Circle1{
	// Do calculations for circumference
	public double circumference(double radius){
		double circumference;
		circumference = 2*Math.PI*radius;
		return circumference;
	}
	public double area(double radius){
		// Do calculations for area 
		double area;
		radius = Math.pow(radius,2);
		area = Math.PI*radius;
		return area;
	}
	}

}



ERROR:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at circle.CircleTest.main(CircleTest.java:6)

Is This A Good Question/Topic? 0
  • +

Replies To: Area of Circle Program - Exception in thread "main" java.lang.

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 03:59 PM

First I dont know why you extended Circle1 there.Also I dont think if you mean to make your Circle1 an inner class!! If so then you missed one brace} to close the CircleTest before openning Circle1. Another problem is this code:
String circumference = String.format("%.3f",circumference);
	String area = String.format("%.3f",area); 

I suspect you meant to call methods circumference(double) and area(double) there? Then it should be like:
String circumference = String.format("%.3f",myCircle.circumference(radius));
	String area = String.format("%.3f",myCircle.area(radius));



NT: It is better this line:
 radius = Integer.parseInt(input); 
to be:
 radius = Double.parseDouble(input); 
since radius is double
Was This Post Helpful? 2
  • +
  • -

#3 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 04:22 PM

View Postsmohd, on 31 July 2011 - 03:59 PM, said:

First I dont know why you extended Circle1 there.Also I dont think if you mean to make your Circle1 an inner class!! If so then you missed one brace} to close the CircleTest before openning Circle1. Another problem is this code:
String circumference = String.format("%.3f",circumference);
	String area = String.format("%.3f",area); 

I suspect you meant to call methods circumference(double) and area(double) there? Then it should be like:
String circumference = String.format("%.3f",myCircle.circumference(radius));
	String area = String.format("%.3f",myCircle.area(radius));



NT: It is better this line:
 radius = Integer.parseInt(input); 
to be:
 radius = Double.parseDouble(input); 
since radius is double



Thank you for your assistance smohd, I have made the suggested changes and am still receiving some errors. Below is my revised code.

package circle;
import javax.swing.JOptionPane;

public class CircleTest {
	
	public static void main (String[] args)
	{
	String input;
	double radius;
	
	// Create new object
	Circle1 myCircle = new Circle1();
	
	// Get Radius
	input = JOptionPane.showInputDialog("Please enter radius.");
	radius = Double.parseDouble(input);
	
	// Limit output to three digits after the decimal point
	String circumference = String.format("%.3f",myCircle.circumference(radius));
	String area = String.format("%.3f",myCircle.area(radius));

	// Display results
	JOptionPane.showMessageDialog(null, "Circumference = " + circumference + "\nArea = " + area);

	}
}

public class Circle1{
	// Do calculations for circumference
	public double circumference(double radius){
		double circumference;
		circumference = 2*Math.PI*radius;
		return circumference;
	}
	public double area(double radius){
		// Do calculations for area 
		double area;
		radius = Math.pow(radius,2);
		area = Math.PI*radius;
		return area;
	}
	}

}



ERRORS:
LINE 28 Multiple markers at this line
- The public type Circle1 must be defined in its own file
- The type Circle1 is already defined
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 04:34 PM

Ya since they are in the same file that class should be define without keyword public, like class Circle1{
Was This Post Helpful? 0
  • +
  • -

#5 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 04:49 PM

View Postsmohd, on 31 July 2011 - 04:34 PM, said:

Ya since they are in the same file that class should be define without keyword public, like class Circle1{


Thanks again... OK so no I've added some exception handling and am now receiving an new error. Below is my revised code:
package circle;
import javax.swing.JOptionPane;

public class CircleTest {
	
	private static String temp;
	
	public static void main (String[] args)
	{
	
	boolean keepAsking = true;
	String input;
	double radius;
	
	// Create new object
	Circle1 aCircle= new Circle1();
	
	while(keepAsking){
	
	// Get Radius
	input = JOptionPane.showInputDialog("Please enter radius.");
	radius = Double.parseDouble(input);
	
	try{

		radius =Double.parseDouble(temp);//parse to double

		keepAsking =false;
	
	// Limit output to three digits after the decimal point
	String circumference = String.format("%.3f",aCircle.circumference(radius));
	String area = String.format("%.3f",aCircle.area(radius));

	// Display results
	JOptionPane.showMessageDialog(null, "Circumference = " + circumference + "\nArea = " + area);
	
	}catch(Exception e){

		JOptionPane.showMessageDialog(null, "Please enter a valid number");

		}
	}
}

 class Circle1{
	// Do calculations for circumference
	public double circumference(double radius){
		double circumference;
		circumference = 2*Math.PI*radius;
		return circumference;
	}
	public double area(double radius){
		// Do calculations for area 
		double area;
		radius = Math.pow(radius,2);
		area = Math.PI*radius;
		return area;
	}
}

}



ERROR:
LINE 16: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type CircleTest is accessible. Must qualify the allocation with an enclosing instance of type CircleTest (e.g. x.new A() where x is an instance of CircleTest).

at circle.CircleTest.main(CircleTest.java:16)
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 05:04 PM

I thought you accepted my advice of removing the Circle1 class out of CircleTest!! Then what is the last } doing there
Was This Post Helpful? 0
  • +
  • -

#7 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 05:18 PM

View Postsmohd, on 31 July 2011 - 05:04 PM, said:

I thought you accepted my advice of removing the Circle1 class out of CircleTest!! Then what is the last } doing there


Can you please clarify?? I must have missed it. When I remove the last } I still receive an error. What am I missing?? Below is my code:
package circle;
import javax.swing.JOptionPane;

public class CircleTest {
	
	private static String temp;
	
	public static void main (String[] args)
	{
		
	boolean keepAsking = true;
	String input;
	double radius;
	
	// Create new object
	Circle1 aCircle= new Circle1();
	
	while(keepAsking){
	
	// Get Radius
	input = JOptionPane.showInputDialog("Please enter radius.");
	radius = Double.parseDouble(input);
	
	try{

		radius =Double.parseDouble(temp);//parse to double

		keepAsking =false;
	
	// Limit output to three digits after the decimal point
	String circumference = String.format("%.3f",aCircle.circumference(radius));
	String area = String.format("%.3f",aCircle.area(radius));

	// Display results
	JOptionPane.showMessageDialog(null, "Circumference = " + circumference + "\nArea = " + area);
	
	}catch(Exception e){

		JOptionPane.showMessageDialog(null, "Please enter a valid number");

		}
	}
	
	}
 class Circle1{
	// Do calculations for circumference
	public double circumference(double radius){
		double circumference;
		circumference = 2*Math.PI*radius;
		return circumference;
	}
	public double area(double radius){
		// Do calculations for area 
		double area;
		radius = Math.pow(radius,2);
		area = Math.PI*radius;
		return area;
	}
	
 }




ERRORS:
Line 8:Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at circle.CircleTest.main(CircleTest.java:8)
Line 60: Syntax error, insert "}" to complete ClassBody
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 05:25 PM

What the heck is class Circle1 ???
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 05:26 PM

That } must be before line class Circle1{
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 05:27 PM

Sorry just saw it... my bad
Was This Post Helpful? 0
  • +
  • -

#11 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 05:31 PM

View Postpbl, on 31 July 2011 - 05:25 PM, said:

What the heck is class Circle1 ???



That was based off some advice from "smhod". Would that create the error I'm getting?? If I add "public" back into it makes no difference and I still receive the below errors?? Please remember that I am a newbie and would appreciate your comments less "aggressive". Thanks.

LINE 8: Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at circle.CircleTest.main(CircleTest.java:8)

LINE 62: Syntax error, insert "}" to complete ClassBody
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 05:41 PM

What an horrible mix of class and static methods :)
That is what your class should look like


import javax.swing.JOptionPane;

public class CircleTest {

	double radius;
	
	CircleTest(double radius) {
		this.radius = radius;
	}
	// Do calculations for circumference
	public double circumference(){
		return 2*Math.PI*radius;
	}
	public double area(){
		// Do calculations for area 
		return Math.PI*radius*radius;
	}


	public static void main (String[] args)
	{

		boolean keepAsking = true;

		while(keepAsking){
			keepAsking =false;

			// Get Radius
			String input = JOptionPane.showInputDialog("Please enter radius.");
			try{

				double radius =Double.parseDouble(input);//parse to double
				CircleTest aCircle = new CircleTest(radius);
				// Limit output to three digits after the decimal point
				String circumference = String.format("%.3f",aCircle.circumference());
				String area = String.format("%.3f", aCircle.area());

				// Display results
				JOptionPane.showMessageDialog(null, "Circumference = " + circumference + "\nArea = " + area);

			}catch(Exception e){

				JOptionPane.showMessageDialog(null, "Please enter a valid number");

			}
		}

	}
}


Was This Post Helpful? 0
  • +
  • -

#13 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 06:02 PM

View Postpbl, on 31 July 2011 - 05:41 PM, said:

What an horrible mix of class and static methods :)
That is what your class should look like


import javax.swing.JOptionPane;

public class CircleTest {

	double radius;
	
	CircleTest(double radius) {
		this.radius = radius;
	}
	// Do calculations for circumference
	public double circumference(){
		return 2*Math.PI*radius;
	}
	public double area(){
		// Do calculations for area 
		return Math.PI*radius*radius;
	}


	public static void main (String[] args)
	{

		boolean keepAsking = true;

		while(keepAsking){
			keepAsking =false;

			// Get Radius
			String input = JOptionPane.showInputDialog("Please enter radius.");
			try{

				double radius =Double.parseDouble(input);//parse to double
				CircleTest aCircle = new CircleTest(radius);
				// Limit output to three digits after the decimal point
				String circumference = String.format("%.3f",aCircle.circumference());
				String area = String.format("%.3f", aCircle.area());

				// Display results
				JOptionPane.showMessageDialog(null, "Circumference = " + circumference + "\nArea = " + area);

			}catch(Exception e){

				JOptionPane.showMessageDialog(null, "Please enter a valid number");

			}
		}

	}
}




Thanks PBL...but the assignment requirement asks that the code have two classes "Circle" and "CircleTest". How would I incorporate the "Circle" class into the program??
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 06:10 PM

public class Circle {

	double radius;

	Circle(double radius) {
		this.radius = radius;
	}
	// Do calculations for circumference
	public double circumference(){
		return 2*Math.PI*radius;
	}
	public double area(){
		// Do calculations for area 
		return Math.PI*radius*radius;
	}
}



import javax.swing.JOptionPane;

public class CircleTest {


	public static void main (String[] args)
	{

		boolean keepAsking = true;

		while(keepAsking){
			keepAsking =false;

			// Get Radius
			String input = JOptionPane.showInputDialog("Please enter radius.");
			try{

				double radius =Double.parseDouble(input);//parse to double
				Circle aCircle = new Circle(radius);
				// Limit output to three digits after the decimal point
				String circumference = String.format("%.3f",aCircle.circumference());
				String area = String.format("%.3f", aCircle.area());

				// Display results
				JOptionPane.showMessageDialog(null, "Circumference = " + circumference + "\nArea = " + area);

			}catch(Exception e){

				JOptionPane.showMessageDialog(null, "Please enter a valid number");

			}
		}

	}
}


Was This Post Helpful? 0
  • +
  • -

#15 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Area of Circle Program - Exception in thread "main" java.lang.

Posted 31 July 2011 - 06:22 PM

View Postpbl, on 31 July 2011 - 06:10 PM, said:

public class Circle {

	double radius;

	Circle(double radius) {
		this.radius = radius;
	}
	// Do calculations for circumference
	public double circumference(){
		return 2*Math.PI*radius;
	}
	public double area(){
		// Do calculations for area 
		return Math.PI*radius*radius;
	}
}



import javax.swing.JOptionPane;

public class CircleTest {


	public static void main (String[] args)
	{

		boolean keepAsking = true;

		while(keepAsking){
			keepAsking =false;

			// Get Radius
			String input = JOptionPane.showInputDialog("Please enter radius.");
			try{

				double radius =Double.parseDouble(input);//parse to double
				Circle aCircle = new Circle(radius);
				// Limit output to three digits after the decimal point
				String circumference = String.format("%.3f",aCircle.circumference());
				String area = String.format("%.3f", aCircle.area());

				// Display results
				JOptionPane.showMessageDialog(null, "Circumference = " + circumference + "\nArea = " + area);

			}catch(Exception e){

				JOptionPane.showMessageDialog(null, "Please enter a valid number");

			}
		}

	}
}




Got it...so with Java if you want to create multiple classes from the same package/project they will always be on another .java file, correct?? I started out doing that but got confused along the way.

Thanks for your assistance.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2