Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Stuck on Java Program You are going to write a program that finds the prime factors (Links to an external site.)Links to an external site.of

Stuck on Java Program

You are going to write a program that finds the prime factors (Links to an external site.)Links to an external site.of a number.

image text in transcribed

The user creates a Factors object which passes the number they want factored in the constructor. The user can print out all of the factors present using a while loop. The UML above contains all of the instance variables and methods needed to implement the problem. You will probably need some local variables also to fully implement this.

Explanation of Methods:

Use the constructor to initiate the calculation of the factors. You may want to consider delegating some or all of the work to additional private methods. All the calculated factors should be added to the factorList instance variable

hasMoreFactors() and nextFactor() are meant to be utilized within a loop (in your Program class) by a consumer of the class ONLY AFTER the factors have been calculated. Essentially, the pattern (for the consumer) is: while there are factors, get the next factor. Use the factorIndex as an internal counter for each pass through the loop.

hasMoreFactors() should use the factorIndex and compare it to the factorList boundary to determine if the end has been reached.

getNextFactor() should use the current factorIndex to determine which factor to retrieve and, of course, increment the factor.

Your Program.java should contain code to test your Factorsobject.

Hints:

The parameter passed to the constructor will always be integers greater than or equal to 2.

Compute all of the factors in the constructor.

Use factorIndex to remember where you are in the factor ArrayList.

All loops must be while loops.

Don't forget about integer division and modulo!

Getting Started

We are going to do this exercise by writing the object that solves the problem first (in a source file called Factors.java) and then testing it using code we write into Program.java. Using the techniques shown on the web page titled "How to Start Every Project in this Class" create a source file called Factors.java as well as a file called Program.java.

There is no code to copy for the assignment. You get to do it all! Don't forget to provide proper Javadoc documentation.

Your Program.java should contain code to test your Factors object. Ask the user for input (using a scanner) and outputs the factors sequentially. If you input 150 then your output should look like this:

2 3 5 5

Notice that the numbers are in ascending order. You will need to do that too.

Once you've written your code run the code by single clicking on Program.java in the package explorer and selecting Run->Run from the menu or using the keyboard shortcut. Examine the output. Does it do what you want? If not, how can you modify the code to do what you want?

Factors Program factorList: ArrayList itacixorinx: ini main(args: String[): voi Factors(numberToFactor: int + + hasMoreFactors(): boolean + nextFactor(): int

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

Students also viewed these Databases questions