Not your question, but when one play with fractions, it is a common practice to reduce the fractions
10/8 => 5/4
17/12
3/2
15/9 => 5/3
the reduction is done by dividing both numbers with their GCD.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Some coding styles are easier to read than other, see your code:
public class Fraction {
private int numerator;
private int denominator;
public Fraction(int n, int d) {
numerator = n; denominator = d;
}
public Fraction add(Fraction f) {
return newFraction(numerator ∗ f.denominator + f.numerator ∗ denominator, denominator ∗ f.denominator);
}
public Fraction add(int x) {
return newFraction(numerator + x ∗ denominator, denominator);
}
public Fraction add(int n, int d) {
return newFraction(numerator ∗ d + n ∗ denominator, denominator ∗ d);
}
public String toString() { return numerator + "/" + denominator;
}
public static void main(String[] args) {
Fraction f1 = newFraction(1, 2);
Fraction f2 = newFraction(2, 3);
Fraction f3 = newFraction(3, 4);
Fraction[] sums = newFraction[4];
sums[0] = f1.add(f3);
sums[1] = f2.add(f3);
sums[2] = f1.add(1);
sums[3] = f2.add(3, 3);
for (int i = 0; i <= sums.length; i++) {
System.out.println(sums[i].toString());
}
}
}
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[
^]
ultraedit[
^]