Date: Wednesday, March 4, 2020.
Due date: Wednesday, March 25, 2020.
Objective: To define methods in Java.
In this project we will rework the program from the last that inputs two integer numbers from the user simplifies the fraction composed of the two numbers. This time we will organize the code into multiple methods.
Create a Java project in Eclipse and call it lab8. Create a class in this project called Fraction8 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.
Copy the code from your previous file Fraction.java that you created the last week. Test the program to make sure it still works.
a. Greatest Common Divisor Method
Below the main method but before the brace closing the class, add a static function called gcd, returning an integer value, and taking two integer parameters n and m, with the following header:
static int GCD(int n, int m)
Add a pair of empty braces after it. Add a comment on a line before the method header briefly explaining what the function does.
Going back to the main, identify the part of the code that computes the GCD of n and m, without the output statement. Cut the code from the main and paste it inside the body of the function GCD.
To make this function work, declare the gcd as a local variable of type int. Then before the end of the function, add a line returning the value of gcd.
Going back to the main, add a line in the place where you removed the code from, where you assign to the variable gcd the value returned by the function call GCD(n, m). Run the program to make sure its execution works the same way.
b. Fraction Simplification Method
Let's make the fraction simplification another method. Add another method below the GCD with the header:
static void simplify(int n, int m)
Add a pair of braces after it, then move the code from the main starting from the line that computes the GCD, all the way to the line that outputs the simplified fraction, into the function simplify.
For this function to work, declare the gcd as a local variable. Then since we don't need it in the main anymore, delete the declaration of this variable in the main.
Add a call in the main in the place where you removed the code from. Test the program again.
c. Run Again Method
Finally, let's create another function that asks the user if they want to try again with another set of numbers. This function will return true or false, so it's a boolean value. The process of asking the user the question and inputting the answer requires the use of a scanner. Since we already have one in the main and we don't want to define another one, we'll pass it in as parameter. Add a method with the following header:
static boolean runAgain(Scanner scan)
Add braces, then move the two lines asking the user if they want to try another fraction and imputing the variable again into this function. Then test if the value of again is 1, and if it is, then return true, and if not, return false. Declare the variable again as local in this function and delete it from the main.
Going back to the main, replace the test for again being 1 in the continuation condition with a call to the function runAgain with scan as the parameter.
Test the program again to make sure it still runs the same. This is the end of this lab.
Upload Fraction8.java to Canvas in Assignments - Homework 8, or wait until you have completed the homework and the second part of the lab to upload all the files at the same time.