1

Whenever I try to run my code, I get the error "The method add(String) is undefined for the type String," "The method subtract(String) is undefined for the type String," and so on for the multiply and divide methods. I don't know if the rest of my code runs correctly because I haven't been able to run the actual program. Why am I getting this error?

Main:

import java.util.*;

public class RationalNumberMain 
{

      public static void main(String[] args) 
      {

          Scanner s = new Scanner(System.in);

          System.out.print("Enter first fraction here: ");
          String r1 = s.nextLine();

          System.out.print("Enter second fraction here: ");
          String r2 = s.nextLine();

          RationalNumber x = new RationalNumber(r1);
          RationalNumber y = new RationalNumber(r2);

            System.out.println("Rational Number 1 is " + r1);
            System.out.println("Rational number 2 is " + r2);
            System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
            System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
            System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
            System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
      }
}

Class:

public class RationalNumber 
{

    private String fraction;
    private String s1;
    private String s2;
    private int num;
    private int den;
    private int combineNum;
    private int combineDen;
    private String finalFraction;
    private int divideNum;
    private int divideDen;


    public RationalNumber(String userFraction) 
    {
        fraction = userFraction;
        s1 = "";
        s1 = "";
        num = 0;
        den = 0;
        combineNum = 0;
        combineDen = 0;
        finalFraction = "";
        divideNum = 0;
        divideDen = 0;
    }

    public String toString() 
    {
        return fraction;
    }

    public void seperate()
    {
        s1 = fraction.substring(0,1);
        s2 = fraction.substring(2);
        num = Integer.parseInt(s1);
        den = Integer.parseInt(s2);

    }

    public int getNum() 
    {
        return num;
    }

    public int getDen() 
    {
        return den;
    }

    public String add(RationalNumber x) 
    {
        combineDen = den * x.getDen();
        combineNum = (num * x.getDen()) + (x.getNum() * den);
        finalFraction = String.valueOf(combineDen) + "/" + String.valueOf(combineNum);
        return finalFraction;
    }

    public String subtract(RationalNumber x) 
    {
        combineDen = den * x.getDen();
        combineNum = (num * x.getDen()) - (x.getNum() * den);
        finalFraction = String.valueOf(combineDen) + "/" + String.valueOf(combineNum);
        return finalFraction;
    }

    public String multiply(RationalNumber x) 
    {
        combineDen = den * x.getDen();
        combineNum = num * x.getNum();
        finalFraction = String.valueOf(combineDen) + "/" + String.valueOf(combineNum);
        return finalFraction;
    }

    public String divide(RationalNumber x) 
    {
        divideNum = x.getDen();
        divideDen = x.getNum();
        combineDen = den * divideDen;
        combineNum = num * divideNum;
        finalFraction = String.valueOf(combineDen) + "/" + String.valueOf(combineNum);
        return finalFraction;
    }

}
2
  • Don't you mean add(x) and add(y) instead of r1 and r2? This is why it's good to use meaningful variable names, not arbitrary ones like x or r. Your method expects a RationalNumber and you are passing a String. Commented Feb 6, 2020 at 16:59
  • Have you a JDK properly installed and configured? Do the code ever ran before? Commented Feb 6, 2020 at 17:00

3 Answers 3

3

r1 is of type String:

String r1 = s.nextLine();

What you want it to add objects of type RationalNumber (created from r1):

RationalNumber x = new RationalNumber(r1);

that is:

x.add(y) instead of r1.add(r2)

And the same for other arithmetic operations in your code.

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

Comments

2

These lines of code should be interacting with the rational numbers, not the input.

        System.out.println(r1 + " + " + r2 + " = " + x.add(y));
        System.out.println(r1 + " - " + r2 + " = " + x.subtract(y));
        System.out.println(r1 + " * " + r2 + " = " + x.multiply(y));
        System.out.println(r1 + " / " + r2 + " = " + x.divide(y));

An IDE like IntelliJ would have shown that x and y were created but never used. That would have been a good clue that something was not as you were planning.

Comments

0

I am not sure what you expect. java.util.String does not have any of these method add, subtract, multiply, divide.

Did you mean:

System.out.println(r1 + " + " + r2 + " = " + x.add(y));
System.out.println(r1 + " - " + r2 + " = " + x.subtract(y));
System.out.println(r1 + " * " + r2 + " = " + x.multiply(y));
System.out.println(r1 + " / " + r2 + " = " + x.divide(y));

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.