0

I have the following code and I need to convert my string o1 & o2 (input by the user) into the Fraction class f1 & f2. I'm not sure how to do this and any help would be appreciated!

Update: Setters and getters are already in place, I only pasted the relevant aspects of the code in here. Everything works but I don't know how to assign f1 a value (the value of which should be o1).

private int numerator;
private int denominator;
public Fraction(){
    numerator = 0;
    denominator = 1;
}

public static void main(String[] args){
    String o1 = null;
    String o2 = null;

    Scanner scan = new Scanner(System.in);
    System.out.println("Input Operand 1, separate numerator and denominator by '/'.");
    o1 = scan.next();
    System.out.println("Input Operand 2, separate numerator and denominator by '/'.");
    o2 = scan.next();

    Fraction f1 = new Fraction();
    int n1 = f1.getNumerator();
    int d1 = f1.getDenominator();
    Fraction f2 = new Fraction();
    int n2 = f2.getNumerator();
    int d2 = f2.getDenominator();

    Fraction A = new Fraction(n1, d1);
    Fraction B = new Fraction(n2,d2);
    A.printF(add(A,B));
    A.printF(sub(A,B));
    A.printF(multiply(A,B));
    A.printF(divide(A,B));      
}
2
  • stackoverflow.com/questions/3481828/… Commented Feb 14, 2014 at 19:41
  • 1
    Need more details... user enters, e.g. "2/5" and you want to create a "Fraction" with numerator 2 and denominator 5 and "Fraction" is your own class? Apache's NumberUtils has methods to do String to int, but what you'll want to do is split the input on "/" to get numerator and denominator...and then store those. Commented Feb 14, 2014 at 19:42

2 Answers 2

3

If I understood correctly...

First step: splitting at the slash

String[] o1Parts = o1.split("/");
String[] o2Parts = o2.split("/");

Second step: converting to int

int o1Numerator = Integer.parseInt(o1Parts[0]);
int o1Denominator = Integer.parseInt(o1Parts[1]);

int o2Numerator = Integer.parseInt(o2Parts[0]);
int o2Denominator = Integer.parseInt(o2Parts[1]);

Third and last step: creating the Fraction object

Fraction o1Fraction = new Fraction(o1Numerator, o1Denominator);
Fraction o2Fraction = new Fraction(o2Numerator, o2Denominator);

In case the class doesn't have this constructor, but only setters:

Fraction o1Fraction = new Fraction();
o1Fraction.setNumerator(o1Numerator);
o1Fraction.setDenominator(o1Denominator);

Fraction o2Fraction = new Fraction();
o2Fraction.setNumerator(o2Numerator);
o2Fraction.setDenominator(o2Denominator);

Caveats

Please mind that this code does no input validation, so if you write something that is not number/number, it'll break. Also, I did not compile to test it, so there may be one or two syntax errors hiding in there, but this is the basic idea.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was saying in my comment, but the requirements were somewhat unclear. This is a good solution, assuming the requirements are what it seems.
1

You can separator the String on basis of '/' and set the value of numerator of denomerator attribute of class Fraction as below :

        String o1 = "10/5";
        Fraction f1 = new Fraction();
        f1.setNumerator(Integer.parseInt(o1.substring(0,o1.indexOf("/"))));
        f1.setDenomerator(Integer.parseInt(o1.substring(o1.indexOf("/")+1)));

I don't find the setter and getter for the attribute,create it and set the value with above code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.