• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Perfect Number program  RSS feed

 
albert abbene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am having some trouble getting this code to compile and run within eclipse. The problem is when i click to "Run" the program, it brings me to the "Type to Run" window, but my program is not on this list.
Everything I have written previously has always appeared on this list as a "type to run." I think I am overlooking something basic because I am still VERY new to programming.

Thank you very much in advance. Also, my apologies for the format. I tried putting <code> tags around it, but they did nothing.




(I put in the code tags for you. You just guessed wrong about the tag format. Next time you post, click the "Code" button and see what you get. -- Paul C)


 
Greg Brannon
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse isn't giving you the run option you're used to, because Eclipse doesn't recognize the code as a runnable Java class. Why? Because the signature of your main() method is not correct. Can you fix it?
 
albert abbene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:Eclipse isn't giving you the run option you're used to, because Eclipse doesn't recognize the code as a runnable Java class. Why? Because the signature of your main() method is not correct. Can you fix it?


I went ahead and fixed the header, at which point my variables in the main() method were flagged with something like:

Cannot make a static reference to the non-static method getRange() from the type Pnum

Also, after fixing the main() method signature, eclipse is still not recognizing the code as a runnable Java class. Why? I don't know, maybe I havn't actually fixed it

At least i figured out how the code tags work, makes it so much more readable.


 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's a relief that you figured out code tags.

You likely further confused Eclipse by changing the name of the class from Perfection to Pnum. There are two requirements for a class to be runnable:

1. The file name must be the same as the top-level public class that contains the method main(). For example, if the top-level public class is named Pnum, the name of the source code file must be Pnum.java.

2. The signature of the main() method in the top-level public class of the same name as the file name must be correct. For example, public static void main( String[] args ).

You fixed number 2, but then likely broke number 1. Do you have a red squiggly under the class name, Pnum, that says "The public type Pnum must be defined in its own file."? That just means that the file name and class name don't match.

The error you're getting, "Cannot make a static reference to the non-static method getRange() from the type Pnum," is one of the more difficult concepts for beginners to understand. It is caused by the common beginner technique of having single-class programs with nearly all logic orchestrated by the main() method which must be static by definition. The correction is to make the method getRange() static, as in:

public static void getRange()

You'll find the same problem and solution with evaluatePerfectNumbers(). Then, when those methods are static, they won't be able to reference the non-static variables lowerBound and upperBound, and you'll have to make them static as well.

Soon, hopefully, you will graduate from the "single-class, main() method in charge" construction to an OO approach that will quickly relieve the main() method of the awesome responsibility of orchestrating the program's flow.
 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not entirely sure what the purpose of this program is, but x can't be 0. For example 5 % 0 will yield an undefined answer since you can't divide by 0. Also as a side note, some people can get confused by the line
Another way to write it is:
but that's just an fyi preference thing.
 
albert abbene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I restarted from scratch, and was able to write the program to emulate the behavior I desired. It accepts 2 numbers to be input from a user, then scans the range of integers for Perfect Numbers and prints them.



I ran into all sorts of problems attempting to split this behavior into separate methods, where variables couldn't be resolved to types, or could not be passed between static/public methods. etc. Your comments have been extremely helpful. Thank you.
 
dirk dj jaeckel
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also as a side note, some people can get confused by the line

This is a natural way to write it, i think all languages from the beginning on approched it, never seen somebody getting confused


Another way to write it is:
but that's just an fyi preference thing.

But THIS is really confusing, real hacker style



greetings
 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dirk dj jaeckel wrote:

Another way to write it is:
but that's just an fyi preference thing.

But THIS is really confusing, real hacker style


That's why I mentioned that it's a preference thing. I've known some people who got confused and a couple were mathematician's -- something about sum=sum + 1 is impossible or something. But again, it's just a programming preference that I and a lot of other people I know like to use.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!