Date: Wednesday, February 26, 2020.
Due date: Wednesday, March 4, 2020.
Objective: To use the for loop in Java.
In this project we will input two integer numbers from the user and find their greatest common divisor. Then we will simplify the fraction composed of the two numbers.
Create a Java project in Eclipse and call it lab7. Create a class in this project called Fraction and containing the main method. Write your name, course, semester, and homework number at the top as a comment. Add the code for the definition of a scanner.
a. Greatest Common Divisor
Declare two integer variables n and m, and input them both from the user. We will assume that both n and m are positive numbers.
Compute the GCD of n and m and store it in a variable called gcd. For this, start by assigning to the gcd, the smallest of the two numbers. Use the conditional operator for that. Then use a for loop going backward towards 1. When you find a value such that both numbers are divisible by it, stop. This is the GCD of the two numbers. Output this value.
b. Fraction Simplification
After computing the GCD of n and m, we can use it to simplify the fraction n/m. For that, we can say that the fraction is equal to (n/gcd) / (m/gcd). Output a statement telling the user what the simplified fraction is.
For example, if n = 100 and m = 15, the GCD is equal to 5, and the fraction 100/15 simplifies to 20/3.
Another example, if n = 12 and m = 18, the GCD is equal to 6, and the fraction 12/18 simplifies to 2/3.
c. Run Again
Let's make the program ask the user if they want to run the computations again for a different set of numbers. Declare a variable called again and initialize it as 1. Then place the entire code except for the variable declarations inside a do-while loop with the continuation condition (again == 1). Inside the loop, after displaying the fraction, ask the user whether they would like to run the program again and simplify another fraction, and to reply with a 0 for no and 1 for yes.
Add an output statement after the loop saying goodbye to the user, so that they know that program has ended.
Run the program to make sure everything works correctly.
Upload Fraction.java to Canvas in Assignments - Homework 7, or wait until you have completed the homework to upload both files at the same time.