Question: JAVA SCRIPT For this problem, we wish to prompt the user for a natural number, determine all of the factors (if any exist) and print

JAVA SCRIPT

For this problem, we wish to prompt the user for a natural number, determine all of the factors (if any exist) and print the result to the console. For example, when the user enters 10,920, the program should print all of the factors (including any repeated factors)

If the number the user enters is prime, the output should indicate the number the user entered is prime

3. Create a method inside the class clsFactorGenerator with the signature static boolean hasMoreFactors(int intcurrentNumber). This method will take 1 parameter of type int named intcurrentNumber and will return a boolean value. If the number stored in intcurrentNumber is composite, the method will return true. If the number stored in intcurrentNumber is prime, the method will return false.

a. In the body of the method do the following:

  1. Create a local variable of type boolean named blnnextFactor and assign the value

    false to blnnextFactor. HINT: THIS VARIABLE WILL BE USED TO INDICATE WHETHER THE NUMBER STORED IN INTCURRENTNUMBER CONTAINS ANY FACTORS.

  2. Use a loop to iterate through the numbers from 2 to the number before the number stored in intcurrentNumber.

1. In the body of the loop, use a decision structure to determine if the iterated number is a factor of intcurrentNumber. HINT: USE THE MODULO OPERATOR TO DETERMINE IF THE REMAINDER IS 0.

a. If the current iterated number is a factor of intcurrentNumber set the value of blnnextFactor to true.

iii. Return blnnextFactor. 4. Create a method inside the class clsFactorGenerator with the signature static int

nextFactor(int intcurrentNumber). This method will take 1 parameter of type int named intcurrentNumber and will return an integer value of the smallest factor of intcurrentNumber. This method assumes the parameter intcurrentNumber contains a composite number.

a. In the body of the method do the following:

  1. Create a local variable of type int named intfactor and assign the value 0 to intfactor.

    HINT: THIS VARIABLE WILL BE USED TO STORE THE SMALLEST INTEGER FACTOR OF

    INTCURRENTNUMBER.

  2. Use a loop to iterate through the numbers 2 to the number before the number stored in intcurrentNumber.

1. In the body of the loop, use a decision structure to determine if the iterated number is a factor of intcurrentNumber. HINT: USE THE MODULO OPERATOR TO DETERMINE IF THE REMAINDER IS 0.

a. If the current iterated number is a factor of the number stored in intcurrentNumber, store the current iterated number in intfactor and stop the loop prematurely.

iii. Return intfactor. 5. In the main method do the following:

  1. Create a variable of type Scanner named scnUserInput using System.in InputStream.

  2. Prompt the user to enter a natural number using a prompt message of your choice and store the

    result in a variable of type int named intuserInput.

c. Use a decision structure to determine if the number is prime or composite using the

hasMoreFactors() method. i. If the number can be factored:

1. Use a loop to calculate the factors of intuserInput using the nextFactor() method and print each factor on a separate line in ascending order. See Figure 2: clsFactorGenerator Factored Number Output for an example. HINT: YOU NEED TO REMOVE EACH FACTOR YOU CALCULATE FROM THE NUMBER STORED IN INTUSERINPUT TO CALCULATE THE NEXT FACTOR.

ii. If the number cannot be factored: 1. Print the number and a message stating the number is prime to the console

Close the Scanner object.

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!