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);
}
}
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.