Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Circle perimeter program: File is not executable, please select a main class you want to run

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I'm writing a basic program to calculate and output the perimeter of a circle based on a given radius. However, I am receiving the following error on line 13:

Syntax error on token "class", @ expected

Can someone please let me know what this means and how to fix it?

Thanks in advance.

 
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why you would want to wrap your main() method in a class, and I'm sure that that isn't the syntax for a class.  Try removing the inner class statement (and its matching brace).
 
Saloon Keeper
Posts: 11189
244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like 'test_circle' is supposed to be a method. However, you declared another method signature 'main' inside it, and on top of that you used the 'class' keyword where a return type should be.

Your code is quite messy. Fix this issues:

  • Don't use underscores in class and method names. Use UpperCamelCase for class names and lowerCamelCase for method and variable names.
  • Indent your code properly. Most of your method signatures are on the same indentation level as your class header. This is likely the source of the other mistakes you made.
  • Your Lab_Week9_Circle class is pretty useless. It seems it only acts as a namespace for your circle class. Instead, you should be using package names.
  •  
    Saloon Keeper
    Posts: 6772
    64
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Proper indentation is VERY important. If you had done so you would have caught (one) of your mistakes.
    Why are your classes nested so deep? Or nested at all? Seems like "Lab_Week9_Circle" was supposed to be your project name and not the name of your class. The signature of your main() method is incorrect. On line 17 you declare a class but you pass in a parameter as though it was a method. Do you need 17 at all?
     
    Carey Brown
    Saloon Keeper
    Posts: 6772
    64
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Assignment take what is on the right side and copies it to the left side. You will be copying the class member variable "radius" to the local variable "required_radius".

    Following Java naming conventions you should use camel case and not underscores to separate words, so: "requiredRadius".
     
    Charles Ormond
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote:I'm not sure why you would want to wrap your main() method in a class, and I'm sure that that isn't the syntax for a class.  Try removing the inner class statement (and its matching brace).



    Remove the testCircle method you mean? My lab brief says its required, see D:

    Exercise 1– Circle.java

    a. Write a class to represent a circle – you should only require one instance variable for the radius.

    Your class should have a constructor which takes one variable which is the required radius

    b. Add a method to calculate the perimeter of the circle (using the formula perimter = 2πr) (You can
    get the value of pi using `Math.pi’)

    c. Add a method to calculate the area of the circle (using the formula area = πr2
    )
    d. Write a class TestCircle, and create 2 new circle objects with different radiuses. Print out their
    perimeters and areas.

     
    lowercase baba
    Posts: 12792
    51
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Charles Ormond wrote:
    Remove the testCircle method you mean? My lab brief says its required, see D:


    You don't have a testCircle METHOD.  You are defining a new CLASS called testCircle:

     
    Marshal
    Posts: 67464
    257
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also be careful about naming conventions. Make your ClassNames start with CapitalLetters, and avoid underscores. Use mixedCase for names comprising several words.
     
    Charles Ormond
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have now updated the code but am still receiving the following errors:

    Illegal modifier for the local class Circle; only abstract or final is permitted
    Illegal modifier for parameter perimeter; only abstract or final is permitted
    Illegal modifier for parameter area; only final is permitted


     
    Rancher
    Posts: 133
    7
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What you are asked to do is:

    write two classes in separate files, one called Circle and another called TestCircle

    Circle should have some methods as described in the task but no main method.

    TestCircle should have a main method, inside which you are going to test your Circle class.

    Hope that helps.
     
    Charles Ormond
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Adrian Grabowski wrote:What you are asked to do is:

    write two classes in separate files, one called Circle and another called TestCircle

    Circle should have some methods as described in the task but no main method.

    TestCircle should have a main method, inside which you are going to test your Circle class.

    Hope that helps.



    oh ok, thanks. This clarifies things a lot
     
    Charles Ormond
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I have now separated the two classes into two seperate files: Circle and Lab_Week9_TestCircle.

    I am now receiving this error:

    The file is not executable, please select a main class you want to run



    Here is the code from the Circle Class




    And here is the now separate TestCircle:

     
    Marshal
    Posts: 24950
    61
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Charles Ormond wrote:I have now separated the two classes into two seperate files: Circle and Lab_Week9_TestCircle.



    Neither of them have a public static void main(String[] args) method, so neither of them is runnable. You wanted the Lab_Week9_TestCircle class to be runnable, right? So that's the one which needs to be fixed.
     
    Charles Ormond
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Paul Clapham wrote:

    Charles Ormond wrote:I have now separated the two classes into two seperate files: Circle and Lab_Week9_TestCircle.



    Neither of them have a public static void main(String[] args) method, so neither of them is runnable. You wanted the Lab_Week9_TestCircle class to be runnable, right? So that's the one which needs to be fixed.



    Changed to public static void (was missing the static part)

    Working now, thanks
     
    Sheriff
    Posts: 14758
    245
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good job on getting your program to work.

    A suggestion: since you already know about methods, consider using a method to eliminate some of the redundant code.

    So instead of doing this:


    You can do this:

    So even though you have one additional method, you still have the same number of lines of code. More importantly, the code duplication has been eliminated and the program is better organized.
     
    Charles Ormond
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for this suggestion, will do!
     
    Junilu Lacar
    Sheriff
    Posts: 14758
    245
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To see why it's useful to eliminate duplication in your code, consider what you'd have to do if you were asked to display a circle's radius as well (right now you only display the area and the circumference). When you have code duplication, you have to add a System.out.println() statement to each section of duplicated code. When you extract the duplicated code out to its own method, you only have to add one line of code and the rest of the program follows.
     
    what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
    Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
    https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!