Question: extra supplied code: Project 4 code // replacement for line 89 in main() ArrayList tests = parseCommandLine(new String[]{all}); // // Find the Greatest Common Divisor

 extra supplied code: Project 4 code // replacement for line 89in main() ArrayList tests = parseCommandLine(new String[]{"all"}); // // Find the GreatestCommon Divisor of Two Integers // //Recursive version // public long gcd(longp, long q) // { // //base case // if(q == 0)// { // return p; // } // return gcd(q, p %q); // } //Iterative version // public long gcd(long p, long q)

extra supplied code:

Project 4 code // replacement for line 89 in main() ArrayList tests = parseCommandLine(new String[]{"all"}); // // Find the Greatest Common Divisor of Two Integers // //Recursive version // public long gcd(long p, long q) // { // //base case // if(q == 0) // { // return p; // } // return gcd(q, p % q); // } //Iterative version // public long gcd(long p, long q) // { // while(q != 0) // { // long temp = q; // q = p % q; // p = temp; // } // return p; // } 

The following are the skeletons for this project I am submitting it as typed text so that it can easily be used in your own software. the commented code is useful intructions for what to do in each area.

this is FractionTester.java :

import java.util.ArrayList;

public class FractionTester

{

private static final int CONSTRUCTOR = 1;

private static final int CONSTRUCTOR_LONG = 2;

private static final int GCD = 3;

private static final int CONSTRUCTOR_LONG_LONG = 4;

private static final int GET_DENOMINATOR = 5;

private static final int GET_NUMERATOR = 6;

private static final int ADD = 7;

private static final int SUBTRACT = 8;

private static final int MULTIPLY = 9;

private static final int DIVIDE = 10;

private static final int TEST_MIN = CONSTRUCTOR;

private static final int TEST_MAX = DIVIDE;

private static final int MAX_NUM_TESTS = TEST_MAX + 1;

private static int TEST = 0;

private static int TEST_CASE = 0;

private static int TEST_CASES_FAILED = 0;

private static boolean TEST_PASSED = false;

private static ArrayList parseCommandLine(String[] args)

{

ArrayList tests = new ArrayList();

boolean perform_all = false;

for (int i = 0; i

{

if (args[i].equals("all"))

{

perform_all = true;

}

}

if (perform_all)

{

System.out.println("All tests specified to be executed.");

for (int i = TEST_MIN; i

{

tests.add(new Integer(i));

}

}

else

{

System.out.println(args.length +

" command line arguments specified to be executed.");

for (int i = 0; i

{

int test = new Integer(args[i]).intValue();

System.out.print(test + " ");

if (test >= TEST_MIN && test

{

tests.add(new Integer(test));

}

}

System.out.println();

}

System.out.println(tests.size() +

" valid tests specified to be executed.");

return tests;

}

private static void dump_error_message(String message)

{

System.out.println(" " + message);

TEST_PASSED = false;

TEST_CASES_FAILED++;

}

private static void checkContents(String contents, String test_contents)

{

if (!contents.equals(test_contents))

{

dump_error_message(" expected [" + test_contents + "] found [" + contents + "]");

}

TEST_CASE++;

}

public static void main(String args[])

{

ArrayList tests = parseCommandLine(args);

boolean[] TestResults = new boolean[MAX_NUM_TESTS];

// initialize test results array

for (int i = 0; i

{

TestResults[i] = false;

}

int total_test_cases = 0;

int total_failed_test_cases = 0;

for (int i = TEST_MIN; i

{

TEST_PASSED = true;

TEST_CASES_FAILED = 0;

if (tests.contains(new Integer(i)))

{

TEST = i;

TEST_CASE = 1;

System.out.println(" Test " + TEST + " started");

switch(i)

{

case CONSTRUCTOR:

testConstructor();

break;

case CONSTRUCTOR_LONG:

testConstructorLong();

break;

case GCD:

testGCD();

break;

case CONSTRUCTOR_LONG_LONG:

testConstructorLongLong();

break;

case GET_DENOMINATOR:

{

int[] requiredPassedTests = { CONSTRUCTOR, CONSTRUCTOR_LONG, CONSTRUCTOR_LONG_LONG, GCD };

for (int rpt = TEST_MIN; rpt

{

if (!TestResults[rpt])

{

dump_error_message("Cannot perform this test since test " + rpt + " failed.");

break;

}

}

testGetDenominator();

}

break;

case GET_NUMERATOR:

{

int[] requiredPassedTests = { CONSTRUCTOR, CONSTRUCTOR_LONG, CONSTRUCTOR_LONG_LONG, GCD, GET_DENOMINATOR };

for (int rpt = TEST_MIN; rpt

{

if (!TestResults[rpt])

{

dump_error_message("Cannot perform this test since test " + rpt + " failed.");

break;

}

}

testGetNumerator();

}

break;

case ADD:

{

int[] requiredPassedTests = { CONSTRUCTOR, CONSTRUCTOR_LONG, CONSTRUCTOR_LONG_LONG, GCD };

for (int rpt = TEST_MIN; rpt

{

if (!TestResults[rpt])

{

dump_error_message("Cannot perform this test since test " + rpt + " failed.");

break;

}

}

testAdd();

}

break;

case SUBTRACT:

{

int[] requiredPassedTests = { CONSTRUCTOR, CONSTRUCTOR_LONG, CONSTRUCTOR_LONG_LONG, GCD };

for (int rpt = TEST_MIN; rpt

{

if (!TestResults[rpt])

{

dump_error_message("Cannot perform this test since test " + rpt + " failed.");

break;

}

}

testSubtract();

}

break;

case MULTIPLY:

{

int[] requiredPassedTests = { CONSTRUCTOR, CONSTRUCTOR_LONG, CONSTRUCTOR_LONG_LONG, GCD };

for (int rpt = TEST_MIN; rpt

{

if (!TestResults[rpt])

{

dump_error_message("Cannot perform this test since test " + rpt + " failed.");

break;

}

}

testMultiply();

}

break;

case DIVIDE:

{

int[] requiredPassedTests = { CONSTRUCTOR, CONSTRUCTOR_LONG, CONSTRUCTOR_LONG_LONG, GCD };

for (int rpt = TEST_MIN; rpt

{

if (!TestResults[rpt])

{

dump_error_message("Cannot perform this test since test " + rpt + " failed.");

break;

}

}

testDivide();

}

break;

default:

System.out.println("Unrecognized test " + i);

}

System.out.println("Test " + TEST + " " + (TEST_PASSED ? "passed" : "failed"));

TestResults[TEST] = TEST_PASSED;

System.out.println("Test " + TEST + " " + "Failed (" + TEST_CASES_FAILED

+ " / " + TEST_CASE + ") test cases");

total_test_cases += TEST_CASE;

total_failed_test_cases += TEST_CASES_FAILED;

TEST_CASES_FAILED = 0;

TEST_CASE = 0;

System.out.println("Test " + TEST + " ended");

}

}

int failed_count = 0;

int passed_count = 0;

for (int i = TEST_MIN; i

{

if (tests.contains(new Integer(i)))

{

if (TestResults[i])

{

passed_count++;

}

else

{

failed_count++;

}

}

}

System.out.println(" Failed " + total_failed_test_cases + " / " + total_test_cases + " test cases");

System.out.println("Passed: " + passed_count + " / " + tests.size() + " tests");

System.out.println("Failed: " + failed_count + " / " + tests.size() + " tests");

}

private static void testConstructor()

{

Fraction test_Fraction = new Fraction();

checkContents(test_Fraction.toString(), "0");

}

private static void testConstructorLong()

{

for (int i = 0; i

{

Fraction test_Fraction = new Fraction(i);

checkContents(test_Fraction.toString(), (new Integer(i)).toString());

}

}

private static void testGCD()

{

long a;

long b;

long gcd_expected;

long gcd_result;

Fraction test_Fraction = new Fraction();

//

// Relatively Prime Tests

//

a = 2; b = 3; gcd_expected = 1;

gcd_result = test_Fraction.gcd(a,b);

if (gcd_result != gcd_expected)

{

dump_error_message("Expected GCD " + gcd_expected + "; found " + gcd_result);

}

TEST_CASE++;

a = 97; gcd_expected = 1;

for (int k = 2; k

{

gcd_result = test_Fraction.gcd(a, k);

if (gcd_result != gcd_expected)

{

dump_error_message("Expected GCD " + gcd_expected + "; found " + gcd_result);

}

TEST_CASE++;

}

a = 2; gcd_expected = 1;

for (int k = 97; k >= 2; k -= 2)

{

gcd_result = test_Fraction.gcd(k, a);

if (gcd_result != gcd_expected)

{

dump_error_message("Expected GCD " + gcd_expected + "; found " + gcd_result);

}

TEST_CASE++;

}

//

// gcd(a,b) > 1

//

for (int k = 2; k

{

gcd_result = test_Fraction.gcd(k, k);

if (gcd_result != k)

{

dump_error_message("Expected GCD " + gcd_expected + "; found " + k);

}

TEST_CASE++;

}

a = 3; gcd_expected = 3;

for (int k = 3; k

{

gcd_result = test_Fraction.gcd(a, k);

if (gcd_result != gcd_expected)

{

dump_error_message("Expected GCD " + gcd_expected + "; found " + gcd_result);

}

TEST_CASE++;

}

a = 7654321; b = 56791; gcd_expected = 19;

gcd_result = test_Fraction.gcd(a, b);

if (gcd_result != gcd_expected)

{

dump_error_message("Expected GCD " + gcd_expected + "; found " + gcd_result);

}

TEST_CASE++;

}

private static void testConstructorLongLong()

{

for (int i = 1; i

{

Fraction test_Fraction = new Fraction(i, i);

checkContents(test_Fraction.toString(), "1");

}

for (int i = 1; i

{

Fraction test_Fraction = new Fraction(i, i*2);

checkContents(test_Fraction.toString(), "1/2");

}

for (int i = 1; i

{

Fraction test_Fraction = new Fraction(2*i, i*3);

checkContents(test_Fraction.toString(), "2/3");

}

for (int i = 1; i

{

Fraction test_Fraction = new Fraction(i, -1);

checkContents(test_Fraction.toString(), new Integer(-i).toString());

}

}

private static void testGetDenominator()

{

//

// Add tests here

//

}

private static void testGetNumerator()

{

//

// Add tests here

//

}

private static void testAdd()

{

//

// Add tests here

//

}

private static void testSubtract()

{

for (int i = 1; i

{

Fraction test_Fraction1 = new Fraction(-i);

Fraction test_Fraction2 = new Fraction(i);

Fraction difference = test_Fraction1.subtract(test_Fraction2);

checkContents(difference.toString(), new Integer (- 2 * i).toString());

}

//

// Add tests here

//

}

private static void testMultiply()

{

for (int i = 2; i

{

Fraction test_Fraction1 = new Fraction(i);

Fraction test_Fraction2 = new Fraction(1, i);

Fraction product = test_Fraction1.multiply(test_Fraction2);

checkContents(product.toString(), "1");

}

//

// Add tests here

//

}

private static void testDivide()

{

for (int i = 2; i

{

Fraction test_Fraction1 = new Fraction(i);

Fraction test_Fraction2 = new Fraction(1, i);

Fraction quotient = test_Fraction1.divide(test_Fraction2);

checkContents(quotient.toString(), new Integer (i * i).toString());

}

//

// Add tests here

//

}

}

-----

skeleton code for Fraction.java :

import java.lang.Math.*;

public class Fraction implements Comparable

{

public Fraction()

{

//TODO

}

public Fraction(long a)

{

//TODO

}

public Fraction(long a, long b) throws ArithmeticException

{

//TODO

}

public long getDenominator()

{

//TODO

}

public long getNumerator()

{

//TODO

}

public Fraction add(Fraction r)

{

//TODO

}

public Fraction subtract(Fraction r)

{

//TODO

}

public Fraction multiply(Fraction r)

{

//TODO

}

public Fraction divide(Fraction r)

{

//TODO

}

//

// Find the Greatest Common Divisor of Two Integers

//

public long gcd(long p, long q)

{

//TODO

}

public String toString()

{

if (_denom == 1) return "" + _numer;

return _numer + "/" + _denom;

}

public int compareTo(Object obj)

{

if (obj == null) return -1;

if (!(obj instanceof Fraction)) return 1;

if (this.equals((Fraction)(obj))) return 0;

return (this.subtract((Fraction)(obj)).getNumerator() > 0 ? 1 : -1);

}

// Returns true if and only if obj are the same number or reference the same object

public boolean equals(Object obj)

{

if (obj == null) return false;

if (obj == this) return true;

if (this.subtract((Fraction)obj).getNumerator() == 0) return true;

return false;

}

}

In this project, you will implement a class to represent a Fractional number. Mathematically (and for this assignment), a Fractional number is defined as a fraction in which the numerator and denominator are integers with the denominator being non-zero. Requirements To ensure consistency among solutions each implementation must implement the following requirements. Your implementation should reflect the definition of a simplified Fractional number at all times. A long (integer) is used to represent both the numerator and denominator. The Fraction class must be able to handle both positive and negative numbers The Fractional number must be in simplest form after every operation; that is, 8/6 shall be immediately reduced to 4/3. In order to simplify a Fractional number properly, use the Euclidean Algorithm to determine the greatest common divisor of the two numbers All methods that have an object as a parameter must be able to handle an input of null. By definition, the denominator ofa fraction cannot be zero since it leads to an undefined value. In Java, this division by zero results in an ArithmeticException; the Fractiorn class must throw an ArithmeticException when division by zero is attempted. The denominator should be initialized to a sensible and consistent value. The numerator shall be initialized to zero. * . . The denominator should always be positive leaving the numerator to indicate the sign of the number (positive or negative) The Fraction class shall reside in the default package

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!