I am ask to write a class for rational numbers. A rational number is quotient of two integers, such as 2/3 or ½. Your class should have three constructors. One constructor takes no argument, and initializes the rational number to 0. Another constructor takes a numerator only (the denominator is assumed to be 1). The third constructor takes both a numerator and denominator. Your class should have methods for adding, subtracting, multiplying, and dividing rational numbers. Keep in mind that before adding or subtracting rational numbers, the denominators must be the same in both numbers. Ideally, your class should also convert all results to the smallest numerator and denominators. For example, if a result yields 4/8, your class should convert it to ½. Write a program that shows that all methods in the Rational class work as they should.

Belows are my code and i am stuck here, actully i am the kind of person that not too smart to think logicall, if you r willing to help me, can u give me a hint that is clear enough for me to understand. Thank u very much

``````public class rationalNumbers {

/**
* @param args
*/
public static void main(String[] args) {
int a;
}
class Rational{
int num=0;
Rational(){

}
Rational(int a){

}
Rational (int a,int b){

}

}
}``````

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

2
Contributors
1
2
Views
6 Years
Discussion Span

Your Rational class seems ok. But you need to add private attributes to the class. When you call the constructor you need to assign the arguments.

``````class Rational{

private int numerator = 0;
private int denominator = 1; // it cannot be zero!

Rational(){
numerator = 0;
}

Rational(int a){
numerator = a;
}

Rational (int a,int b){
numerator = a;
denominator = b;
}

}``````

You need to add checks. If for example someone enters zero for denominator you need to display a message:

``````Rational (int a,int b){
numerator = a;

if (b==0) {
} else {
denominator = b;
}

}``````

You also need a method that makes changes to the numerator, denominator so when the user enters: 4/8, you need to convert that to 1/2. So you can do this:

``````class Rational{

private int numerator = 0;
private int denominator = 1; // it cannot be zero!

Rational(){
numerator = 0;
}

Rational(int a){
numerator = a;
}

Rational (int a,int b){
numerator = a;

if (b==0) {
} else {
denominator = b;
}

simplify();
}

private void simplify() {
// numerator
// denominator
}

}``````

You need to call that method whenever you make changes to the numerator, denominator. It is better to call that method from inside the class. That is why it is private as well as the numerator, denominator. If you add public set methods you can call that method inside them. Also add more methods that do what your assignment describes. And add public methods that print, set or return the values of the attributes.

When you are done call them in a main:

``````public static void main(String[] args) {
Rational r = new Rational(2,3);

// call your methods for testing
}``````