$25
PROGRAM #8: 40 points –
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
Scoring
At a bare minimum, the program you submit must have the assigned source code, and your source code must compile and run without crashing.
• If you do not submit a zip file containing your source code (.java file), your score will be zero.
• If you submit source code that does not compile, your score will be zero.
• If you submit source code that roughly resembles the requirements and it compiles, but it crashes under normal operating conditions (nice input from the user), your score will be reduced by 75%.
• Deductions will be made for not meeting the usual requirements:
● Source code that is not formatted according to guidelines
● File and class names that do not meet specifications
Full credit
No credit or Partial credit
Use a menu to obtain user input
(6 points)
Your menu allows the user to choose any valid option, but does not allow any invalid integer values.
There are errors in the way the menu handles valid and/or invalid input.
Compute the highest and the lowest pairs
(10 points)
Your program correctly computes the two highest and the least two pairs of n numbers read from the user. It only allows valid integer values from the user.
There are errors in the way the your code is determining the highest and lowest pairs.
PI approximation
(12 points)
Your program correctly calculate the approximation of PI value. It only allows valid positive integer value from the user.
Your program doesn’t calculate the approximation of PI value correctly. It allows negative integer value from the user.
Greater common divisor (8 points)
Your program correctly finds the greater common divisor of the 2 numbers entered by the user.
Your program incorrectly calculating the greater common divisor of the 2 numbers entered by the user.
Format console output
(4 points)
You formatted output as specified, including aligning numbers.
Screen output does not match specifications.
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