This is an Object-Oriented Programming Assignment. It has a public class fraction with a bunch of methods in it. The cod

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

This is an Object-Oriented Programming Assignment. It has a public class fraction with a bunch of methods in it. The cod

Post by answerhappygod »

This is an Object-Oriented Programming Assignment. It has a
public class fraction with a bunch of methods in it. The code is
tested in the main class.
Can someone please add comments to the code explaining the code
*Don't just write a method can multiply or divide*
public class Fraction {
//Attributes
private int numerator, denominator;
//Constructor
public Fraction() {
numerator = 0;
denominator = 0;
}
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
//Accessors
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
public double getValue() {
double value = (double) numerator / denominator;
return value;
}
//Mutators
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
//Methods
//
public int gcd() {
int x, y, t;
x = numerator;
y = denominator;
while (y > 0) {
t = x % y;
x = y;
y = t;
}
return x;
}
public void reduce() {
int factor;
factor = gcd();
numerator = numerator / factor;
denominator = denominator / factor;
}
public static int getwholeNumber(int a, int b) {
int Numerator = 2;
int Denominator = 2;
a = (Numerator / Denominator);
b = Numerator % Denominator;
return getwholeNumber(a, b);
}
//add another fraction to this instance
public void add(Fraction other) {
this.numerator = this.numerator * other.denominator +
other.numerator * this.denominator;
this.denominator = this.denominator * other.denominator;
this.reduce();
}
//subtracts another fraction from this instance
public void subtract(Fraction other) {
this.numerator = this.numerator * other.denominator -
other.numerator * this.denominator;
this.denominator = this.denominator * other.denominator;
this.reduce();
}
// multiply
public Fraction multiply(Fraction other) {
Fraction result = new Fraction(numerator * other.numerator,
denominator * other.denominator);
result.reduce();
return result;
}
// divide
public Fraction divide(Fraction other) {
Fraction result = new Fraction(numerator * other.denominator,
denominator * other.numerator);
result.reduce();
return result;
}
public void reciprocal() {
// uses temp variable to find reciprocal of the fraction
int temp = numerator;
numerator = denominator;
denominator = temp;
}
public double value() {
return (double) numerator / denominator;
}
public String toString() {
return numerator + "/" + denominator;
}
public String toMixed() {
return numerator / denominator + " " + (numerator % denominator) +
"/" + denominator;
}
// method takes in another Fraction object
// method returns a boolean value as result
public boolean greaterThan(Fraction fraction) {
// return true if value of the current Fraction object is greater
than the value of the given fraction object
return value() > fraction.value();
}
public boolean lessThan(Fraction fraction) {
// return true if value of the current Fraction object is greater
than the value of the given fraction object
return value() < fraction.value();
}
}
class Main {
public static void main(String[] args) {
Fraction a = new Fraction(4, 3);
Fraction b = new Fraction(2, 4);
Fraction c = new Fraction(5, 4);
Fraction d = new Fraction(3, 6);
Fraction e = a.multiply(b);
Fraction f = a.divide(b);
System.out.println(a.toString());
System.out.println(b.toString());
b.reduce();
System.out.println(b.toString());
System.out.println(c.toMixed());
a.add(b);
System.out.println(+d.value());
System.out.println(a.toString());
d.reciprocal();
System.out.println(d.toString());
System.out.println(e);
System.out.println(f);
a.multiply(b);
// testing greaterThan method
System.out.println(a.greaterThan(b));
System.out.println(a.lessThan(b));
System.out.println(b.greaterThan(c));
System.out.println(b.lessThan(c));
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply