// THIS CODE IS MY OWN WORK, IT WAS WRITTEN WITHOUT CONSULTING // A TUTOR OR CODE WRITTEN BY OTHER STUDENTS - YOUR NAME /** * Euclid3 is a revision of Euclid.java from the start of the semester * * We compute the GCD (greatest common divisor) of two integers * by the Euclid's algorithm, but now in a separate gcd method * that RETURNS the Greatest Common Divisor (instead of priniting it), * which we will test several times from the main method. * * @version cs170 Fall 2011 */ // TODO (in BlueJ): // // * Remember to fix the honor statement. (We interpret "THIS CODE" // to just mean the changes you make to this file.) // // * Read what the main() method (see below) is doing // // * Read what the testGcd() method (see below) is doing // // * Read what the gcd() method (see below) is doing // // * Compile and run the program in BlueJ. // // You should see as the last line: // Passed 6 tests out of 7 // This test fail: // Calling gcd(7, 0) ... GCD = 0 not 7, ERROR! // // * Modify the gcd method so that it passes all the tests. // ONLY modify the method named "gcd" (the first one). // DO NOT modify the other two methods (main or testGcd). // // Hint: // Right now, the "gcd" method always returns the variable B. // When one of the number is 0, the GCD is equal to the other number. // You must change: // // return B; // // to return a number based on some condition. // Use: // if ( A == 0 ) // return ??? // else // return ??? // // NOTE: Complete this task before moving on to the next one !!! // // * Modify the gcd method so it passes all tests QUICKLY. // // Hint: Use this IDEA // instead of "A = A - B;", // use: "A = A % B;", // which has the effect of subtracting as many copies of B // as possible, at once. // // * Add a comment before the method "gcd" (the first method), // so that a reasonable description shows up in the editor // "Documentation" view. // How to do it: // In the BlueJ editor, click "Edit" // and then "Add method comment" to get started on this. public class Euclid3 { // Method gcd(A,B) implements Euclid's algorithm. // It RETURNS the greatest common divisor of A and B. public static int gcd(int A, int B) { while ( A != 0 && B != 0 ) { if ( A >= B ) A = A - B; else B = B - A; /* ---------------------------------------------------------- If you want to see what's is going on in the "gcd" method, you can uncomment the println statement on the next line to print A and B inside loop: ---------------------------------------------------------- */ //System.out.println("A=" + A + " B=" + B); } return B; // Return the GCD. } // ------------------------------------------------------ // Do NOT change these 2 methods below !!!! // ------------------------------------------------------ /** * Method testGcd(A,B,G) tests whether gcd(A,B) returns * the expected value G. * It prints out a message indicating whether the gcd method passed * this test. * * @param A a non-negative integer * @param B a non-negative integer * @param G the expected value of gcd(A,B) * @return Indicates whether gcd(A,B) returned the expected value G. */ public static boolean testGcd(int A, int B, int G) { System.out.print("Calling gcd(" + A + ", " + B + ") ... "); int result = gcd(A, B); if (result == G) { System.out.println("GCD = " + result + ", good."); return true; // true means "test successful" } else { System.out.println("GCD = " + result + " not " + G + ", ERROR!"); return false; // false means "test failed" } } /** * Method main is our main entry point. * * It runs several gcd test cases, using testGcd, and finally * prints out a summary of how many tests were pass or fail. * * @param args ignored */ public static void main(String args[]) { // Run several gcd test cases, like codingbat.com int pass = 0; // number of passed tests int fail = 0; // number of failed tests /* ----------------------------------------------------------- Now we run many tests... Example: if testGcd(36, 28, 4) returns true, we increment pass by 1 otherwise we increment fail by 1. And so on ---------------------------------------------------------- */ if (testGcd(36, 28, 4)) { ++pass; } else { ++fail; } if (testGcd(28, 36, 4)) { ++pass; } else { ++fail; } if (testGcd(255, 190, 5)) { ++pass; } else { ++fail; } if (testGcd(0, 5, 5)) { ++pass; } else { ++fail; } if (testGcd(7, 0, 7)) { ++pass; } else { ++fail; } // ============================================================== // These two are slow now! // ============================================================== if (testGcd(2100000001,2,1)) { ++pass; } else { ++fail; } if (testGcd(3,2100000000,3)) { ++pass; } else { ++fail; } System.out.println("Passed " + pass + " tests out of " + (pass+fail) ); System.out.println(); } }