Question: JAVA: Write a program that computes the prime factorization of a positive integer number. The number in question is computed as the product of prime

JAVA: Write a program that computes the prime factorization of a positive integer number. The number in question is computed as the product of prime numbers raised to various exponents. The prime factorization problem is finding the primes and their exponents. For instance, number 72 is the product 72 = 2^3*3^2 and 60 = 2^2*3*5, where ^ is the power (exponentiation) operator; ^ has higher precedence than multiplication.

Write a class PrimeFactorizer with the following skeleton structure:

public class PrimeFactorizer { /** * Initialize the object with target number n. */ public PrimeFactorizer(int n) { ... } /** * Return n, the target number. */ public int getN() { ... } /** * Compute factorization. Do not repeat operation if it was called before. */ public void compute() { ... } /** * Return the factors and exponents in two arraylists. Call compute() first, if necessary. * For instance, if n=60, primes=[2,3,5], and exponents=[2,1,1]. * This function overwrites the 'n' parameter passed to the constructor. */ public void getFactorsAndExponents(int n, ArrayList primes, ArrayList exponents) { .... } /** * Return a string with the "pretty" representation of the prime factorization. * For instance, if n is 60, then toString() for the PrimeFactorizer(60) object * should be "60 = 2^2*3*5". Call compute() if not done before. */ public String toString() { .... } ...... // other code, helper functions, etc. }

Write a test class PrimeFactorTest with a main() function that reads an int number n from the terminal with the Scanner object, computes its prime factorization with a PrimeFactorizer object, and then displays the factorization to the terminal with System.out.println().

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!