over here I have some code that is supposed to make a constructor and some getters for my Fraction class. I'm wondering, is it possible to get both parameters in one getter? Because I'm only supposed to use only one method into obtaining both of my results.
Such as "1/2 and 3/5" for example.
import java.lang.reflect.*; import java.lang.annotation.*; import java.util.*; import java.time.*; // Please do not change or remove this line.
class Fraction {
private Integer nominator;
private Integer denominator;
public Fraction(Integer nominator, Integer denominator){
this.nominator = nominator;
this.denominator = denominator;
}
public Integer getNominator() {
return nominator;
}
public Integer getDenominator() {
return denominator;
}
}
class Main {
public static Fraction createFraction(Integer nominator, Integer denominator) {
return new Fraction(nominator, denominator);
}
public static void main(String[] arguments) {
final Fraction HALF = createFraction(1, 2);
final Fraction THREE_FIFTH = createFraction(3, 5);
System.out.println((HALF));
System.out.println((THREE_FIFTH));
}
public static String toString(Fraction fraction) {
return fraction.getDenominator() + "/" + fraction.getNominator();
}
}
toString()fromMaintoFractionand then you have what you need. Edit: also, the term is numerator, not nominator.nominatormakes no sense. Variable names should represent the purpose of the variable, butnominatoris not something that happens in fractions. You might have intendednumerator.