File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Vector help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector help" Watch "Vector help" New topic
Author

Vector help

Fran Montanez
Greenhorn

Joined: Jan 14, 2005
Posts: 14
This is my first time posting so I hope I'm posting this in the right place...

I tried to create a Vector that holds objects of Type Point.
I need to use the formula to operate on the Points in the Vector, so that each x, y, and z value get updated in order to have 14 new Points stored in the vector.
When I tried to run the program, the complier said that problem was in the area with the formula. What am I doing wrong. Thanks to any help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

You are passing three numbers as arguments to your call to addElement(), but addElement only takes one argument, an Object (a Point qualifies as an Object). You simply need to construct a new Points(), just as you did earlier in the program, and then pass that as an argument to addElement. You could write



But personally, this is far too messy. I'd write



Now, even given this, I think the code still won't compile. The problem is that your equations are going to return doubles -- i.e., real numbers --, but you're trying to store them in integer variables. There's nothing wrong with that, necessarily (although I bet you'll find the loss of precision leads to bad looking graphics) but you need to tell the compiler that you know what you're doing by using a "cast". That would look like



Where, in every case, ... represents one of your long expressions.

Final point: An instance of your class "Points" represents a point. You therefore ought to rename it "Point".


[Jess in Action][AskingGoodQuestions]
Fran Montanez
Greenhorn

Joined: Jan 14, 2005
Posts: 14
Thank You very much for your help I'll try this....
Fran Montanez
Greenhorn

Joined: Jan 14, 2005
Posts: 14
Ok,

I tried your suggestion, However the program is still not compiling. I'm at a complete loss. Any help is really appreciated

my new code:

And these are the errors I'm getting:
A:\BottleRotation3.java:116: cannot resolve symbol
symbol : method getx ()
location: class java.lang.Object
int x = (int) ((ve.get(i).getx()*(float)Math.cos(ang*count) + ve.get(i).getx()*(float)Math.sin(ang*count)));
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Fran Montanez:
A:\BottleRotation3.java:116: cannot resolve symbol
symbol : method getx ()
location: class java.lang.Object
int x = (int) ((ve.get(i).getx()*(float)Math.cos(ang*count) + ve.get(i).getx()*(float)Math.sin(ang*count)));
This is telling you that you are trying to call the method getx() on an object reference whose declared type is Object. While you are putting Point instances into the Vector, the Vector class declarations all use "Object". Specifically, the return type of Vector.get() is Object, so the expression "ve.get(i)" is an Object which doesn't have a getx() method.

You must tell the compiler that you expect it to be a Points at runtime by casting it.
Fran Montanez
Greenhorn

Joined: Jan 14, 2005
Posts: 14
That did it! Thank you very much for all your help
Fran Montanez
Greenhorn

Joined: Jan 14, 2005
Posts: 14
I was wondering do I still need this if I wanted to add the x, y, z to my vector?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Fran Montanez:
I was wondering do I still need this if I wanted to add the x, y, z to my vector?


I'm not sure what you are asing here. Can you please explain?

Also, I'd like to add my two bits about your code. I think David answered your question very well. I'd just like to point out that part of your problem is caused by trying to do too much in a single line of code. Sometimes it is helpful to be able to chain method calls (like a.b().c()). However, as you have probably noticed, it can lead to some confusion. It is often very helpful to break lines of code up. For example, David's solution calls ve.get(i) and stores the value in a temporary variable. Also, I would call p.getx() and store that. So the code could look something like this:


IMO, this looks a little cleaner. Unfortunately, there isn't much you can do about the mathematical formula because it's a little complicated, but at least it looks slightly less messy than the original version. (In editing your code, I found that the cast to float is unncessary. Also, there were some extra parenthesis.)

HTH

Layne

p.s. Are you doing rotations here? I've dabbled a little with graphics and the math here looks awfully familiar.


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
After glancing at your code, I have a question. It looks like you are using OpenGL for your graphics here. I've only used OpenGL in C++, not Java, but I assume the internals are the same and only the public API differs slightly (since everything must be wrapped in a class in Java). OpenGL can take care of the rotation transformations for you and you don't ever need to update your list of Points. You do this by calling glRotate() with a new value each time you need to redraw the object. Is there some other reason you are doing the rotation calculations yourself? It's certainly a good educational exercise to do this work yourself, but you shouldn't mix your own calculations with the one's OpenGL uses IMO. I only mention this because it looks like you are using glRotate() along with your own calculations. As I said above, I think this is a bad idea.

Anyway, that's just my two cents. Good luck with your program.

Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
One last comment: When you have other questions on this program, you may want to mosey over to the "other" APIs forum or the Game Development forum. Your questions are definitely appreciated here. Your questions may fit the topics discussed there a little more closely (this is a very general forum after all). Also, you are more likely to find people that know the answers there, me thinks.

Layne
Fran Montanez
Greenhorn

Joined: Jan 14, 2005
Posts: 14
I am using openGL. Actually Jogl, which uses Java. I have to create a surface of revolution project which is why I'm using the formulas to do my own rotation.

My problem is with trying to use GL.linestrip with the int x, int y, int z that is defined in the inner loop. I'm really desperate with this program because I have a deadline. I'm not sure how to proceed. Any help is welcome and appreciated.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector help