0

I am trying to add, multiply and divide fractions in Java. The Output I am getting is : FRACTIONS@3d4eac69. Any ideas?

public class FRACTIONS {
private int numer, denom;
    public FRACTIONS (){
    numer=1;
    denom=1;
}
public FRACTIONS (int n, int d){
    numer=n;
    denom=d;
}
public int getNumerator(){
    return numer;
}
public int getDenominator (){
    return denom;
}
public FRACTIONS add(FRACTIONS other){
    int n = numer * other.denom + other.numer * denom;
    int d = denom * other.denom;
    return new FRACTIONS (n, d);
}

public FRACTIONS sub(FRACTIONS other){
int n = numer * other.denom + other.numer * denom;
int d = denom * other.denom;
return new FRACTIONS (n, d);
}

public FRACTIONS mult(FRACTIONS other){
int n = numer * other.numer;
int d = denom * other.denom;
return new FRACTIONS (n, d);
}

public FRACTIONS div(FRACTIONS other){
int n = numer * other.denom;
int d = denom * other.numer;
return new FRACTIONS (n, d);
}

public String toString(){
String str;
str= n + " / " + d;
return str;
}

}

Tester Program:

import java.util.Scanner;
public class FRACTIONS_TESTER {

public FRACTIONS_TESTER() {
}

public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    Scanner scan = new Scanner (System.in);
    FRACTIONS numer, denom;
    numer= new FRACTIONS();
    denom = new FRACTIONS ();
    System.out.print ("Enter the numerator for fraction 1: ");
    int n1 = reader.nextInt();
    System.out.print ("Enter the denominator for fraction 1: ");
    int d1 = reader.nextInt();
    System.out.print ("Enter the numerator for fraction 2: ");
    int n2 = reader.nextInt();
    System.out.print ("Enter the denominator for fraction 2: ");
    int d2 = reader.nextInt();
int n = 5;
int d = 6;
    FRACTIONS f1=new FRACTIONS (n1, d1);
    FRACTIONS f2=new FRACTIONS (n2, d2);
    FRACTIONS f3=new FRACTIONS (n,d);


    int opt;
    System.out.println ("Select the corresponding number for the desired 
 operation: ");
    System.out.println (" 1.  Addition \n 2.  Subtraction \n 3. 
 Multiply \n 4.  Divison");
    opt=scan.nextInt();
    if (opt==1){
        f3=f1.add (f2);

    }
    if (opt==2){
        f3=f1.sub (f2);

    }
    if (opt==3) {
        f3=f1.mult (f2);

    }
    if (opt==4){
        f3=f1.div (f2);

    }
    }
 System.out.println (f3);
}

Thanks in advance. I just want the output to be a string in that is returned in the form of "/"

John DeMarco
  • 5
  • 1
  • 3
  • Did you recompile your FRACTIONS class after adding the toString method? – Kenney Nov 07 '15 at 18:10
  • You need the toString method in the answer below, but I am commenting to say you should follow Java naming conventions with CamelCase class names, not ALL_CAPS – OneCricketeer Nov 07 '15 at 18:27
  • This code doesn't compile. Please don't ask people to examine code that is different from what you're actually using. – Matt Timmermans Nov 07 '15 at 19:10
  • This is the code that I am using. I modified the toString like the one below, and still am getting an error. I am not sure how to print out the data from the toString... – John DeMarco Nov 07 '15 at 19:11

1 Answers1

0

n and d are undefined in the scope of toString. Perhaps you meant

@Override
public String toString() {
    return numer + " / " + denom;
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thanks. I added this and am still getting the FRACTIONS@3d4eac69 as my output, instead of the solution of the fractions in the form of numerator / denominator. – John DeMarco Nov 08 '15 at 16:50