Just fill in the missing code for two methods: isPrime() and isPerfect(). We dis
ID: 3906008 • Letter: J
Question
Just fill in the missing code for two methods: isPrime() and isPerfect(). We discussed them in class last week.
You are given skeletons of method isPrime() and a method isPerfect(). You must write the code for both.
The first method: static boolean isPefrect( int n ) takes a number and returns true if and only if it is prime. A prime number is a number greater than 1 that is divisible only by 1 and itself. You may search wikipedia for a list of prime numbers. Try to be efficient in how you determine if it is prime.
The second method: static boolean isPerfect( int n ) takes a number and returns true if and only if it is perfect. A perfect number is a number equal to the sum of all its factors.
In this case the number 1 is counted in the set of factors but the number n is not counted in. Try to be efficient in how you determine if it is perfect.
This is how the program is to be executed and correct output if you put 1 and 30 on the cmd line
// Project2.java NSTRUCTOR SAMPLE SOLUTION
import java.io.*; // BufferedReader
import java.util.*; // Scanner
public class Project4
{
public static void main (String args[]) throws Exception
{
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED CMD ARGS
if (args.length < 2)
{
System.out.println(" usage: C:\> java Project4 <lo> <hi> "); // i.e. C:> java Project4 1 30
System.exit(0);
}
int lo = Integer.parseInt( args[0] ); // i.e. 10
int hi = Integer.parseInt( args[1] ); // i.e. 1000
// TEST EVERY NUMBER BETWEEN LO AND HI INCLUSIVE FOR BEING PRIME AND BEING PERFECT
for ( int i=lo ; i<=hi ; ++i)
{
System.out.print( i );
if ( isPrime(i) ) System.out.print( " prime ");
if ( isPerfect(i) ) System.out.print( " perfect ");
System.out.println();
}
} // END MAIN
// *************** YOU FILL IN THE METHODS BELOW **********************
// RETURNs true if and only if the number passed in is perfect
static boolean isPerfect( int n )
{
/* Y O U R C O D E H E R E */
//return false; // (just to make it compile) YOU CHANGE AS NEEDED
}
// RETURNs true if and only if the number passed in is prime
static boolean isPrime( int n )
{
/* Y O U R C O D E H E R E */
//return false; // (just to make it compile) YOU CHANGE AS NEEDED
}
} // END PROJECT4 CLASS
31 32 RETURNs true if and only if the number passed in is perfect static boolean 13Perfect ( int n ) Just fill in the methods and don't change anything else in this file. 34 35 36 37 38 39 ? 40 41 42 43 // END PROJECT4 CLASS /*Yo URCO D E HERE //return false; // (just to make it compile) YoU CHANGE AS NEEDED // RETURNs true if and only if the number passed in is prime static b olean ?sPrime ( int n ) /*Yo URCO D E HE RE //return false; // (just to make it compile) YoU CHANGE AS NEEDEDExplanation / Answer
// Project2.java NSTRUCTOR SAMPLE SOLUTION
import java.io.*; // BufferedReader
import java.util.*; // Scanner
public class Project4
{
public static void main (String args[]) throws Exception
{
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED CMD ARGS
if (args.length < 2)
{
System.out.println(" usage: C:\> java Project4 <lo> <hi> "); // i.e. C:> java Project4 1 30
System.exit(0);
}
int lo = Integer.parseInt( args[0] ); // i.e. 10
int hi = Integer.parseInt( args[1] ); // i.e. 1000
// TEST EVERY NUMBER BETWEEN LO AND HI INCLUSIVE FOR BEING PRIME AND BEING PERFECT
for ( int i=lo ; i<=hi ; ++i)
{
System.out.print( i );
if ( isPrime(i) ) System.out.print( " prime ");
if ( isPerfect(i) ) System.out.print( " perfect ");
System.out.println();
}
} // END MAIN
// *************** YOU FILL IN THE METHODS BELOW **********************
// RETURNs true if and only if the number passed in is perfect
static boolean isPerfect( int n )
{
/* Y O U R C O D E H E R E */
//find factors and the sum of factors of number
int factorSum = 0;
for(int i=1;i<n;i++)
{
if (n%i == 0 )// i is a factor of n
factorSum += i; // sum of all factors of n
}
//System.out.print(factorSum);
if(factorSum == n)// sum of factors = n
return true;
else
return false;
//return false; // (just to make it compile) YOU CHANGE AS NEEDED
}
// RETURNs true if and only if the number passed in is prime
static boolean isPrime( int n )
{
/* Y O U R C O D E H E R E */
int flag = 1;
if(n ==1 )
flag =0;
else
for(int i=2;i<n;i++)
{
if(n%i == 0) // divisible by any number less than n
flag = 0; // is not prime
}
if(flag == 1)
return true;
else
return false;
//return false; // (just to make it compile) YOU CHANGE AS NEEDED
}
} // END PROJECT4 CLASS
Output:
1
2 prime
3 prime
4
5 prime
6 perfect
7 prime
8
9
10
11 prime
12
13 prime
14
15
16
17 prime
18
19 prime
20
21
22
23 prime
24
25
26
27
28 perfect
29 prime
30
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.