Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions