Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Main program File : FractionMath.java public class FractionMath { public static

ID: 3864399 • Letter: M

Question

Main program File : FractionMath.java


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);

}
}

Main program Fi FractionMathiava Very similar to assignment #19, for this assignment l want you to write a Fraction class that works with the FractionMath program above. You can download the above file, and then it should use your Fraction java file to run. Remember that fractions cannot have zero in the denominator, so if you ever have to change the denominator to zero, make it one instead. Your Craction class should have the following Fields o a numerator (int) o a denominator (int) Methods o A default constructor that sets the numerator to 1 that sets the denominator to 1 o A full constructor That accepts both the numerator and denominator. Note that if the denominator is zero, you should change it to a 1. o A toString0 method That returns a string in the format {space numerator denominator tspace 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. o get Numerator0 o get Denominator0 o These should just return the values with no changes. o setNumerator0 This should change the numerator setDenominator0 o This should change the denominator, as long as it isn't zero. If it is zero, then don't change anything, make no changes. o getDecimalValue() This should return the decimal value of the fraction 3/4 would return 0.75 o 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.

Explanation / Answer

Fraction.java

public class Fraction {

   // Declaring instance Variables.
   private int numerator;
   private int denominator;

   // Default Constructor
   public Fraction() {
       super();
       this.numerator = 1;
       this.denominator = 1;
   }

   // Parameterized constructor
   public Fraction(int n, int d) {
       super();
       this.numerator = n;
       if(d==0)
       this.denominator = 1;
       else
       this.denominator = d;
   }

   //toString() method is used to display the contents of an object inside it
   @Override
   public String toString() {
       String str=" ";
       if(denominator==1)
       return str+numerator;
       else
       return str+numerator + "/"+ denominator ;
   }

   //Setters and getters
   public int getNumerator() {
       return numerator;
   }

   public void setNumerator(int numerator) {
       this.numerator = numerator;
   }

   public int getDenominator() {
       return denominator;
   }

   public void setDenominator(int denominator) {
       if(denominator!=0)
       this.denominator = denominator;
   }
  
   //This method is used to calculate the Decimal value of the fraction
   public double getDecimalValue()
   {
       return (double)numerator/denominator;
   }
  
   //This method will check whether the two fractions are equal or not
   public boolean equals(Fraction other)
   {
       if(this.getDecimalValue()==other.getDecimalValue())
           return true;
       else
           return false;
   }
  
   //this method is used to multiply one fraction with another
   public void multiply(Fraction other)
   {
       this.numerator=this.numerator*other.numerator;
       this.denominator=this.denominator*other.denominator;
   }

  
}

_________________

FractionMath.java
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);

}
}

_______________________

output:

*** Does the toStringMethod work?
First fraction : 1
Second fraction : 3/4
Third fraction : 2/5
Forth fraction : 6

*** Does the get() methods work?
w = 1 over 1

*** Does the set() methods work?
w fraction Before : 1
w fraction After : 22
w fraction After : 22/55
w fraction After : 22/55
The above line should be no different
Is 22/55 eqaul to 3/4?
false
Is 22/55 eqaul to 2/5?
true

3/4 = 0.75
2/5 = 0.40
6 = 6.00

x fraction Before : 3/4
y fraction Before : 2/5
x fraction After : 6/20
y fraction After : 2/5
*** Test #2 ***
y fraction Before : 2/5
z fraction Before : 6
y fraction After : 12/5
z fraction After : 6

_____________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote