Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Having a problem with to String

    Hello I am trying to learn java and I am having a problem adding and mulitiplying fractions and displaying it on a to String. My main class is called Rational and have two private ints numerator and denominator. I have a constructor with two objects: int newNumerator and newDenominator. I have two public accessors methods and two public moderator methods. I created two public methods: one named Rational multiply (Rational r) and Rational add (Rational r) ( this is where the problem starts). I put two temp variable inside the Rational multiply method and four temp variable inside the Rational add method. I did my algorithm for both and get no errors. I have for both to return new Rational (tempvariable1, tempvariable2) for both. Now when I start the toString method, I type my phrase and include my temp variables but there are error lines at each variable. Can someone please help me, I'll really appreciate it.

    here is code for Rational methods:
    Java Code:
     public Rational multiply (Rational r)
     {
         int tempnumer;
         int tempdenom;
         tempnumer = numerator * r.numerator;
         tempdenom = denominator * r.denominator;
       
         return new Rational(tempnumer, tempdenom);
     }
     
     public Rational add (Rational r)
     {
           int tempa;
           int tempb;
           int tempc;
           int tempd;
         
         tempa = denominator * r.denominator;
         tempb = r.denominator * numerator;
         tempc = denominator * r.numerator;
         tempd = tempb + tempc;
         
         return new Rational(tempd,tempa);
     }
     
     
    public String toString()
     {   
         return "2/3 * 5/7 = " + tempnumer + "/" + tempdenom + "/n" +
          "2/3 + 5/7 = " + tempd + "/" + tempa;        
         
     }
    Last edited by Norm; 06-01-2012 at 02:58 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Having a problem with to String

    there are error lines at each variable.
    Please copy and paste here the full text of the error messages.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    The error message is: cannot find symbol
    symbol: variable tempnumer
    location: class Rational

    each temp variable has this error message

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Having a problem with to String

    The compiler can not find a definition for the variable shown in the message: tempnumber that is in scope (within the same pair of {}s) where the variable is being used.
    Where are those variables defined? Make sure they are in scope where you are trying to use them.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    The variable are only defined in those methods and nowhere else

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Having a problem with to String

    The compiler can not find the definitions for those variables that are in scope where you are trying to use them.
    Do you know what "in scope" means?

    Where is tempnumber defined?

    Where is the error message?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    Quote Originally Posted by Norm View Post
    The compiler can not find the definitions for those variables that are in scope where you are trying to use them.
    Do you know what "in scope" means?

    Where is tempnumber defined?

    Where is the error message?

    in scope meaning parameters? tempnumer(numerator) is defined inside the Rational multiply (Rational r) method and nowhere else.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Having a problem with to String

    tempnumer(numerator) is defined inside the Rational multiply (Rational r) method and nowhere else.
    Then there should not be a problem using tempnumer in the multiply() method. You will not be able to access that variable from any other method.
    The posted (post#3) error message does not show the line and line number where the compiler found the problem.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    Quote Originally Posted by Norm View Post
    Then there should not be a problem using tempnumer in the multiply() method. You will not be able to access that variable from any other method.
    The posted (post#3) error message does not show the line and line number where the compiler found the problem.
    The error message was at line 29 and 30 where I put the variables. If you want I can show you the entire code and not just that snippet I copied on here.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Having a problem with to String

    Did you read and understand what I said in post #8? Especially this part:
    You will not be able to access that variable from any other method.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    Quote Originally Posted by Norm View Post
    Did you read and understand what I said in post #8? Especially this part:
    You will not be able to access that variable from any other method.
    I understand, but I have tried to put the variables in the main class an when I ran the program, it equaled 0/0.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Having a problem with to String

    Was that the wrong answer?
    You'll have to post the code so we can see what the problem is.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    Quote Originally Posted by Norm View Post
    Was that the wrong answer?
    You'll have to post the code so we can see what the problem is.
    Main Class
    Java Code:
    public class Rational 
    {
        private int denominator;
        private int numerator;
        
         public Rational(int newDenominator, int newNumerator) //Set up the constructor
        {
            setDenominator(newDenominator);
            setNumerator(newNumerator);
        }
        
        
        public int getDenominator() // Set up the accessor
        {
            return denominator;
        }
        public int getNumerator() //Set up the accessor
        {
            return numerator;
        }
        
     public void setDenominator(int newDenominator) // Set up the moderator
     {   if (newDenominator >=1)
         denominator = newDenominator;
     else if (newDenominator == 0 )
         denominator = 1;
     
     }
     
     public void setNumerator(int newNumerator) //Set up the moderator
     {   
         numerator = newNumerator;
     }
     
     public Rational multiply (Rational r)
     {
         int tempnumer;
         int tempdenom;
         tempnumer = numerator * r.numerator;
         tempdenom = denominator * r.denominator;
       
         return new Rational(tempnumer, tempdenom);
     }
     
     public Rational add (Rational r)
     {
           int tempa;
           int tempb;
           int tempc;
           int tempd;
         
         tempa = denominator * r.denominator;
         tempb = r.denominator * numerator;
         tempc = denominator * r.numerator;
         tempd = tempb + tempc;
         
         return new Rational(tempd,tempa);
     }
     
     
    public String toString()
     {   
         return "2/3 * 5/7 = " + tempnumer + "/" + tempdenom + "/n" +
          "2/3 + 5/7 = " + tempd + "/" + tempa;        
         
     }
    
    Client
    
    public class RationalClient {
        
        public static void main(String [] args)
        {   
             // Setting up an object to pull from Rational and display the integers on the print line
            Rational integer = new Rational(3, 2);
            System.out.println("The numerator of rational # 1 is " + integer.getNumerator());
            System.out.println("The denominator of rational # 1 is " + integer.getDenominator());
            
            
            Rational fraction = new Rational(7, 5);
            System.out.println("Rational # 2 is " + fraction.getNumerator() + "/" + fraction.getDenominator());
            
          // Suppose to display the addition and multiplication of the fractions
            System.out.println(integer.toString());
    //????? end of code???
    Last edited by Norm; 06-02-2012 at 04:14 AM. Reason: added code tags

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Having a problem with to String

    when I ran the program, it equaled 0/0.
    Please post the console output showing the problem.

    Does the code you just posted compile? It looks like it has the same problem as your first post. Where is the code where you have tried to put the variables in the main class?
    Last edited by Norm; 06-02-2012 at 04:18 AM.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    Quote Originally Posted by Norm View Post
    Please post the console output showing the problem.

    Does the code you just posted compile? It looks like it has the same problem as your first post. Where is the code where you have tried to put the variables in the main class?
    This is how I had it when I put it into the main class :

    public class Rational
    {
    private int denominator;
    private int numerator;
    int tempnumer;
    int tempdenom;
    int tempa;
    int tempb;
    int tempc;
    int tempd;
    public Rational(int newDenominator, int newNumerator) //Set up the constructor
    {
    setDenominator(newDenominator);
    setNumerator(newNumerator);
    }


    public int getDenominator() // Set up the accessor
    {
    return denominator;
    }
    public int getNumerator() //Set up the accessor
    {
    return numerator;
    }

    public void setDenominator(int newDenominator) // Set up the moderator
    { if (newDenominator >=1)
    denominator = newDenominator;
    else if (newDenominator == 0 )
    denominator = 1;

    }

    public void setNumerator(int newNumerator) //Set up the moderator
    {
    numerator = newNumerator;
    }

    public Rational multiply (Rational r)
    {

    tempnumer = numerator * r.numerator;
    tempdenom = denominator * r.denominator;

    return new Rational(tempnumer, tempdenom);
    }

    public Rational add (Rational r)
    {


    tempa = denominator * r.denominator;
    tempb = r.denominator * numerator;
    tempc = denominator * r.numerator;
    tempd = tempb + tempc;

    return new Rational(tempd,tempa);
    }


    public String toString()
    {
    return "2/3 * 5/7 = " + tempnumer + "/" + tempdenom + "/n" +
    "2/3 + 5/7 = " + tempd + "/" + tempa;

    }

    When I compile this code it give me the 0/0

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Having a problem with to String

    Its better if the code compiles. If it does not compile, then it will never work.

    Try debugging the code by adding println statements to show the values of variables as they are changed and used. The print out can show you where the problem is. Add lots of printlns to show the values of all the variables.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    Quote Originally Posted by Norm View Post
    Its better if the code compiles. If it does not compile, then it will never work.

    Try debugging the code by adding println statements to show the values of variables as they are changed and used. The print out can show you where the problem is. Add lots of printlns to show the values of all the variables.
    I tried using the debugger but I kinda still don't understand it fully since I'm still learning java

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Having a problem with to String

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default Re: Having a problem with to String

    Did I do something wrong?

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Having a problem with to String

    Quote Originally Posted by thorobred View Post
    Did I do something wrong?
    Yes you did ;-) may I suggest that you keep your Rationals immutable (i.e. you shouldn't be able to change the numerator and/or denominator); also simplify the numerator and denominator in the constructor of that Class; you have to do that only once. The operators (add, mul etc.) return a new Rational object. The only clever stuff should happen in your toString() method, i.e. 3/1 should be printed as 3 and 0/3 should be printed as 0.

    kind regards,

    Jos
    Last edited by JosAH; 06-02-2012 at 09:59 AM. Reason: silly typo ...
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2010, 10:26 AM
  2. String Display problem
    By 123456 in forum New To Java
    Replies: 4
    Last Post: 06-15-2010, 02:15 PM
  3. Problem Showing A Big String?
    By AJArmstron@aol.com in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 03:56 AM
  4. string to negative int problem
    By pendulum101 in forum New To Java
    Replies: 5
    Last Post: 11-01-2009, 06:47 PM
  5. String Problem
    By gnomewise in forum New To Java
    Replies: 1
    Last Post: 10-19-2008, 01:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •