Starting from:
$35

$29

PROGRAM #8 Solution

Outcomes
    • Write programs that obtain user input
    • Write programs that use loops to perform calculations and validate input
    • Write programs that use if and else statements
    • Write programs that display numbers formatted according to a given specification
    • Format and comment source code that adheres to a given set of formatting guidelines 

Preliminaries: 
    • Review the definition of a greater common divisor.

Assignment
Write a program named PatternChecker that presents the user with a menu of options based on the user’s choice related to generating some patterns on numbers.
The options are:
    1. Largest and smallest: Construct two pairs of numbers from the user’s input of n numbers, the largest and smallest pairs.  For example, if the user’s choice of n is 7 and enters the numbers 44, 7, 29, 16, 82, 21 and 38 as input, the result should be (82, 44) and (7, 16).  Note that you are not allowed to use arrays. You need to achieve this task using a loop and the user’s input is stored in a single variable and overwritten each time with the new value inside the loop. N should be greater than or equals 2.
    2. PI approximation: The value of π can be determined by the series equation

This option let the user specify n, the number of terms used in approximating π. For example, if n = 3, your code should calculate use only three terms to calculate 
 = 3.46

    3. Greatest common divisor: Given two positive numbers determine their gcd. 
For example, the greatest common divisor of 165 and 90 is 15. 


Requirements
    • 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.
    • 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:


    
    • 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.  
    • 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.  
    • Submitting: Whenever you make progress (reach a new milestone), submit a draft.  The class name should be PatternChecker. It should be the only file in a folder named program8.  Zip the program8 file and submit the resulting .zip file.

Sample runs
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.
Sample run 1
List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 1
Enter the limit: 6
Enter 6 numbers: 10 20 30 1 2 3
*** Largest and Smallest Pairs: (30, 20),(1, 2)

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 2
Enter the number of terms you want to use for approximating the PI value: 3
*** PI is approximately = 3.47

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 3
Enter two numbers: 165 90
*** The greatest common divisor of 165 and 90 = 15

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 4
*** End

Sample run 2
List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: -10
Invalid input

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 0
Invalid input

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 5
Invalid input

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 1
Enter the limit: 0
Invalid input. The limit should be at least 2
Enter the limit: 2
Enter 2 numbers: 8 3
*** Largest and Smallest Pairs: (8, 3),(3, 8)

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 1
Enter the limit: 4
Enter 4 numbers: 5 10 2 7
*** Largest and Smallest Pairs: (10, 7),(2, 5)

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 2
Enter the number of terms you want to use for approximating the PI value: 1000
*** PI is approximately = 3.14

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 3
Enter two numbers: 70 90
*** The greatest common divisor of 70 and 90 = 10

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 0
Invalid input

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 2
Enter the number of terms you want to use for approximating the PI value: 1000
*** PI is approximately = 3.14

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 3
Enter two numbers: -10 10
Invalid input. Enter two positive numbers: 10 20
*** The greatest common divisor of 10 and 20 = 10

List of Pattern Checker problems:
1) Largest and Smallest Pairs
2) PI approximation
3) Greatest common divisor
4) Quit
Choice: 4
*** End


More products