Question

Assignment 4, Fraction Comparable Instructions For this assignment, you will be updating the Fraction class from...

Assignment 4, Fraction Comparable

Instructions

For this assignment, you will be updating the Fraction class from Assignment 1. To get started, you can either make a copy of your Assignment 1 Fraction.java or download the Fraction.java template at the bottom of this assignment.

You will need to update Fraction so that it implements the Comparable interface. This will require adding an implements statement to the class declaration as well as the compareTo method. You will also add a simplify method which reduces a fraction to its lowest terms and, to help you with this, a static method gcd which finds the greatest common divisor of two integers.

The requirements of the methods to be added are as follows:

  • int compareTo(Object other): Return -1 if this fraction is smaller than the fraction other, return 0 if this fraction is equal to the fraction other, and return 1 if this fraction is greater than the fraction other. Hint: To compare the two fractions, it will help to first convert them to fractions with a common (equal) denominator.
  • static int gcd(int a, int b): return the greatest common divisor of a and b. To find the gcd of two numbers there are several possible algorithms. One of these was showcased in term 1 lesson 19 - More Loops.
  • void simplify(): reduce the fraction to its simplest possible form: e.g. the fraction 12/18 should be reduced to 2/3. The static method gcd provides a useful helper for this method.

When you have successfully written your simplify method, you should add calls to this method at the end of the Fraction(int n, int d) constructor and the add method. This will ensure that fractions are always stored in their simplest possible form.

To test your code, download the runner class student_fraction_runner.java (Links to an external site.) into the same folder that holds your Fraction.java. Execute the method student_fraction_runner.main, and verify that the output matches the sample run listed below. Remember to change the runner to test with other values to make sure your program fits all the requirements. We will use a similar but different runner to grade the program.

When you are ready, paste your entire Fraction.java class in the box below, click run to test the output, and click submit when you are satisfied with your results.

Note: once you have completely finished the assignment feel free to add more methods (e.g. multiply) or extend the functionality of your Fraction class to include, for example, negative fractions. Make sure you submit your assignment first before making any changes like this.

Sample Run

Fraction 1: 4/5
Fraction 2: 3/2
Fraction 3: 4/5

Compare fraction 1 to fraction 2: -1
Compare fraction 2 to fraction 1: 1

Compare fraction 1 to fraction 3: 0
Compare fraction 3 to fraction 1: 0

Compare fraction 2 to fraction 3: 1
Compare fraction 3 to fraction 2: -1

Milestones

As you work on this assignment, you can use the milestones below to inform your development process:

Milestone 1: Write the static gcd method and test this by calling it on pairs of integers from another class.

Milestone 2: Write the simplify method (you will probably find it useful to call the gcd method). Add calls to this method at the end of the constructor and the add method.

Milestone 3: Implement the Comparable interface and write the method compareTo. Download the runner file and ensure the results are as expected.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Files:

Fraction.java

/* A class which is used to represent fractions*/
public class Fraction implements java.lang.Comparable<Object> {
public int numerator;
public int denominator;

public static final boolean ASSIGNMENT_4 = true;

/**
* default constructor which creates a fraction 1/1
*/
public Fraction()
{
this.numerator = 1;
this.denominator = 1;
}

/**
* If n is positive, set numerator to n. Otherwise, set numerator to 1. If d is
* positive, set denominator to d. Otherwise, set denominator to 1.
*
* @param n numerator
* @param d denominator
*/
public Fraction(int n, int d)
{
this.numerator = n > 0 ? n : 1;
this.denominator = d > 0 ? d : 1;

if (ASSIGNMENT_4)
simplify();
}

/**
* Returns the fraction as a string in the format "numerator/denominator". For
* example 1/2 or 5/3.
*
* @return String in the format numerator/denominator
*/
@Override
public String toString()
{
return String.format("%d/%d", this.numerator, this.denominator);
}

/**
* Returns any improper (top-heavy) fraction as a mixed number, for example, 2
* 3/5. If the numerator of the fraction part is 0, return only the integer part
* of the mixed number. If the fraction is proper, return only the fraction
* part.
*
* @return fraction as mixed number
*/
public String mixedNumber()
{
int fraction = (int) (this.numerator / this.denominator), numerator = this.numerator % this.denominator;
return String
.format("%s %s", fraction == 0 ? "" : " " + String.valueOf(fraction), numerator == 0 ? "" : String.valueOf(numerator) + "/" + String.valueOf(this.denominator));
}

/**
* If n and d are both positive, add the fraction n/d to this fraction.
* Otherwise, leave the fractions unchanged. In general the sum of the fractions
* a/b and c/d is {@code (a*d + c*b)/(b*d)}
*
* @param n numerator
* @param d denominator
*/
public void add(int n, int d)
{
if (n < 1 || d < 1)
return;

this.numerator = this.numerator * d + n * this.denominator;
this.denominator *= d;

if (ASSIGNMENT_4)
simplify();
}

/*
* Assignment 4 parts
*/

/**
* return the greatest common divisor of a and b. To find the gcd of two numbers
* there are several possible algorithms. One of these was showcased in term 1
* lesson 19 - More Loops.
*
* @param a
* @param b
* @return greatest common divisor
*/
static int gcd(int a, int b)
{
while (b != 0)
{
int r = a % b;
a = b;
b = r;
}
return a;
}

/**
* void simplify(): reduce the fraction to its simplest possible form: e.g. the
* fraction 12/18 should be reduced to 2/3. The static method gcd provides a
* useful helper for this method.
*/
void simplify()
{
int gcd = gcd(numerator, denominator);
numerator = numerator / gcd;
denominator = denominator / gcd;
}

/**
* Return -1 if this fraction is smaller than the fraction other, return 0 if
* this fraction is equal to the fraction other, and return 1 if this fraction
* is greater than the fraction other. Hint: To compare the two fractions, it
* will help to first convert them to fractions with a common (equal)
* denominator.
*
* @return -1|0|1
*/
@Override
public int compareTo(Object other)
{
Fraction target;

try
{
target = (Fraction) other;
} catch (ClassCastException e)
{
return -2;
}

// The least common multiple (lcm) of a and b is their product
// divided by their greatest common divisor (gcd) ( i.e. lcm(a, b) =
// ab/gcd(a,b)).
int lcm = this.denominator / gcd(this.denominator, target.denominator) * target.denominator;

int iFirstNum = this.numerator * (lcm / this.denominator);
int iSecondNum = target.numerator * (lcm / target.denominator);

return iFirstNum == iSecondNum ? 0 : iFirstNum > iSecondNum ? 1 : -1;

}
}

FractionTest.java

import java.io.IOException;

public class FractionTest{

public static void main(String str[]) throws IOException
{
if (test_1(str))
test_2(str);
}

public static boolean test_1(String str[]) throws IOException
{
Fraction fraction1 = new Fraction(7, 4);
System.out.println("Fraction 1: " + fraction1);
System.out.println("As a mixed number: " + fraction1.mixedNumber());
System.out.println();

Fraction fraction2 = new Fraction();
System.out.println("Fraction 2: " + fraction2);
System.out.println("As a mixed number: " + fraction2.mixedNumber());
System.out.println();

Fraction fraction3 = new Fraction(2, 7);
System.out.println("Fraction 3: " + fraction3);
System.out.println("As a mixed number: " + fraction3.mixedNumber());
fraction3.add(2, 5);
System.out.println("Add 2/5: " + fraction3);
System.out.println();

Fraction fraction4 = new Fraction(24, 6);
System.out.println("Fraction 4: " + fraction4);
System.out.println("As a mixed number: " + fraction4.mixedNumber());
fraction4.add(1, -2);
System.out.println("Attempt to add 1/-2: " + fraction4);
System.out.println();

Fraction fraction5 = new Fraction(5, 2);
System.out.println("Fraction 5: " + fraction5);
System.out.println("As a mixed number: " + fraction5.mixedNumber());
fraction5.add(2, 3);
System.out.println("Add 2/3: " + fraction5);
System.out.println("As a mixed number: " + fraction5.mixedNumber());
System.out.println();

return Fraction.ASSIGNMENT_4;
}

public static void test_2(String str[]) throws IOException
{

Fraction fraction1 = new Fraction(8, 10);
System.out.println("Fraction 1: " + fraction1);

Fraction fraction2 = new Fraction(12, 8);
System.out.println("Fraction 2: " + fraction2);

Fraction fraction3 = new Fraction(4, 5);
System.out.println("Fraction 3: " + fraction3);
System.out.println();

System.out.println("Compare fraction 1 to fraction 2: " + fraction1.compareTo(fraction2));
System.out.println("Compare fraction 2 to fraction 1: " + fraction2.compareTo(fraction1));
System.out.println();

System.out.println("Compare fraction 1 to fraction 3: " + fraction1.compareTo(fraction3));
System.out.println("Compare fraction 3 to fraction 1: " + fraction3.compareTo(fraction1));
System.out.println();

System.out.println("Compare fraction 2 to fraction 3: " + fraction2.compareTo(fraction3));
System.out.println("Compare fraction 3 to fraction 2: " + fraction3.compareTo(fraction2));
}
}

output:

Fraction 1: 7/4
As a mixed number: 1 3/4

Fraction 2: 1/1
As a mixed number: 1

Fraction 3: 2/7
As a mixed number: 2/7
Add 2/5: 24/35

Fraction 4: 4/1
As a mixed number: 4
Attempt to add 1/-2: 4/1

Fraction 5: 5/2
As a mixed number: 2 1/2
Add 2/3: 19/6
As a mixed number: 3 1/6

Fraction 1: 4/5
Fraction 2: 3/2
Fraction 3: 4/5

Compare fraction 1 to fraction 2: -1
Compare fraction 2 to fraction 1: 1

Compare fraction 1 to fraction 3: 0
Compare fraction 3 to fraction 1: 0

Compare fraction 2 to fraction 3: 1
Compare fraction 3 to fraction 2: -1

Hope this helps!

Please let me know if any changes needed

Add a comment
Know the answer?
Add Answer to:
Assignment 4, Fraction Comparable Instructions For this assignment, you will be updating the Fraction class from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT