Write a Fraction class that works with the FractionMath program above. It should use your Fraction.java file to run. Main Program: public class FractionMath { public static void main(String[] arguments) { // create four fractions accounts Fraction w = new Fraction(); Fraction x = new Fraction(3,4); Fraction y = new Fraction(2,5); Fraction z = new Fraction(6,0); System.out.println("*** Does the toStringMethod work?"); System.out.println("First fraction : " + w); System.out.println("Second fraction : " + x); System.out.println("Third fraction : " + y); System.out.println("Forth fraction : " + z); System.out.println(); // Does the get() method work? System.out.println("*** Does the get() methods work?"); System.out.println("w = " + w.getNumerator() + " over " + w.getDenominator()); System.out.println(); // Can we change the values correctly name System.out.println("*** Does the set() methods work?"); System.out.println("w fraction Before : " + w); w.setNumerator(22); System.out.println("w fraction After : " + w); w.setDenominator(55); System.out.println("w fraction After : " + w); w.setDenominator(0); System.out.println("w fraction After : " + w); System.out.println("The above line should be no different"); // Does the equality work System.out.println("Is " +w+ " eqaul to " +x+ "?"); System.out.println(" " + w.equals(x) ); System.out.println("Is " +w+ " eqaul to " +y+ "?"); System.out.println(" " + w.equals(y) ); System.out.println(); // Does the getDecimal work System.out.printf(x + " = %4.2f %n", x.getDecimalValue()); System.out.printf(y + " = %4.2f %n", y.getDecimalValue()); System.out.printf(z + " = %4.2f %n", z.getDecimalValue()); System.out.println(); // Does the multiply work System.out.println("x fraction Before : " + x); System.out.println("y fraction Before : " + y); x.multiply(y); System.out.println("x fraction After : " + x); System.out.println("y fraction After : " + y); System.out.println("*** Test #2 ***"); System.out.println("y fraction Before : " + y); System.out.println("z fraction Before : " + z); y.multiply(z); System.out.println("y fraction After : " + y); System.out.println("z fraction After : " + z); } } Your Fraction class should have the following Fields a numerator  (int) a denominator (int) Methods A default constructor that sets the numerator  to 1 that sets the denominator to 1 A full constructor That accepts both the numerator and denominator. Note that if the denominator is zero, you should change it to a 1. A toString() method That returns a string in the format  {space} numerator / denominator {space}  2/5  however,  if the denominator is 1, then the fraction is a whole number so you should just return the numerator. i.e.  do not print 4/1  just print 4. getNumerator() getDenominator() These should just return the values with no changes. setNumerator() This should change the numerator .setDenominator() This should change the denominator, as long as it isn't zero.   If the new value is zero, then don't change anything, make no changes and leave it where it was. getDecimalValue() This should return the decimal value of the fraction 3/4 would return 0.75 equals (Fraction other) This method should return a boolean type.  This method should compare the decimal value of this fraction with the decimal value of the "other" point and see if they are equal or not.  If equal return true, else return false; multiply(Fraction other) This method should take the current fraction and multiple it by the "other" fraction and change the value to the new fraction. So if x = 2/5 and y  = 3/4 then x.multiply(y) would change x to 6/20 but leave y as 3/4 Remember that fractions cannot have zero in the denominator, so if you ever have to change the denominator to zero, make it one instead.

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Modularization Techniques
Section: Chapter Questions
Problem 13RQ
icon
Related questions
Question

Write a Fraction class that works with the FractionMath program above. It should use your Fraction.java file to run.
Main Program:
public class FractionMath
{
public static void main(String[] arguments)
{
// create four fractions accounts
Fraction w = new Fraction();
Fraction x = new Fraction(3,4);
Fraction y = new Fraction(2,5);
Fraction z = new Fraction(6,0);
System.out.println("*** Does the toStringMethod work?");
System.out.println("First fraction : " + w);
System.out.println("Second fraction : " + x);
System.out.println("Third fraction : " + y);
System.out.println("Forth fraction : " + z);
System.out.println();
// Does the get() method work?
System.out.println("*** Does the get() methods work?");
System.out.println("w = " + w.getNumerator() + " over " + w.getDenominator());
System.out.println();
// Can we change the values correctly name
System.out.println("*** Does the set() methods work?");
System.out.println("w fraction Before : " + w);
w.setNumerator(22);
System.out.println("w fraction After : " + w);
w.setDenominator(55);
System.out.println("w fraction After : " + w);
w.setDenominator(0);
System.out.println("w fraction After : " + w);
System.out.println("The above line should be no different");
// Does the equality work
System.out.println("Is " +w+ " eqaul to " +x+ "?");
System.out.println(" " + w.equals(x) );
System.out.println("Is " +w+ " eqaul to " +y+ "?");
System.out.println(" " + w.equals(y) );
System.out.println();
// Does the getDecimal work
System.out.printf(x + " = %4.2f %n", x.getDecimalValue());
System.out.printf(y + " = %4.2f %n", y.getDecimalValue());
System.out.printf(z + " = %4.2f %n", z.getDecimalValue());
System.out.println();
// Does the multiply work
System.out.println("x fraction Before : " + x);
System.out.println("y fraction Before : " + y);



x.multiply(y);
System.out.println("x fraction After : " + x);
System.out.println("y fraction After : " + y);
System.out.println("*** Test #2 ***");
System.out.println("y fraction Before : " + y);
System.out.println("z fraction Before : " + z);
y.multiply(z);
System.out.println("y fraction After : " + y);
System.out.println("z fraction After : " + z);
}
}

Your Fraction class should have the following

  • Fields
    • a numerator  (int)
    • a denominator (int)
  • Methods
    • A default constructor
      • that sets the numerator  to 1
      • that sets the denominator to 1
    • A full constructor
      • That accepts both the numerator and denominator.
      • Note that if the denominator is zero, you should change it to a 1.
    • A toString() method
      • That returns a string in the format
      •  {space} numerator / denominator {space}
      •  2/5 
      • however,  if the denominator is 1, then the fraction is a whole number so you should just return the numerator.
      • i.e.  do not print 4/1  just print 4.
    • getNumerator()
    • getDenominator()
      • These should just return the values with no changes.
    • setNumerator()
      • This should change the numerator
    • .setDenominator()
      • This should change the denominator, as long as it isn't zero.  
      • If the new value is zero, then don't change anything, make no changes and leave it where it was.
    • getDecimalValue()
      • This should return the decimal value of the fraction
      • 3/4 would return 0.75
    • equals (Fraction other)
      • This method should return a boolean type. 
      • This method should compare the decimal value of this fraction with the decimal value of the "other" point and see if they are equal or not. 
      • If equal return true, else return false;
    • multiply(Fraction other)
      • This method should take the current fraction and multiple it by the "other" fraction and change the value to the new fraction.
      • So if x = 2/5 and y  = 3/4
      • then x.multiply(y)
        • would change x to 6/20
        • but leave y as 3/4


Remember that fractions cannot have zero in the denominator, so if you ever have to change the denominator to zero, make it one instead.




Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage