Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Program Objective Note: This homework does not use polymorphism. We are going to do a math problem instead Find the set of smallest factors

Java Program

Objective

Note: This homework does not use polymorphism. We are going to do a math problem instead

Find the set of smallest factors that compose a number

  • Example: n = 90
    • Factors = {1, 2, 3, 5, 6, 9, 10, 15, 18, 30, 45, 90 }
    • Least Factors = {2,3,3,5}
  • That is, 2 * 3 * 3 * 5 = 90
  • Notice these are the smallest of the factors?

Part 1

Write a method that returns a List of all factors for a variable n

What are the factors of n?

  • They are the numbers from 1 to n that divide evenly into n
    • Note: No number greater than n can divide evenly into it
  • You can test if something divides evenly with Modulus (%)
    • Returns the remainder of the division

Part 2

Write a Method that takes a List of factors and the number n and returns the List of smallest factors

Follow this algorithm (written in pseudocode)

  • while(n != 1)
    • i = 1
    • nextDivFound = false
    • while(nextDivFound == false)
      • if factors[i] divides evenly into n
        • nextDivFound = true
        • returnList.add(factors[i])
        • n = n / factors[i]
      • else i++

Note that the factor[0] is skipped. This will contain the number 1 and will lead to an infinite loop.

Main Function

Read in the value n from the console using any method you prefer.

  • Example is Scanner from java.util.*
  • Scanner scan = new Scanner(System.in);
  • int n = scan.nextInt();

Call your first method to find the factors then output the List it returns. Next call the second method to find the smallest factors and output the List it returns.

image text in transcribed

nter n: Enter n: 5 Enter n: 12 Factors: 1 2 3 4 6 12 Factors: 1 2 3 5 69 10 15 18 30 45 90 Factors 1 5 Smallest Factors: 2 3 3 5 Smallest Factors: 5Smallest Factors: 2 2 3

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

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

interest on dollar 8,400 at 7% for 60 days is

Answered: 1 week ago