We recently asked out students to create a simple method in Java return if a given number is a prime number or not.
The task included a list of requirements the students had to fulfill. However, after correcting I realized that for many beginners some concepts are not fully clear – even if those concepts have been taught before. These are things an experienced developer sometimes often forgets because one has used these concepts over the years and became second nature of the developers.
Let’s have a look at what we asked for:
- Implement a method which returns if a given number is prime or not.
- The method has to be accessible from callers outside of the current class.
- The method has to be called without the corresponding class to be instantiated.
- Name the given number ‘n’.
These points mainly addressed the understanding of basic language concepts of the programming language taught so far.
There are different approaches, how to test number for being prime. In the given example we use the simple 6k +/- 1 optimization method.
public static boolean isPrime(int n) {
if (n <= 3)
return (n > 1);
if (n % 2 == 0 || n % 3 == 0)
return false;
int i = 5;
while (i * i <= n) {
if (n % i == 0 || n % (i+2) == 0)
return false;
i = i + 6;
}
return true;
}
While we have been very indulgent with the algorithms “invented” we have been very strict about the language concepts and syntax used in the solutions. number four of the previous task list should be, however, clear. The thing is called ‘n’.
I reflected this quite a while, trying to come up with explanations to the various issues I have seen in the solutions.
Methods as Return Values
Return values. We asked for a method that returns if a given number is prime or not. First of all, you need to define the type of the return value. In the given example it was boolean. Simply because you want to return a yes or no or – as we say in programming jargontrue or false.
public static boolean isPrime(int n) {
...
return false;
...
return true;
}
Also you make sure that all execution paths in your method provide a return statement. The value returned is either true or false.
For someone new to programming, the concept of methods, functions and so on is not always intuitive. In this case I usually try to come up with basic math. I show a mathematical function like f(x) = x + 1. As long as you know something about math it should be clear what this function does. You provide a value for x and you will receive a value increased by one. Then I write this down as code.
int f(int x) {
return x + 1;
}
You have written your very first method. It eventually does exactly what the mathematical function did before. Well done. Usually I celebrate this with my students.
It doesn’t matter if we are talking about object oriented or functional programming languages. The idea behind methods and functions is always the same. The syntax differs from language to language but the concept stays the same.
In addition I extend this example to the mathematical formula f(g(x)) = x + 1 with g(x) = x *2. Again I show how both methods would look in code.
int g(int x) {
return x * 2;
}
int f(int x) {
return x +1;
}
Now I write down a few lines to show how this would work.
int x = 5;
int y = f(g(x));
While most compilers would refuse to compile this without some additional syntactical sugar, to the students the concepts should be quite clear by now.
Accessibility – Private Property vs. Public Services
If you are not used to programming yet, accessibility can give you a major headache. Who can access what? Over time you will understand concepts like information hiding, encapsulation or abstraction. All of these concepts will make use of accessibility. Although there are slightly different meanings and keywords in a variety of programming language, the idea is always the same. While there is a tutorial available for Java, this is one thing very hard to understand for those learning programming.
I usually try to find some real-world examples: public phone, a phone within the flat with is protected (to be used by family members only) by walls and doors and my private iPhone.
But also public services to be used by everybody, protected classes to be used only for those members in the course and private property not to be accessed by anybody else than me.
I realized that these concepts are slightly more difficult to non-native English speakers until I talked about the examples above which might include translation to some point.
Static – Just use it
I even don’t know where to start how to explain this to somebody not familiar with object-orientation and higher programming languages. I came from Assembler, C, Pascal, BASIC, LISP to Perl, C++ and finally to Java and .NET (VB, C# some F#), some Ruby, Python and various other languages I just don’t have in mind.
If found
The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.
and
In C# terms, “static” means “relating to the type itself, rather than an instance of the type”. You access a static member using the type name instead of a reference or a value, e.g. Guid.NewGuid().
and hell ya, this explains everything to a novice. So I explain it a very simple but inaccurate way: When using the static keyword on a method, you can just run the code. If using it on a variable, you can just use the variable. To some point you can think of static as “just use it”.
If you are new to programming and come across this article, I hope this helps .
tl;dr
There’s no short version. Just trying explaining public static foo to someone without a clue about programming.