-1

I am trying to store Fraction objects in an Stack of type Number and then retrieve them to perform arithmetic calculations on them, however, the objects are being converted to type Number when I put them in the stack and I cannot convert them back to type Fraction. The compilation error occurs at line 19 of the source code below. How do I fix this?

Source code:

import java.util.Scanner;
import java.util.Stack;

public class Test{
    public static void main(String[] args){

        String line = "3/4";
        Scanner input = new Scanner(line);

        Stack<Number> numbers = new Stack<>();

        while (input.hasNext()){
            if (input.hasNext("/")){
                numbers.push(new Fraction(input.next()));
            }
        }

        if (numbers.peek() instanceof Fraction){
            Fraction rhs = numbers.pop();
            System.out.println(rhs.getFraction());
        }


    }
}

The Fraction class exends Number because I need to be able to store Integers, Doubles, Fractions, and Complex numbers to support inter-type mathematical operations. Note that this is not the entire Fraction class, but it is all I used for the compilation of this small test program.

public class Fraction extends Number{
    private int numerator;
    private int denominator;

    public Fraction(String s){
        this.numerator = Integer.parseInt(s.substring(0, s.indexOf("/") - 1));
        this.denominator = Integer.parseInt(s.substring (s.indexOf("/") + 1, s.length() - 1));
    }

    public String getFraction(){
        String output = this.numerator + "/" + this.denominator;
        return output;
    }
    ///Methods for retrieving and changing both the numerator and the denominator
    public int getNum(){
        return this.numerator;
    }
    public int getDenum(){
        return this.denominator;
    }
    public void setNum(int num){
        this.numerator = num;
    }
    public void setDenum(int denum){
        this.denominator = denum;
    }

    public int intValue(){
        return (Integer) this.numerator/this.denominator;
    }
    public double doubleValue(){
        return this.numerator/this.denominator;
    }
    public long longValue(){
        return this.numerator/this.denominator;
    }
    public short shortValue(){
        return 0;
    }
    public float floatValue(){
        return 0.0f;
    }
}
Fugs
  • 39
  • 5
  • Java, in general, makes it a really bad idea to mix number types. Pick one universal one and stick with it. It's almost impossible to make code mixing number types work. But the answer to your specific problem is probably just to add a cast to `Fraction`. – Louis Wasserman Mar 14 '17 at 20:15
  • You can't use a variable `rhs` outside it's scope. Also there is not point wraping a Fraction with another Fraction. – Peter Lawrey Mar 14 '17 at 20:16
  • I would implement `toString` rather than `getFraction`and review `intValue`, `doubleValue`, `shortValue` and `floatValue` which all value issues. – Peter Lawrey Mar 14 '17 at 20:18
  • Instead of `s.substring (s.indexOf("/") - 1, s.length() - 1)` most likely you wanted `s.substring (s.indexOf("/") + 1)` – Peter Lawrey Mar 14 '17 at 20:19
  • I have to use multiple number types because I need to be able to add/subtract/multiply/divide Fractions, integers, doubles, and complex numbers. Also, I made all of the corrections that you guys suggested, However, my issue remains. – Fugs Mar 14 '17 at 20:23
  • I gave an answer below, but see http://stackoverflow.com/questions/474535/best-way-to-represent-a-fraction-in-java – marisbest2 Mar 14 '17 at 20:26

1 Answers1

1

This has nothing to do with Fraction. If Fraction subclasses Number then you should be able to cast from Number to Fraction. So something like

Fraction rhs = (Fraction) numbers.pop()
marisbest2
  • 1,346
  • 2
  • 17
  • 30