How come it gives this error when I run a program?

Thread Solved

Join Date: Dec 2008
Posts: 65
Reputation: StarZ is an unknown quantity at this point 
Solved Threads: 1
StarZ StarZ is offline Offline
Junior Poster in Training

How come it gives this error when I run a program?

 
0
  #1
Oct 7th, 2009
I copied this code from the textbook, so the code should have no mistake? I typed the contructor file(I think thats what its called) and the actual codes. So the code looks like this:

  1. public class TestCircle {
  2. public static void main(String[] args) {
  3. Circle spot = new Circle();
  4.  
  5. spot.setRadius(5);
  6.  
  7. System.out.println("Circle radius:" + spot.getRadius());
  8. System.out.println("Circle area: " + spot.area());
  9.  
  10. }
  11. }
And I saved it as TestCircle.java

the constuctor file:
  1. /**
  2.  * Circle class.
  3.  */
  4. public class Circle {
  5. private static final double PI = 3.14;
  6. private double radius;
  7.  
  8. /**
  9.   * constructor
  10.   * pre: none
  11.   * post: A circle object created. Radius initialized to 1.
  12.   */
  13. public Circle() {
  14. radius = 1; //default radius
  15. }
  16.  
  17.  
  18. /**
  19.   * Changes the radius of the circle.
  20.   * pre: none
  21.   * post: radius has been changed.
  22.   */
  23. public void setRadius(double newRadius) {
  24. radius = newRadius;
  25. }
  26.  
  27.  
  28. /**
  29.   * Calculates the area of the circle.
  30.   * pre: none
  31.   * post: The area of the circle has been returned.
  32.   */
  33. public double area() {
  34. double circleArea;
  35.  
  36. circleArea = PI * radius * radius;
  37. return(circleArea);
  38. }
  39.  
  40.  
  41. /**
  42.   * Returns the radius of the circle.
  43.   * pre: none
  44.   * post: The radius of the circle has been returned
  45.   */
  46. public double getRadius() {
  47. return(radius);
  48. }
  49. }
Saved it as Circle.java

When I compile it, it's fine.. no errors but when I go to see the ouput it gives me this:

> java Circle
Static Error: No static method in Circle with name 'main' accepts arguments (String[])

What does that mean, how do I fix it?
I think this happens to most of the programs I to run.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 26
Reputation: nomemory is an unknown quantity at this point 
Solved Threads: 5
nomemory nomemory is offline Offline
Light Poster
 
2
  #2
Oct 7th, 2009
It's very simple:
... the class Circle doens't have any main method. So it's not an actual "program".

You should use:
  1. javac Circle.java TestCircle.java
  2. java TestCircle

First line will compile both classes.
Second line will run the class containing the main method.

(don't skip the basics)
Last edited by nomemory; Oct 7th, 2009 at 6:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 65
Reputation: StarZ is an unknown quantity at this point 
Solved Threads: 1
StarZ StarZ is offline Offline
Junior Poster in Training
 
1
  #3
Oct 7th, 2009
What do u mean?
Like save it as that name?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso
 
1
  #4
Oct 7th, 2009
nomemory means compile the classes then run TestCircle. You cannot run Circle directly as it has no main method. main is the entry point for you program. As nomemory said, don't skip the basics. Please mark your other thread as solved as well!
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 65
Reputation: StarZ is an unknown quantity at this point 
Solved Threads: 1
StarZ StarZ is offline Offline
Junior Poster in Training
 
0
  #5
Oct 7th, 2009
oh ok, I got now.. got it to output.
So it's like instead of running the constructor file(circle) run the actual code program instead.. k thx. I'll mark the other one solve too but I can't find where it is, how do i find it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC