# Perfect Numbers: using a class method

• Goal of this Homework

• Reinforce your knowledge of loops
• Reinforce your knowledge of if-statements
• Reinforce class (static) methods

• Relevant Material: various chapters in book

• Preparation

Save the following files in your project directory (preferably, make a directory called "hw5" in your "cs170" directory, and save these files there (right click and select save target as)

• An empty "Perfect.java" file: click here - You must modify this file for this assignment.

• A test program file named "PerfectTest.java": click here - do NOT modify this program

You can also get the test program using the commands:

```    mkdir ~/cs170/hw5
cp  /home/cheung/teaching/web/170/Projects/hw5/Handouts/*.java   ~/cs170/hw5
cd ~/cs170/hw5
```

The last command will make the directoy "~/cs170/hw5" as the current directory.

In the remainder of the homework description, I will assume that you have saved these file in your ~/cs170/hw5 directory.

• If you use BlueJ

• If you are using BlueJ for the lab, you must open hw5 as "Non BlueJ"

• Homework Assignment

• Write a class (static) method named isPerfect(n) that:

• Uses one integer input parameter n .

• Returns true when the input number n is a "perfect number"
It returns false otherwise (if the input number is not a "perfect number").

I will explain below what a "perfect number" is...

• What you need to do in this assignment:

• Edit the program file Perfect.java in your ~/cs170/hw5 directory and write the isPerfect(n) in this file.

• The isPerfect(n) must be a class (static) method.

• The isPerfect(n) method has one input parameter of the type int (integer)

• The isPerfect(n) method returns a boolean value (i.e., the return type of the isPerfect(n) method is boolean)

• More details...

• A perfect number is a number whose sum of its proper divisors is equal to the number itself.

A proper divisor of a number n is a number between 1 and n-1 that divides n.

• The first 2 perfect numbers are:

• 6

The number 6 is perfect because the proper divisors of 6 are: 1, 2, 3.
Their sum 1+2+3 is equal to 6, i.e., sum of its proper divisors is equal to the number itself

• 28

The number 28 is perfect because the proper divisors of 28 are: 1, 2, 4, 7, 14. Their sum 1+2+4+7+14 is equal to 28, i.e., sum of its proper divisors is equal to the number itself

• Test

• To compile the project, issue this command (the current directory must be ~/cs170/hw5):

javac PerfectTest.java

NOTE: Your program will NOT compile if the method isPerfect() is NOT a class (static) method

• To run the project, issue this command (the current directory must be ~/cs170/hw5):

java PerfectTest

• The correct output is:
```	6
28
496
8128
```

BTW, the next perfect number is 33550336 !

• Turnin

• Turn in the homework with the following command:
```   /home/cs170001/turnin   ~/cs170/hw5/Perfect.java  hw5

```