[Solved] CSE174-program 10- Review the definition of a prime number

$25

File Name: CSE174-program_10-_Review_the_definition_of_a_prime_number.zip
File Size: 546.36 KB

SKU: [Solved] CSE174-program 10- Review the definition of a prime number Category: Tag:
5/5 - (1 vote)

Preliminaries:

  • Review the definition of a prime number (any positive integer with exactly two factors). The first few prime numbers are 2, 3, 5, and 7. Note that 1 is not a prime number by this definition.
  • Review how to format numbers to fit a particular width using printf().

Assignment requirements:

Write a program named PrimeStuff that presents the user with a menu of options related to prime numbers. The user should be able to work with prime numbers up to and including the maximum value for Javas long data type. Whenever the user enters input, make sure that their numbers are valid, using a loop to prevent them from entering invalid numbers. Match the formatting as close as possible to what is shown in the sample run. Some special notes:

  • Using a Scanner that works in multiple methods: Do not create a separate Scanner object in several methods. Instead, your program should have one Scanner object that is shared by all the methods that need it. In order to do this, you will create a global Scanner object as shown. This creates it inside the class but outside of the methods:
  • Factoring numbers: Factor the number into prime numbers, from smallest to largest. For example, 36 = 2 * 2 * 3 * 3. Note that if a number is prime, then its prime factorization is itself. So, if the user wants to factor the number 83 which is prime, it would look like this: 83 = 83. Note that 1 is not prime. If the user wants to factor 1, just display 1 = 1.
  • Listing prime numbers: This should let the user list as many prime numbers as she wants (up to 1000 numbers), starting at whichever positive integer she wants, with any number of numbers per row (up to 20 numbers per row). Note that if the users starting number is a prime number, then the list should begin with that prime number. If the users starting number is not prime, then the list should begin with the first prime after that number. When listing numbers in multiple rows, the numbers should be right-aligned over one another. See the sample run for examples.
  • Validating input: You should never allow the user to try to use an illegal value. You may assume that the user will always enter an integer, but you should use a loop to make sure that the integer is always within the appropriate range. You may assume that if the user wants to list primes, she will always use a starting value that will not cause the list to exceed the maximum long value. See sample run for other limits.
  • Use short methods: No method should contain more than 30 lines of code. Most methods can be written in 15 or fewer lines of code.
  • Avoid code duplication: As much as possible, do not perform a calculation in more than one place. For example, there should only be one place in your code where you check to see if a number is prime.
  • Submitting: Whenever you make progress (reach a new milestone), submit a draft. The class name should be PrimeStuff. It should be the only file in a folder named program10. Zip the program10 file and submit the resulting .zip file.

Sample run:

Your program should match this format as closely as possible. Note that text shown in red is there because the user typed it. You are not supposed to print those.

What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 0**** INVALID OPTION ****What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 8**** INVALID OPTION ****What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 1Enter a number between 1 and 9223372036854775807: -6Enter a number between 1 and 9223372036854775807: 1001> 1001 is not prime.What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 2Enter a number between 1 and 9223372036854775807: 1001> 1001 = 7 * 11 * 13What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 1Enter a number between 1 and 9223372036854775807: 89> 89 is prime.What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 2Enter a number between 1 and 9223372036854775807: 89> 89 = 89What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 2Enter a number between 1 and 9223372036854775807: 256> 256 = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 2Enter a number between 1 and 9223372036854775807: 1> 1 = 1What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 3Enter starting number between 1 and 9223372036854775807: 755How many primes (1-1000): 0How many primes (1-1000): 1001How many primes (1-1000): 25How many primes per row (1-20): 0How many primes per row (1-20): 21How many primes per row (1-20): 8757 761 769 773 787 797 809 811821 823 827 829 839 853 857 859863 877 881 883 887 907 911 919929What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 3Enter starting number between 1 and 9223372036854775807: 15677How many primes (1-1000): 100How many primes per row (1-20): 1015679 15683 15727 15731 15733 15737 15739 15749 15761 1576715773 15787 15791 15797 15803 15809 15817 15823 15859 1587715881 15887 15889 15901 15907 15913 15919 15923 15937 1595915971 15973 15991 16001 16007 16033 16057 16061 16063 1606716069 16073 16087 16091 16097 16103 16111 16127 16139 1614116183 16187 16189 16193 16217 16223 16229 16231 16249 1625316267 16273 16301 16319 16333 16339 16349 16361 16363 1636916381 16411 16417 16421 16427 16433 16447 16451 16453 1647716481 16487 16493 16519 16529 16547 16553 16561 16567 1657316603 16607 16619 16631 16633 16649 16651 16657 16661 16673 What would you like to do?1) Check if a number is prime2) Factor a number3) List prime numbers4) QuitChoice: 4

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSE174-program 10- Review the definition of a prime number
$25