Reputation: 7
FYI: I am a beginner. Also, I understand that calling methods is a novice concept and there are a few threads like this already. My situation is a little different because I am very restricted by pseudo-code that my program must mirror identically. I am having trouble calling methods from other methods, including calling a method from inside main. Here is the pseudo-code followed by the code that I wrote:
PSEUDO-CODE:
// The user enters an integer and the program calculates that many primes
// It uses 3 methods, including the main. All the methods are in the same class
// and should be declared as ‘public static.’
Project Print the First n Primes
Package printTheFirstNPrimesPackage
Class PrintTheFirstNPrimes
Method Main
Declare numberOfPrimes as integer
Print “How many prime numbers do you want?"
Read numberOfPrimes from the keyboard
Call the method: PrintNPrimes(numberOfPrimes)
end Method (Main)
// ***********************************************************
// This method accepts an integer and prints that many prime
// numbers, starting at 2. 2 is the lowest primt number.
// ***********************************************************
Method void PrintNPrimes(int n)
declare i as integer
declare myNum as integer
myNum = 2 // The first prime number
i = 0
loop while i < n // This could be a ‘for’ loop
if IsPrime(myNum) // Call the Isprime method, (see below)
i = i + 1
print myNum
End If
myNum = myNum + 1
end loop
end Method PrintNPrimes
// **********************************************************
// This method accepts an integer and tests to see if it is
// a prime number. If it is prime, the method returns true,
// otherwise it returns false.
// **********************************************************
Method boolean IsPrime(int number)
Declare result as boolean
result = true
declare i as integer
i = 2
loop while i < number
if ((number % i) == 0)
result = false
exit loop
end if
end loop
return result
end Method
end Class
End Package
End Project
JAVA CODE:
package printTheFirstNPrimesPackage;
import java.util.*;
public class PrintTheFirstNPrimes {
public static void main(String [] args) {
int numberOfPrimes;
Scanner primes = new Scanner(System.in);
System.out.println("How many prime numbers do you want?");
numberOfPrimes = primes.nextInt();
// Call the method PrintNPrimes(numberOfPrimes)
}
public static void PrintNPrimes(int n) {
int i;
int myNum;
myNum = 2; // The first prime number
i = 0; {
while (i < n)
// if IsPrime(myNum) // Call the IsPrime method (see below) {
i = i + 1;
System.out.println(myNum);
myNum = myNum + 1;
}
}
public static boolean IsPrime(int number) {
boolean result;
result = true;
int i = 2;
while (i < number) {
if ((number % 1) == 0)
result = false;
}
return result;
}
}
My main issue is calling the IsPrime method within the if statement. I get an error saying the IsPrime cannot be converted from int to boolean which I knew, but the pseudo-code restricts me from doing much else. I also would like advice on how I should call the PrintNPrimes method within method main. Thanks.
Upvotes: 1
Views: 2682
Reputation: 34367
Update code below with resolution for both (including if statement) of your compilation errors:
printNPrimes(numberOfPrimes);
if (isPrime(myNum)) // Call the IsPrime method (see below) {
Full updated code:
public static void main(String [] args) {
int numberOfPrimes;
Scanner primes = new Scanner(System.in);
System.out.println("How many prime numbers do you want?");
numberOfPrimes = primes.nextInt();
printNPrimes(numberOfPrimes);
}
public static void printNPrimes(int n) {
int i;
int myNum;
myNum = 2; // The first prime number
i = 0; {
while (i < n)
if (isPrime(myNum)) // Call the IsPrime method (see below) {
i = i + 1;
System.out.println(myNum);
myNum = myNum + 1;
}
}
public static boolean isPrime(int number) {
boolean result;
result = true;
int i = 2;
while (i < number) {
if ((number % 1) == 0)
result = false;
}
return result;
}
I didn't check the logic.
Upvotes: 1
Reputation: 83527
if IsPrime(myNum)
needs to be
if (IsPrime(myNum))
Also be sure to restore your curly braces. I don't see any reason why this will cause an error. Please post the exact error message if you still have problems.
Upvotes: 1
Reputation: 66637
Because your PrintNPrimes
is static method, you can just call the method by passing the numberofPrimes
.
Example:
public static void main(String [] args) {
int numberOfPrimes;
Scanner primes = new Scanner(System.in);
System.out.println("How many prime numbers do you want?");
numberOfPrimes = primes.nextInt();
PrintNPrimes(numberOfPrimes);
}
..........
Note: Java naming convention suggests that use first letter as small case letter while defining methods.
You can follow same approach to invoke other methods.
Upvotes: 1