Hi, I've been given an assignment to write a java program that determines if a random given number is Prime OR not.

But we also have to make comments in each lines, explaining the function/purpose of each lines to this java coding.

So far, I've gotten the program corrected! All that's left is the comments. So, can anyone be kind enough as to go through my coding down here, and add/correct more suitable comments for each lines in this coding?

Here's the coding (with all the comments I made so far...):

    import java.util.Scanner; //This line helps the compiler find the class file Scanner. 
    public class PrimeNumber{ //The class name here is PrimeNumber
    public static void main(String args[]){ //This is the main method which is the entry point into this prime  number coding program.
    int n; //This is the declaration of the variable n.
    Scanner Prime = new Scanner(System.in); //This allows users to read values of various types.
    System.out.println(" Please enter a number: "); //This line prints out whatever is written between the 2 double codes in the brackets.
    n = Prime.nextInt(); //A method that returns the next integer from the source.
    if (isPrime(n))
    System.out.println("The number is prime.\n"); //This line prints out whatever is written between the 2 double codes in the brackets.
    else
    System.out.println("The number is not prime.\n"); //This line prints out whatever is written between the 2 double codes in the brackets.
    }
    static boolean isPrime(int n) { //This is the starting of Boolean valued expressions used in this java coding.
    if (n%2==0)return false; //This line checks odds up to the square root.
    for(int i=3;i*i<=n;i+=2) { //This line defines an integer 'i' as the integer other than 1 and the given number. That means, i>2 and i<num.
    if(n%i==0)
    return false;
    }
    return true;
    }
    } 

Need some help here, thanks in advance!

Recommended Answers

All 5 Replies

Clean it up, then I'll comment. FWIW, I have done a LOT of research into prime number algorithms. They way it is written, it is very hard to read in a short period of time, which is all I have to give you...

Here's the cleaner version, I'm hoping you could lend a hand, rubberman:

import java.util.Scanner;
    public class PrimeNumber{
    public static void main(String args[]){
    int n;
    Scanner Prime = new Scanner(System.in);
    System.out.println(" Please enter a number: ");
    n = Prime.nextInt();
    if (isPrime(n))
    System.out.println("The number is prime.\n");
    else
    System.out.println("The number is not prime.\n");
    }
    static boolean isPrime(int n) {
    if (n%2==0)return false;
    for(int i=3;i*i<=n;i+=2) {
    if(n%i==0)
    return false;
    }
    return true;
    }
    } 

Here's how I comment your code. Please note that, according to your code, 2 is not a prime number. You might want to fix that.

import java.util.Scanner; //This line helps the compiler find the class file Scanner. 
public class PrimeNumber{ //Defines a class called PrimeNumber
public static void main(String args[]){ //Declaration of the main method of the program.
int n; //This line declares an integer variable called n.
Scanner Prime = new Scanner(System.in); //Declares a scanner, a class that takes input from the user.
System.out.println(" Please enter a number: "); //Prints the phrase " Please enter a number: " to the screen on its own line.
n = Prime.nextInt(); //Grabs the very next integer the user types, and stores it in the variable n.
if (isPrime(n)) //An if-statement that calls isPrime() to check if n is prime.
System.out.println("The number is prime.\n"); //Prints the phrase "This number is prime." to the screen on its own line, and then skips a line.
else    //Tells the program to execute the next line of code, in case n isn't prime.
System.out.println("The number is not prime.\n"); //Prints the phrase "This number is not prime." to the screen on its own line, and then skips a line.
}
static boolean isPrime(int n) { //Declares the isPrime() function, which returns true if the parameter 'n' is prime, and false otherwise.
if (n%2==0)return false; //Use the modulus operator to determine if n is divisible by two, in which case it is even and therefore not prime. This function should return false in this case.
for(int i=3;i*i<=n;i+=2) { //For every integer from 3 and up (counting by twos), as long as i-squared isn't larger than n, execute the following code:
if(n%i==0)      //If n is divisible by i (n mod i is zero)
return false;   //Return false, telling the program that n is not a prime number.
}
return true;    //Return true, telling the program that n is a prime number.
}
} 
commented: you're right.. +3

Thanks, Tumlee! Appreciated your efforts,man!

Commenting style:
The comments on this code are really over the top. The reason for comments is to explain the code, but you should assume that the reader has a decent working knowledge of ordinary Java. Comments like

public class PrimeNumber{ //Defines a class called PrimeNumber

don't help. Anyone able to read Java code knows what that statement does. Comments like that just obscure the real code and make it much harder to read.

Good commenting explains what the code is trying to achieve, what algorithms it uses, and explains any obscure, uncommon, or non-obvious code. It does not need to explain what int n; means.

Rather than write lots of comments for the detailed code, concentrate on using good names for variables and methods - get those right and the details of your code will be self-documenting

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.