0

I thought I knew what I was doing but I obviously don't. I have to make a file that takes numbers that the user inputs and converts them into fractions. I have most of the work done. I have constructors and return methods that create fractions and change some numbers into fractions of the correct value (I also had to simplify the fraction and make the denominator a positive instead of a negative). What I am having a real problem with is that we have to use specific methods, one of them being "public static Fraction readFraction();" I don't understand what I am doing wrong but everything I do seems to cause a different error. (The last bunch of code is there to test and make sure each method was in working order) This is what I have done so far:

import java.util.Scanner;

public class Fraction {
  private int _num;
  private int _denom;

  public String toString() {return _num + "/" + _denom;}

  public boolean equals(Fraction a) {
    return _num == a._num && _denom == a._denom;
  }

  public Fraction(){ _num = 0; _denom = 1;}

  public static int gcd(int a, int b) {
    int r = a % b;
    while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    }
    if(b < 0) {b = b * -1; }
    return b;
  }

  public Fraction(int n, int d) {
    int r = Math.abs(gcd(n, d));
    if (d < 0) {
      n = -n;
      d = -d;
    }
    _num = n / r;
    _denom = d / r;
  }

  public Fraction(Fraction a) {
    _num = a._num;
    _denom = a._denom;
  }

  public Fraction add(Fraction a) {
    return new Fraction(_num * a._denom + _denom * a._num, _denom * a._denom);
  }

  public static void main(String[] args) {
    Fraction f = new Fraction();
    System.out.println("Fraction f = " + f.toString());
    System.out.println(gcd(6, 22));
    Fraction g = new Fraction(4, 14);
    System.out.println("Fraction g = " + g);
    Fraction h = new Fraction(g);
    System.out.println("Fraction h = " + h);
    System.out.println("g == h is " + (g == h));
    System.out.println("g.equals(h) is " + g.equals(h));
    Fraction i = f.add(g);
    System.out.println("f.add(g) = " + i);
  }
}

My question is how to get a user to input his or her own numbers to create a fraction using "public static Fraction readFraction();"
I thought it went something like this:

public static Fraction readFraction() {
int ?;
int ?;
return ? + "/" + ?
}

What am I doing wrong?

I am going to continue toying with what I have so I may come back and edit some of my post

1
Contributor
1
Reply
2
Views
9 Years
Discussion Span
Last Post by edchabz
0

AH HA! I wasn't paying attention to the word static. :) What a silly mistake. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.