Question: Execute your solution like this. C:> java Project4 1 30 Just fill in the missing code for two methods: isPrime() and isPerfect(). We discussed them

Execute your solution like this. C:\> java Project4 1 30

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.

 eg. 28 is perfect because 1 + 2 + 4 + 7 + 14 = 28

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

Code:

// 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

{

System.out.println(" usage: C:\\> java Project4 "); // 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

{

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

Command Prompt C: \Users\tim\Desktop project-04 solution>java Project4 1 30 2 prime 3 prime prime 6 perfect 7 prime 9 10 11 prime 12 13 prime 16 17 prime 18 19 prime 20 23 prime 26 27 28 perfect 29 prime 30 C: \Users\tim Desktop\project-04 solution>_

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!