Link to home
Start Free TrialLog in
Avatar of codonohue
codonohue

asked on

Help with writing a Rational Number class

I am very new to java. Frankly, I have no idea what I'm doing. I have to define a class for rational numbers. My professor wants it all in (int) form. Also, in the class she wants us to create a method that will put the negative sign into the numerator. In the main method, it should allow you to thoroughly test your Rational number class, and it is suppose to ask the user which operation to perform, and continue to put in rational numbers till they desire to quit. I was looking here for hopefully a template to follow, but all the ones on here I guess are a farther advanced java, if that even makes sense. Or it is quite possible that I'm just slow. Anyways, for the methods for add, sub, div, and mult, my prof wants us to use void methods with with a calling object and a single argment. And all the examples I've seen on here, they used return methods. Which to me makes more sense to use a return method, but I have to do it the way she wants it. B/c you can't use return methods from void methods right? Anyways, I've been working on this for a week and I have my name at the top and the class name and then a bunch of *blinks* *blinks*. Everythiing I seem to try and do is wrong, and i'm getting pretty frustrated. So, does this make any sense to you what she's asking b/c it seems to be the exact opposite of what all the examples are on here? If anyone has some guidelines I can follow for the way she wants it, so I can make a little more sense out of it, would be greatly appreciated. Or anything really that will help me, but i'm not looking for someone to do it for me, not that anyone would. Anyways, I apologize if my ignorance in this matter is obserd, but this stuff just doesn't make too much sense to me. Thank you for your time to whom ever helps me! :)
Avatar of Malagor
Malagor

Experts are not allowed to do homework for you, just help you understand it so you can do it yourself..  From your post I get the impression that you are confused by alot of it, and I would suggest seeing your professor for help.  Ill try and give you a little push to help understand the problem.

Since she wants you to use integers, you'll want to use 2 int member variables to contain the numerator and denominator.  Then you can just override the tostring function to output them back in rational form.

As for the void function for arithmatic.
If a function is void nothing will get returned and anything done will be in that object only.  I would assume this means she wants you to put the final value into the calling object.  You may want to ask her to elaborate.
So if x = 1 and y = 1
then x.add(y) would make x = 2.  so to output the result of the problem you would just output x.toString()

Having done this problem a few times in my programming classes, I would suggest the mainly overlooked part is to make sure you have a fuction to reduce the rational number to its lowest form.  You can search the net a bit to find some good algorithms for doing this.  Just make sure every time you do an arithmatic call, you reduce the result afterwards or you will end up with fractions like 6/12 when it should be 1/2.
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
We're not allowed to do your homework.
jackmcbarn,
please read what the author wrote:

If anyone has some guidelines I can follow for the way she wants it, so I can make a little more sense out of it, would be greatly appreciated.
Or anything really that will help me, but i'm not looking for someone to do it for me.

Once again:
... i'm NOT looking for someone to do it for me
Avatar of codonohue

ASKER

I've spoken to my professor, it's just something that's not clicking. I'm a math major so, I think I'm looking for some kind of pattern in all of this and I just havn't found it yet, or maybe my brain is just over loaded with equations it doesn't want anymore. And sorry if you got the impression I wanted it done for me, but I don't. I don't pay $300 a class just to copy. But anways, using the x = 1 and y =1 makes more sense to me. And to zzynx, that website is very helpful as well. Thank you for your responses.  :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No, I havn't had the chance to work on it much today after receiving the information from yall because I'm at work. I'll work on it later tonight after work and school. I think the information I have received has given me some more understanding of it, but if i'm having any more issues I'll post the code I do tonight either late tonight or tomorrow morning. Thanks.
okay here...i downloaded j creator on my computer at work and pulled up what i have saved so far..I made some quick changes to some of the info you gave me and i'm still getting the same problem....she wants us to use void methods, but when i go to place the method name into my Driver class it won't compile b/c it says "class expected" or void not allowed here, and the format I have it, is the format i'm pretty sure she wants. However, if in the Class, i switch it to a Return Value method, it will compile both my Rational Class and Rational Driver, but won't add up the values and I'll get 0.0 on my execution page. So, anyways this is what i have so far. Sorry for misspelling and grammar, i'm trying to type fast before my boss catches me. ha! And if I'm having more trouble tonight when i'm completely done, hopefully, I'll post all my code.

import java.io.*;

class Rational
{
            
      int numerator;
      int denominator;
      int alpha;
      int add;
      
      
      public Rational() throws IOException
      {
            BufferedReader in;
            in = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter first rational number: ");
            System.out.print("Enter second rational number: ");
            
      }
      
      public void Rational(int x, int y)
      {
            numerator = x;
            denominator = y;
      }
      
      public void Rational (int wholeNumber)
      {
            wholeNumber = wholeNumber/1;
      }
      
      public void Rational()//she wanted a contructor with no arguments setting any variable to zero??
      {
            alpha = 0/1;
      }
      
      public void calAdd(int y)
      {
            x = x + y;
      }      
      
      
      
      
      
      
      
   
}//end of class

**********Here's my RationalDriver******

import java.io.*;

public class CTORationalDriver
{
      public static void main(String[] args) throws IOException
      {
            Rational allRational;
            allRational = new Rational();
            System.out.println("Addition: " + allRational.calAdd(int y);
      }
}
      
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, thank you for all your help! I finished it last night and it was working! yay! She didn't give us long to do it, so last night at class she gave us an extra day to complete so I went over with her what I did have and she helped me with a few other minor things, but overall it was done right. The way you explained the adding a rational number helped me a whole lot! And I took out all the voids no my constructors that I wanted to use, like you said, and that helped a lot too! It was nice not to see a bunch of compile error messages at the bottom, my brain hates those! lol. Anyways, thanks again! :)
Thanx 4 axxepting