Computer Science
CMPS 260, Fall 2021
Programming Assignment #3 (125 points)
All coding to solve the following problem is to be done by you and only you. You may discuss the requirements of this project, Java, or Intellj with anyone, but you must code your own solution. You may use text, class notes and examples, and online Java resources. You may discuss your code with the instructor, TAs or mentors, but no one else. You may not provide your solution to anyone else. The project is to be created using IntelliJ and Azul Zulu Java 11.
Assignment Purpose
This assignment estimates the value of pi by calculating the percentage of times two random numbers are relatively prime. A more in-depth theoretical explanation and the inspiration for this assignment can be found at the following video link.
Assignment
Follow the numbered instructions below.
1. First, create an IntelliJ Java project and name it pa3-ULID, replacing the term ULID with your University-issued ULID (e.g., C00000000).
2. Starting at the topmost line of the file, insert the following minimally required documenta- tion, filling in your name, ULID, the assignment number, due date and a brief description of what the program will do. You must select one of the two forms of certification of Au- thenticity. (And, no, it is not permissible to substitute your own form.) Submissions not including a certification of authenticity will not be graded. Note that IntelliJ auto-saves only when the program is run.
// Your Name
// Your ULID
// CMPS 260
// Programming Assignment : [insert assignment number here]
// Due Date : [insert date here]
// Program Description: [insert brief description here]
// Certificate of Authenticity: (choose one from below)
// I certify the code in method functions main, createArray, and
// percentageRelativelyPrime of this project are entirely my own work.
(or)
// I certify the code in method functions main, createArray, and
// percentageRelativelyPrime of this project are entirely my own work,
// but I received assistance from [insert name].
// Follow this with a description of the type of assistance.
This document is copyrighted by Nicholas Lipari, PhD and is meant for the sole use of students enrolled in CMPS 260 at UL Lafayette. No reproduction or posting of any portion is permitted.
1 of 4
CMPS 260 Programming Assignment #3 © Nicholas Lipari, PhD — Fall 2021
For example, if you consulted a book, and your solution incorporates ideas found in the book, give appropriate credit; that is, include a bibliographical reference. Note: You do not have to list the course textbook or the instructor’s examples.
3. In the main method, write the Java code that computes an estimate of pi based on the percentage of numbers from a random sampling that are relatively prime (e.g., coprime). The instructions for createArray and percentageRelativelyPrime along with the source code for gcd are given below.
(a) Prompt the user for two integers: a number of samples (numSamples) and a maximum random number (maxRandom).
(b) Make two calls to createArray to populate two arrays (array1 and array2 ) with numSamples integers from 1 to maxRandom, including the endpoints.
(c) Call the method percentageRelativelyPrime with the actual parameters array1 and array2, storing the return value in a double variable percent.
(d) Print the result of the following equation with six (6) positions after the decimal.
πestimate =
√ 6.0
percent
(e) Ask the user if they would like to continue. Repeat the above steps until the user elects to stop.
4. Write a new method in the same class as the method main to create an array of random integers and return the reference of the array.
(a) The public and static method named createArray accepts two (2) integer formal parameters and returns an integer array reference variable.
(b) The two formal parameters define the size of the array to be created and the maximum value to be stored in the array.
(c) Declare an integer array reference variable and array object with the number of elements from the size parameter. Should the size parameter be zero or negative, create an array of size one hundred (100).
(d) Fill the integer array with random values based on the following expression: R∗maxV al+1, where R is a call to Math.random() and maxV al is the maximum value parameter. Should maxV al by zero or negative, replace the maximum value parameter with one hundred (100).
(e) Return the array reference variable to the calling method.
5. Write a new method in the same class as the method main to repeatedly call the gcd method (given below) for every corresponding pair of elements from two integer arrays and return the percentage of pairs that were relatively prime.
(a) The public and static method named percentageRelativelyPrime accepts two (2) integer array formal parameters and returns a double value.
This document is copyrighted by Nicholas Lipari, PhD and is meant for the sole use of students enrolled in CMPS 260 at UL Lafayette. No reproduction or posting of any portion is permitted.
2 of 4
CMPS 260 Programming Assignment #3 © Nicholas Lipari, PhD — Fall 2021
(b) The two formal parameters A and B are arrays of equal length. Iterate through the elements of the arrays and increment a counter whenever Ai and Bi are relatively prime (i.e., their greatest common divisor is 1).
(c) Compute and return the counter divided by the number of elements in an array.
public static int gcd(int number1, int number2) {
int smaller = Math.min(number1,number2);
for(int lcv = smaller; lcv > 1; lcv--) {
if(number1 % lcv == 0 && number2 % lcv == 0)
return lcv; //the gcd of number1 and number 2
}
return 1; //number1 and number2 are relatively prime
}
Example Program Executions
The examples below contain only minimal prompts and output. User input is indicated as red text. Output computed by the program is indicated in blue text.
Number of samples to test: 1234567 Maximum number to test: 12345 pi is approximately 3.141111 Would you like to try again? (Yes/No) Yes
Number of samples to test: 12345678 Maximum number to test: 12345 pi is approximately 3.141448 Would you like to try again? (Yes/No) Yes
Number of samples to test: 12345 Maximum number to test: 1234567 pi is approximately 3.145127 Would you like to try again? (Yes/No) Yes
Number of samples to test: 0 Maximum number to test: 0 pi is approximately 3.188964 Would you like to try again? (Yes/No) No
This document is copyrighted by Nicholas Lipari, PhD and is meant for the sole use of students enrolled in CMPS 260 at UL Lafayette. No reproduction or posting of any portion is permitted.
3 of 4
CMPS 260 Programming Assignment #3 © Nicholas Lipari, PhD — Fall 2021
Additional Requirements
The following coding and implementation details must be present in your solution to receive full credit for Programming Assignment #3.
1. A reference variable and instance object of class java.util.Scanner must be used to read the input from the user.
2. Identifiers must be descriptive (i.e., must self document).
3. Indention of all code blocks (compound statements, code inside braces), including single statements following selection or while statements, is required.
Submitting
In Intellij, select File, Export, Project to Zip File, navigate to a location outside the the project folder, then click OK to save the ZIP file. Be sure to name the file pa3-ULID.zip, replacing the term ULID with your University-issued ULID (e.g., C00000000). Finally, upload the ZIP file to Moodle.
Helpful Hint: Keep a backup copy of your project folder on a Google Drive, a Drop Box Account, a USB memory device, etc., or even on Moodle. Finally, once you turn in your final version, create a copy before the due date and do not change this copy in any way. This final copy can be consulted if there is an upload disaster, but only if the “.java” files have not been changed in any way after the due date.
This document is copyrighted by Nicholas Lipari, PhD and is meant for the sole use of students enrolled in CMPS 260 at UL Lafayette. No reproduction or posting of any portion is permitted.
4 of 4