Well, I like your approach to learning. So let's look at your Rtnl class. That's really the center of your programm. You have to build a representation of Rational numbers inside your programm. What should it do? Let's start with what you've got so far.
The first thing that hurts me is the name. You should avoid cryptic abbreviations, let me suggest Rational as a more meaningful name.
The second thing that comes to mind is that the fields (numerator and denominator) are missing a private before the type. Always make fields private, that enabels encapsulation and this is key in oop. It would look like this:
private int numerator;
With that said, let's look at what the class does. Well, actually it realy doesn't do anything.
You want to make a new Rational object like this
Rational r = new Rational(1,2);
and have it to store the Values 1 and 2 in the fields. Your constructor is missing that functionality, so you should add assignments from the parameters to the fields.
Allright, what should it do next? You want to be abel to add something to a Rational. You'll need a add() method. What parameter should it take? How to implement it? Well, first look at your main how you want to use it, then make it work.
Here is a snippet from your code:
Rtnl y1 = new Rtnl (1,Math.pow(2,y));
sum = sum.add(y1);
if (y1 !=10)
There are several problems in there. In fact, every line contains an error. Let's step through it.
The first line is tricky. Your constructor takes two parameters, both of type int. 1 is clearly an int, but what about the expression Math.pow(2,y)? the Math.pow() method returns a double, you can look that up in the api documentation. To make an int from a double, you have to put (int) before it. The compiler won't do it automaticaly for you. (Why? Think about it.) That would look like this:
r = new Rational(1, (int)Math.pow(2,n));
The second line trys to add something to a Rational. Beside of the still-to-come add() method, there's an error in your thinking here. You cannot assign something to a non-primitive variable. Well, you _can_ do it - but it doesn't work as you want it to. Look up reference types in your favorite java textbook. Eventually the code should look like this:
The last line has the same problem as mentioned before. The != isn't working as expected, because again rtnl is not a primitive datatype. If you want that functionality, you have to write a method for it, for instance equals().
Allright, beside of add() and equals(), what else is missing? You want to be able to print rationals out. It would be handy to have a print() method, or even better a toString() method. Do you need to know (or modify, after construction) nominator and denominator? Write getters and setters for the fields. (Don't know what that is? Your javabook should tell you.)
Seems there's a lot of work waiting for you. Good luck.
[ April 21, 2004: Message edited by: Tobias Hess ]