Euclid's Algorithm Program / 80, 90 & 100 Point Versions
Assignment Purpose:
This program has two goals. First, you need to demonstrate knowledge of selection and repetition control structures. Second, you need to enter values into the program variables with command line arguments.
This lab assignment involves using Euclid's Algorithm for computing the Greatest Common Factor (GCF). This 2000-year-old algorithm is suited very well for the sequential instructions required by a computer program. Euclid's Algorithm is shown below using 120 and 108 as sample numbers to compute the GCF of 12.
Algorithm Steps / Sample ProblemStep 1:
Start with two integers / Integer1 is 120
Integer2 is 108
Step 2:
Divide Integer1 by Integer2 and compute the remainder. / 120 / 108 = 1
The remainder = 12
Step 3:
If the remainder equals 0, you are
finished. The GCF is Integer2. / The remainder is not 0
You are not finished.
Step 4:
If the remainder is not 0 then
Integer1 becomes Integer 2 and
Integer2 becomes the remainder / Integer1 is now 108
Integer2 is now 12
Step 5:
Go to Step2:
Step 2:
Divide Integer1 by Integer2 and compute the remainder. / 108 / 12 = 9
The remainder = 0
Step 3:
If the remainder equals 0, you are
finished. The GCF is Integer2. / The remainder is 0
You are finished and the
GCF = 12
In previous chapters you learned about using class methods and object methods. A well-designed program is broken up into modules for different tasks and these modules become classes and methods. You have not yet learned how to create your own methods and classes. In Chapter VI you are focusing strictly on control structures and input at the command line prompt. The next chapter will continue with more details about OOP and how to divide a program up into separate modules. For now this assignment needs to be done within the main method body { } boundaries.
The student version of the Lab07 assignment is shown below. You are provided with the means to enter two or three values at the command prompt. The third argument is only used for the Lab070.java version of the Greatest Common Factor assignment.
Lab06 Student Version// Lab06st.java
// This is the student version of the Lab06 assignment.
public class Lab06st
{
public static void main(String args[])
{
nr1 = temp1 = Integer.parseInt(args[0]);
nr2 = temp2 = Integer.parseInt(args[1]);
// nr3 = temp3 = Integer.parseInt(args[2]); // only needed for the 100-point version
}
}
80 Point Version
The 80-point version needs to compute the GCF of two numbers only. The output is shown from the command prompt. It can also be done with JCreator by altering the settings to enable argument input for the main method in a special window.
Lab06 80 Point Version / Three Separate Required OutputsC:\Java2001\Labs>java Lab068 10 15
Number 1: 10
Number 2: 15
GCF: 5
C:\Java2001\Labs>java Lab068 120 216
Number 1: 120
Number 2: 216
GCF: 24
C:\Java2001\Labs>java Lab068 7 11
Number 1: 7
Number 2: 11
GCF: 1
90 Point Version
The 90-point version adds computing the Least Common Multiple (LCM) after the GCF is computed. The GCF makes computing the LCM very easy. Start by using Euclid's algorithm to compute the GCF, which is the 80-point version. Then use the following formula to compute the LCM. Keep in mind that you must first compute an accurate GCF.
LCM = Number1 / GCF * Number2
Assume that Number1 = 120 and Number2 = 108. The earlier GCF explanation showed that the GCF of 120 and 108 = 12. With that information and the LCM formula we get the following result.
120 / 12 * 108 = 1080
Lab06 90 Point Version / Three Separate Required OutputsC:\Java2001\Labs>java Lab069 10 15
Number 1: 10
Number 2: 15
GCF: 5
LCM: 30
C:\Java2001\Labs>java Lab069 120 216
Number 1: 120
Number 2: 216
GCF: 24
LCM: 1080
C:\Java2001\Labs>java Lab069 7 11
Number 1: 7
Number 2: 11
GCF: 1
LCM: 77
C:\Java2001\Labs>
100 Point Version
The 100-point version requires the computation of both the GCF and the LCM, as was done for the 90-point version. The added feature for 100 points is that both the GCF and the LCM need to be computed for three integers, not just two.
Is there a GCF formula for three numbers? Is there a LCM formula for three numbers? Perhaps there is, but for the 100-point version you are expected to figure that part out yourself. The sample execution on the next page shows how the output should appear. The rest is up to you.
Important NoteBy now you should be used to the pattern of having your program graded on your computer monitor. Please do not be foolish by trying to generate the required output by using only println output statements. Your program needs to compute the GCF and the LCM. The source code of your program will prove that you followed the lab assignment's requirements.
Lab06 100 Point Version / Three Separate Required Outputs
C:\Java2001\Labs>java Lab060 10 15 25
Number 1: 10
Number 2: 15
Number 3: 25
GCF: 5
LCM: 150
C:\Java2001\Labs>java Lab060 120 216 48
Number 1: 120
Number 2: 216
Number 3: 48
GCF: 24
LCM: 2160
C:\Java2001\Labs>java Lab060 7 11 2
Number 1: 7
Number 2: 11
Number 3: 2
GCF: 1
LCM: 154
C:\Java2001\Labs>
Exposure Java Chapter VI Lab Assignment Page 6 04-28-03