Question
Hello, I am learning how to properly use the this keyword for Java and I needed some help with this fraction challenge. Here is the
Hello, I am learning how to properly use the this keyword for Java and I needed some help with this fraction challenge. Here is the task.
We are going to use this static class as we build out a newFractionclass. Your job is to complete theFractionclass based on the structure that is set up. To do this, you will need to use thethisnotation in your constructor and setters. You will also need to use it to pass the object to theFractionMathclass.
The tester is complete and can be used to test your objects. Feel free to make changes in this class to test other things.
Here is my code so far: Thanks
public class Fraction { private int numerator; private int denominator; public Fraction(int numerator, int denominator){ this.numerator = numerator; this.denominator = denominator; } /** * Returns value of numerator * @return numerator */ public int getNumerator() { return this.numerator; } /** * Sets new value of numerator * @param numerator new numerator value */ public void setNumerator(int numerator) { this.numerator = numerator; } /** * Returns value of denominator * @return denominator */ public int getDenominator() { return this.denominator; } /** * Sets new value of denominator * @param denominator new denominator */ public void setDenominator(int denominator) { this.denominator = denominator; } /** * Updates this fraction by adding another fraction * @param other Fraction to add to existing fraction */ //Calculate by using the FractionMath class, then update //the numerator and denominator from the returned Fraction public void addFraction(Fraction other){ } /** * Updates this fraction by multiplying another fraction * @param other Fraction to multiple to existing fraction */ //Calculate by using the FractionMath class, then update //the numerator and denominator from the returned Fraction public void multiplyFraction(Fraction other){ this.Fraction.add() } /** * Prints fraction as numerator / denominator * Example: 1 / 2 */ public String toString(){ } } public class FractionMath { /* * This is a static class that the Fraction class will use. * No updates are needed. */ public static Fraction add(Fraction frac1, Fraction frac2){ int numerator = frac1.getNumerator() * frac2.getDenominator() + frac2.getNumerator() * frac1.getDenominator(); int denominator = frac1.getDenominator() * frac2.getDenominator(); Fraction solution = new Fraction(numerator, denominator); return solution; } public static Fraction multiply(Fraction frac1, Fraction frac2){ int numerator = frac1.getNumerator() * frac2.getNumerator(); int denominator = frac1.getDenominator() * frac2.getDenominator(); Fraction solution = new Fraction(numerator, denominator); return solution; } }
How do I properly implement the use of "this " key word in Java in my Fraction class ?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started