Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Slowly getting it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Slowly getting it" Watch "Slowly getting it" New topic
Author

Slowly getting it

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Hi,
I'm still on my rational problem of writing a program to display:
1/1 + 1/2 + 1/3 + ...+ 1/n
1/1 + 1/2 + 1/2^2 +...+ 1/2^n
I'm trying to work on the whole constructor thing and I dont' really know if I"m getting anywhere. Can someone help me please and give me a hint of what I'm doing wrong. I'm not sure if I need to have "class Rtnl" in there. I'm a bit shady on the constructor concept as well.
I've posted my code below and then the errors.

The errors are as follows:

62: cannot resolve symbol
symbol : method add (Rtnl)
location: class Rtnl
sum = sum.add(x1);
^
74: cannot resolve symbol
symbol : constructor Rtnl (int,double)
location: class Rtnl
Rtnl y1 = new Rtnl (1,Math.pow(2,y));
^
:76: cannot resolve symbol
symbol : method add (Rtnl)
location: class Rtnl
sum = sum.add(y1);
^
operator != cannot be applied to Rtnl,int
if (y1 !=10)
^

Thanks in advance for the help.
Stacey
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
The first think you have to do is visualise main() with next to no code at
all - regard it as an entry point (only) to a program.
here's a simple demo with constructors.
Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
I'm sorry by I still don't understand. Your sample looks nothing like what I'm doing so I don't even know if I'm doing anything right. I was told to make some parameters in my constructor, but I really don't know if I'm going about it right.
Stacey
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Try this (if all you want to do is display, per your description)
Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Thanks for giving me code, but that doesn't really help me figure anything out. Can anyone look at my code and tell me if I'm way off base or where I should go from here. I would really like to work with what I have and work the errors out. I want to learn what I'm doing.
Thanks
Stacey
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55

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:
sum.add(y1);
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.
HTH,
Tobias
[ April 21, 2004: Message edited by: Tobias Hess ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

in your original post, you say "I'm a bit shady on the constructor concept as well." Let me see if i can help with that...
a constructor is a blueprint on how to build the class you are trying to define - but more simply, it's a method, just like every other method you've written. it can take parameters (or not), you can have overloaded constructors, etc. Every time you do a "new", a constructor is being called. In fact, this is about the ONLY way (with a few small exceptions) to call a constructor.
what gets a little confusing is that if you don't write a constructor, the java compiler writes one for you. it will look like this
public Rtnl(){
}
this takes no parameters, and does nothing, but it's there. BUT, as soon as you write ANY constructor, the jvm says "oh, she wrote one, so i don't have to now". Usually, if you write a constructor, you will also write the "no arg. constructor" as well.
So, what does the constructor do? it sets things up. it initializes your object. it does whatever you need it to do to make sure your object is safe to use. In your Rtnl class, you have two numbers, a numerator and denominator. if you create a Rtnl, should those values be initialzed to something? probably. and, you probably don't want the denom to be 0.
so, in your case, you might decide that if somebody creates a new Rtnl, and doesn't want to give it a value, you will default initialize it to the equivilent of 0.0. so your constructor could look something like

ok, so now i can make a Rtnl, but EVERY one i make will be equivilent to 0.0. It might be nice to have some methods that let me change this, or i might not want to allow that (i.e. once it's made, you can't change the value). but that's another issue. Back to constructors.
As you know, Java allows methods to be overloaded. i can use the same method name over and over, as long as they all have different SIGNIATURES. that means the type and order of parameters i pass it are different. for example
public int myMethod(int fred, int stacey){
//whatever
}
public int myMethod(float fred, int stacey){
//ok!!! types of parameters changed!!!
}
public int myMethod(int stacey, int fred){
//WON'T WORK!!! I HAVE TWO METHODS with (int, int)
}
The same holds for constructors. it might be nice if for your Rtnl class, i could pass it in a numerator and a denominator. i need A SECOND constructor...

the object will now be intialzed how i want. but there's a problem with what i have above. what happens if the user passes in a '0' for the denom? i can handle that in my constructor. instead of what's above, i can do this:

or you could throw an exception, print an error message, end the program... you could HANDLE an error. You have control over your object.
now, maybe i want to let somebody make my object, and pass in a single int, and make the resulting object equivilant to that integer. so the denom would have to be 1. i'd do something like


SO, now that i have all these constructors, what do i do with them??? in your main routine, say you need a Rtnl. now, you can say:

i could write constructors that took two floats, a float and an int, a single float that i then convert into a integer num and denom... whatever i think i'll need.
ok, i wrote a LOT more than i meant to. sorry. let me know if this makes no sense. but i hope it helps.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Thanks for the help so far, I've worked some more on it but I just don't know if I'm placing things right. I'm kinda confused at times. I'm trying to separate things out where they should go but I know I'm wrong. I know the first part of my code alone works to display:
1/1 + 1/2 + 1/3 + ...+ 1/n
It's the second part that is messing things up.

Am I even on the write track. I've read the comments so far and this is what I can sort of get from it. How wrong am I?
Stacey
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55
Hi Stacey,
it doesn't look that bad to me now. You could improve the first constructor with the suggestions Fred gove you about checking the parameter to be none-zero.
I know the first part of my code alone works to display:
How can you know? The code doesn't compile yet, does it?
Maybe it would be helpfull if we have some insights in your learning situation. Why do you learn java? How do you do it? Do you have a good textbook in place?
Tobias
Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
My learning situation is a tough one, I'm taking this beginner Java course (through a university for credits towards my degree) via correspondence. If I would have know it was going to be this tough I would have definitly taken it in the classroom format. Plus I work full time so I'm a bit exhausted. My text book is "Introduction to Java Programming 4th ed. By Y. Daniel Liang.
So I hope that gives you a better idea of where I'm coming from.
I know the first part of my code works because I tried it before I did anything else. It's the second half that I'm adding on that is giving me the grief. But I must say, I do appreciate the help more than you know .
Stacey
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55
Allright, that's pretty much the same thing what I had. It should work just fine. ;-)
I don't know your book, and what topics have you learned until now. To do it the way I suggest it in previous posts, you need to know about classes and objects, object instantiation, methods, parameters, return types. From the level of your confusion about constructors and the like (sorry) I assume that you're not yet ready for this. Maybe a solution with all the code in main and without objects and methods is the right one. (That's what you had allready, isn't it?) What did you learn so far? If the focus was on expressions, statements and control flow the main-solution should be ok.
Later you can always come back to this exercise and solve it in another way.
HTH,
Tobias
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Slowly getting it
 
Similar Threads
Back to rational problem
Heelllpppp! I'm sooo lost
The Programme is not Compiling
Please Help
Help with Rationals