Question: Coding Language: JAVA This is what I have so far: public class Fraction { private int numerator; private int denominator; //Default constructor that creates the

Coding Language: JAVA This is what I have so far: public classFraction { private int numerator; private int denominator; //Default constructor that createsthe fraction: 1/1 public Fraction() { this.numerator = 1; this.denominator = 1;Coding Language: JAVA

This is what I have so far:

public class Fraction { private int numerator; private int denominator; //Default constructor that creates the fraction: 1/1 public Fraction() { this.numerator = 1; this.denominator = 1; } //Sets the numerator to n if it is positive public void setNumerator(int n) { if (n > 0) { this.numerator = n; } } //Sets the denominator to d if it is positive public void setDenominator(int d) { if (d > 0) { this.denominator = d; } } //Setting numerator to n and setting denominator to d only if conditions are met public Fraction(int n, int d) { if (n > 0) { this.numerator = n; } else { this.numerator = 1; } if (d > 0) { this.denominator = d; } else { this.denominator = 1; } } //Returns the numerator of the fraction public int getNumerator() { return numerator; } //Returns the denominator of the fraction public int getDenominator() { return denominator; } //Returns the fraction as a String public String toString() { return numerator + "/" + denominator; } //Returns improper fractions as mixed numbers public String mixedNumber() { int x; int y; x = numerator % denominator; y = numerator / denominator; if (x != 0 && y != 0) { return y + " " + x + "/" + denominator; } else if (x == 0) { return y + ""; } else { return numerator + "/" + denominator; } } //If both n and d are positive, then n/d will be added to the fraction public void add(int n, int d) { if (n > 0 && d > 0) { this.numerator = (this.numerator * n) + (this.denominator * n); this.denominator = this.denominator * d; } } public void add(Fraction other) { } public void multiply(int n, int d) { if (n > 0 && d > 0) { } } public void multiply(Fraction other) { } }

----------------------------------------------------------------------------------------------------

I am not sure how to do the public void add and multiply and I want to make sure the rest of the code is correct.

Please help! Thank you

For this assignment, you will create a class which must be named Fraction to store fractions. The class will hold two integer values: a numerator and a denominator. For your class, we will stick to positive fractions, meaning the numerator and the denominator must both be greater than 0. Your Fraction class will also keep track of the number of Fraction objects created by a program. You will need to create appropriate variables to store the information in these fields, all of which should be private. You will also need to add all of the public constructors and methods which are described below. You may choose to add other methods to help you implement these. Constructors Fraction() : default constructor which creates a fraction 1/1 Fraction(int n, int d) : If n is positive, set numerator to n. Otherwise, set numerator to 1. If a is positive, set denominator to d Otherwise, set denominator to 1. Accessors int get Numerator() : Returns the numerator of the fraction. int getDenominator(): Returns the denominator of the fraction. static int getNumFractions(): Returns the number of fractions created in a program so far. String toString(): Returns the fraction as a string in the format "numerator/denominator". For example 1/2 or 5/3. String mixedNumber(): Returns any improper (top-heavy) fraction as a mixed number, for example, 23/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. Mutators void setNumerator(int n) : Sets the numerator of the fraction to n if it is positive. void setDenominator(int d) : Sets the denominator of the fraction to d if it is positive. void add(int n, int d) : If n and d are both positive, add the fraction n/d to this fraction. Otherwise, leave the fraction unchanged. In general the sum of the fractions a/b and c/d is(a*d + c*b)/(b*d). void add(Fraction other) : Add the fraction represented by other to this fraction. In general the sum of the fractions a/b and c/d is(a'd +c+b)/(b*d). Postcondition: the other Fraction is left unchanged. void multiply(int n, int d)): If n and d are both positive, multiply the fraction n/d by this fraction. Otherwise, leave the fraction unchanged. Unsimplified is ok in this case, the formula for multiplying fractions a/b and c/d is (a*c)/(bd). void multiply(Fraction other): Set this fraction to the product of this fraction and other . Unsimplified is ok in this case, the formula for multiplying fractions a/b and c/d is (a* c)/(b*d). Postcondition: the other Fraction is left unchanged. To test your code, you will need to run the runner_Fraction class main method. Don't add a main method to your Fraction class as your code will not be checked or scored correctly. This runner class is a little more advanced than some of the others you have seen so far. The runner first creates an initial fraction by using the default constructor which you write in the Fraction class. You then have the option to test each method individually by typing the name of the method, or to create a new Fraction using the other Fraction constructor. When a method requires parameters you'll be asked to input these, and if there is an overloaded method you'll be asked to specify which method signature you want to use by typing 1 or 2. As the runner code has calls to all the required public Fraction methods, it won't compile or run until it can see headers for all these methods. So it's a good idea to add headers for each method you have to write in the Fraction class when you first start. You can leave the method bodies blank until you are ready to write them, or if a return is required add a dummy return like return 0; or return ""; This way you'll be able to test the methods you write as you go along. Milestones As you work on this assignment, you can use the milestones below to inform your development process. Milestone 1: Write the method and constructor headers for all the public methods/constructors described, and add dummy return statements (e.g. return ""; or return 0;) to any non-void methods. This will allow you to compile your runner class and test methods as you go along. Create private variables to store the class information (numerator, denominator and number of fractions). Milestone 2: Write the setNumerator, setDenominator methods and set up the two class constructors to initialize the numerator and denominator of each new Fraction. Write the accessor methods getNumerator, getDenominator and toString methods and test all these methods using the runner class. Milestone 3: Add the mixedNumber method to the class. Add code to the constructors to keep track of the number of fractions, then implement the static getNumFractions method. Test your code using the runner. Milestone 4: Write the mutator method add(int n, int d) and then overload this by writing the add(Fraction other) method. Repeat this process for both of the multiply methods. Test all methods thoroughly using the runner class

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!