• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Slowly getting it

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this (if all you want to do is display, per your description)
 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic