Solved

Help with writing a Rational Number class

Posted on 2007-03-27
11
1,422 Views
Last Modified: 2013-12-22
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! :)
0
Comment
Question by:codonohue
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 2

Expert Comment

by:Malagor
ID: 18806235
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.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 150 total points
ID: 18806237
0
 
LVL 4

Expert Comment

by:jackmcbarn
ID: 18808577
We're not allowed to do your homework.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 18808651
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
0
 

Author Comment

by:codonohue
ID: 18808788
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.  :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 50 total points
ID: 18809347
codonohue, based on what has been posted can you come up with some code so that we can help you further? You can start with having a Rational class with 2 fields - a numerator and a denominator (both ints), and methods to get/ set them, and to add rational numbers, subtract numbers, divide and multiply.
0
 

Author Comment

by:codonohue
ID: 18810400
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.
0
 

Author Comment

by:codonohue
ID: 18811799
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);
      }
}
      
0
 
LVL 2

Accepted Solution

by:
Malagor earned 300 total points
ID: 18812267
public void Rational() and public void Rational(int x, int y) are not proper constructors, constructors have no return field listed.  So you would want:
public Rational(){
  num = 0; denom = 1;
}
and
public Rational(int num, int denom){
 //set values to num and denom
}
just remove the void flag from any constructors you want to have

The class not found sounds like its a problem with the way you have the class declared, try adding public to the front of the class declaration:  
public class Rational
Also make sure you are naming the java file its in the same as the class name. It should be Rational.java (make sure R is capitalized in both)


I didnt mean to make your add use x& y just to add 2 rational numbers, I was just tryin to give an example. :)
public void calAdd(int y)
{
       x = x + y;
}
this is fine if you want to add a whole number to your rational number, but you will need to change the way you add it.  If you wanted to add another rational number to it, you would need to make the parameter (Rational y) and add public get methods to retrieve the numer and denom so you can get them from y to add to x.  If int y is going to be a whole number, then you would do
this.numerator = this.numerator + y * this.denomerator  
(this isnt needed, just added so you realize your using the num and denom of the caller)
Since add is void, nothing is given back and just the class's members will be changed.  You will need to have some sort of output method to output
number + "/" + denom
then use that to output the results

as for adding a rational number to it, ill try to explain it in a mathematics way youd understand.

if you had 2 numbers:  a/b and c/d
to add them you would use ad+cb/bd
so your rational add funciton would be something like:
this.num = this.num*y.denom + y.num*this.denom
this.denom = this.denom*y.denom

then reduce this.num & this.denom to the lowest possible fraction

0
 

Author Comment

by:codonohue
ID: 18822738
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! :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 18822756
Thanx 4 axxepting
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now