Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
AP Computer Science I
Euclid's Algorithm Program
Java Keyword Lab Assignment # 06
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
Step 1:
Start with two integers
Step 2:
Divide Integer1 by Integer2 and
compute the remainder.
Step 3:
If the remainder equals 0, you are
finished. The GCF is Integer2.
Step 4:
If the remainder is not 0 then
Integer1 becomes Integer 2 and
Integer2 becomes the remainder
Step 5:
Go to Step2:
Step 2:
Divide Integer1 by Integer2 and
compute the remainder.
Step 3:
If the remainder equals 0, you are
finished. The GCF is Integer2.
Sample Problem
Integer1 is 120
Integer2 is 108
120 / 108 = 1
The remainder = 12
The remainder is not 0
You are not finished.
Integer1 is now 108
Integer2 is now 12
108 / 12 = 9
The remainder = 0
The remainder is 0
You are finished and the
GCF = 12
Exposure Java Chapter VI Lab Assignment Page 1 04-28-03
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 Outputs
Exposure Java Chapter VI Lab Assignment Page 2 04-28-03
C:\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 Outputs
Exposure Java Chapter VI Lab Assignment Page 3 04-28-03
C:\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 Note
By 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
Exposure Java Chapter VI Lab Assignment Page 4 04-28-03
Number 1:
Number 2:
Number 3:
GCF:
LCM:
10
15
25
5
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 5 04-28-03
Exposure Java Chapter VI Lab Assignment Page 6 04-28-03