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! :)

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.

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

codonohueAuthor Commented:

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. :)

Mayank SAssociate Director - Product EngineeringCommented:

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

codonohueAuthor Commented:

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

codonohueAuthor Commented:

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);
}
}

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

codonohueAuthor Commented:

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! :)

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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.