I\'m in java one and I need with this complicated conde. please follow the direc
ID: 3676236 • Letter: I
Question
I'm in java one and I need with this complicated conde. please follow the directions and test your program to see if it works. thank you.
A perfect number is a positive number greater than 1 whose factors (including 1 but not the number itself) add up to the number itself.
For instance, 6 is a perfect number because the factors of 6 add up to 6: 1 + 2 + 3 equals 6. Likewise, 28 is a perfect number because1+2+4+7+14equals28. 26isnotaperfectnumberbecause1+2+13 isnotequalto26.
Write a public static method called testPerfect() which takes one integer argument and returns a boolean value of true or false if the argument is or isn't a perfect number. Your method cannot simply use a switch statement or nested if-else structure to determine if it's a perfect number or not. In other words, you may NOT write code like the following:
if ( number == 28 || number == 496 || number == 6 || number = 8128 ) return true;
else
You must calculate the summation of the factors to see if they add up to the number itself. This method will not perform any output.
Write another public static method printFactors() that also takes an integer argument and prints out the factors of the argument in descending order (largest to smallest). By necessity, this method must perform output. Similarly to the testPerfect() method, you may not simply use and if-else statement to output the factors in this method.
If a number is a perfect number, the output should follow the format:
NUMBER : FACTORS
where NUMBER is the user entered, followed immediately by a space then a colon, followed immediately by a space and a single-
space-separated list of the FACTORS. If the number is not perfect, the output should follow the format:
NUMBER : NOT PERFECT
where NUMBER is the user entered, followed immediately by a space then a colon, followed immediately by a space and the text NOT PERFECT.
Your program should ask how many numbers should be tested and then process only that many numbers. You should validate that the user has entered a number greater than 0 before proceeding to processing numbers.
Goals
Use your knowledge of methods to solve this program.
Set up a counter-controlled loop surrounding method calls.
Points to Think About
Both methods will be called from the main() method at different points. Do not call printFactors() from within testPerfect(). You will only have one method call to each of your two methods inside main(). This refers to the method call code itself, not how many times the method may be called (i.e., inside a loop).
Grading Notes
- Your testPerfect() method must actually calculate and add up the factors to determine true or false. You may not simply write selection code to return true if 6, 28, 496 or other perfect numbers are entered and false otherwise.
- Method documentation must be present
- Your testPerfect() method should not do any output. The only output should occur in your main() method and inthe printFactors() method.
- Your testPerfect() method should not calls your printFactors() method; they should be called separately in the
main() method.
- Your printFactors() method should be a void method (i.e., have a return type of void) and will do output.
sample output
How many numbers would you like to test? -1
Invalid value input – Enter a positive value
How many numbers would you like to test? 0
Invalid value input – Enter a positive value
How many numbers would you like to test? -99
Invalid value input – Enter a positive value
How many numbers would you like to test? 10
Please enter a possible perfect number: 1
1 : NOT PERFECT
Please enter a possible perfect number: 0
0 : NOT PERFECT
Please enter a possible perfect number: 6
6:321
Please enter a possible perfect number: 28
28 : 14 7 4 2 1
Please enter a possible perfect number: 496
496 : 248 124 62 31 16 8 4 2 1
Please enter a possible perfect number: 9
9 : NOT PERFECT
Please enter a possible perfect number: 9999
9999 : NOT PERFECT
Please enter a possible perfect number: 3
3 : NOT PERFECT
Please enter a possible perfect number: 31
31 : NOT PERFECT
Explanation / Answer
/**The java program TestPerfectDriver that prompts user to
* enter number of values to enter. If value is not valid,
* then prompt for valid number.
* Then prompt for numbers. If number is perfect square then
* print its factors otherwise print the number is not perfect.*/
//TestPerfectDriver.java
import java.util.Scanner;
public class TestPerfectDriver
{
public static void main(String[] args)
{
//Create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
//declare an integer variable ,n
int size;
int number;
//repeat the while loop until user enter a valid number
do
{
System.out.print("How many numbers would you like to test?");
size=Integer.parseInt(scanner.nextLine());
if(size<=0)
System.out.println("Invalid value input – Enter a positive value");
}while(size<=0);
//for loop that prompt for numbers
for (int i = 0; i < size; i++)
{
System.out.println("Please enter a possible perfect number:");
number=Integer.parseInt(scanner.nextLine());
//Call testPerfect wiht number and if true
//then call printFactors otherwise print
//not perfect
if(testPerfect(number))
printFactors(number);
else
System.out.print(number+": NOT PERFECT ");
}
}
//Method that takes an integer value and print the factors
private static void printFactors(int number) {
System.out.print(number+":");
for (int i = number/2; i >=1; i--)
{
if(number%i==0)
System.out.printf("%6d",i);
}
System.out.println();
}
//Method testPerfect that takes an integer number
//returns true if the number is perfect number otherwise returns false
private static boolean testPerfect(int number) {
int total=0;
for (int i = 1; i < number; i++)
{
if(number%i==0)
total+=i;
}
if(number==total)
return true;
else
return false;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
How many numbers would you like to test?-1
Invalid value input – Enter a positive value
How many numbers would you like to test?0
Invalid value input – Enter a positive value
How many numbers would you like to test?-99
Invalid value input – Enter a positive value
How many numbers would you like to test?10
Please enter a possible perfect number:
1
1: NOT PERFECT
Please enter a possible perfect number:
0
0:
Please enter a possible perfect number:
6
6: 3 2 1
Please enter a possible perfect number:
28
28: 14 7 4 2 1
Please enter a possible perfect number:
496
496: 248 124 62 31 16 8 4 2 1
Please enter a possible perfect number:
9
9: NOT PERFECT
Please enter a possible perfect number:
9999
9999: NOT PERFECT
Please enter a possible perfect number:
3
3: NOT PERFECT
Please enter a possible perfect number:
31
31: NOT PERFECT
Please enter a possible perfect number:
25
25: NOT PERFECT
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.