Reputation: 15
This program is supposed to create a fraction when an object is created in the main method and use other methods to add different objects. I am using a class that contains the methods for adding and multiplying the fractions. However, in the class where I have the constructor and the accessors and mutators, I also have another two methods which update the values of numerator
and denominator
using the methods from the previously mentioned class. How do I access the variables from said class?
This is the class with the constructor and where I am trying to import the variables:
public class Fraction {
private int numerator;
private int denominator;
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
// Getters and setters left out for brevity
// 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) {
}
public String toString() {
return numerator + " / " + denominator;
}
}
This is the class with the methods add and multiply:
public class FractionMath {
public static Fraction add(Fraction frac1, Fraction frac2) {
int numerator = frac1.getNumerator() * frac2.getDenominator() +
frac2.getNumerator() * frac1.getDenominator();
int denominator = frac1.getDenominator() * frac2.getDenominator();
return new Fraction(numerator, denominator);
}
public static Fraction multiply(Fraction frac1, Fraction frac2) {
int numerator = frac1.getNumerator() * frac2.getNumerator();
int denominator = frac1.getDenominator() * frac2.getDenominator();
return new Fraction(numerator, denominator);
}
}
Upvotes: 0
Views: 288
Reputation: 3915
Some terminology issues here: There are no static variables in your class. There are static methods.
A static variable would be public static int someNumber = 0;
It is not a static class (Such a thing doesn't really exist in Java), but a class with static methods. There are static inner classes, but they aren't really static in the way you'd have static variables or methods.
To call a static method, you'd just use the class name and the method name, e.g.
Fraction result = FractionMath.add(frac1, frac2);
Upvotes: 1