This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes problem with interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with interfaces" Watch "problem with interfaces" New topic
Author

problem with interfaces

Nicole Gruber
Greenhorn

Joined: Jan 15, 2009
Posts: 10
I already have an interface Vector.java and i must create a ReellerVector.java that implements Vektor.java.


ReellerVector.java looks like this:



the class Test contains the main Method:



Of course, it's not right how I wrote ReellerVector.java and therefore I get this Output:
v1: 3
v1: 2.0
v1: Vector: [D@42e816.
v1: Vector: [D@42e816.
v1: Vector: [D@42e816.

Can someone help me ???
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Use Code tags for better readability.

v1: 2.0
v1: Vector: [D@42e816.
v1: Vector: [D@42e816.
v1: Vector: [D@42e816.

Can someone help me ???


What is the expected help ? any error ? how to print out the values form Vector ? chek out Vector Iterator method


public Vector plus(Vector v)
{

for(int n = 0; n < dimension.length; n++)
{

dimension[n] = this.dimension[n] + dimension[n]; // logic error.
}

return v;
}


Seems you dint do anything with Vector v passed as parameter, just returning it ?
Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
Here is code tagged version for anybody to read:


ReelerVector.java




Vector.java



Test.java




As Balu said, you must tell what do you expect. Probably you want plus and times method to add and multiply all elements in array. But you dont output or return anything from those methods but reference you've passed to them while invoking.
Nicole Gruber
Greenhorn

Joined: Jan 15, 2009
Posts: 10
well, i am not suposed to use java.util.Vector.
I have to add up two vectors in the method public Vector plus(Vector v). i know i shouldn't have written return v, but another Vector, for example Vector sum. I just don't know how to do it.. In order to add up two vectors you need to add up each number from the ArrayList. In my example the answer would be a vector: {1+1, 2+2, 3+3}. Now, I don't know how to write that, espacially what to return.
The same problem is at public Vector times(double skalar), where I need to multiply the Vector with a number...
I am a beginner so that all is very difficult for me to understand...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Welcome to JavaRanch

As people have said, please use the code tags to maintain legibility; I have added code tags to your post so you can see how much better it looks.
Pawel Nowacki is quite correct that you are not actually adding the two Vectors. You should have written v.dimension[n] + dimension[n]. You are also returning the parameter Vector, probably unchanged.
You have a stylistic problem: you have an int getDimension() method, but what you have as the field dimension is a double[]. You ought to rename the field, maybe calling it data. You also need to be careful not to confuse this interface with the java.util.Vector class.

The output you have shown is not an error; it is not what you expected, but it is not an error. If you delete the toString() method from the interface, and try again, the whole thing will still compile, and you won't notice any difference in it. For an explanation, start by reading about the Object#toString() method. I hope that will make it clear what is happening.
Nicole Gruber
Greenhorn

Joined: Jan 15, 2009
Posts: 10

But I can't write v.dimension[n] because I have no dimension variable in the interface Vector... ( and I am not alowed to change the interface Vector.java)

P.S. I also changed the field dimension to int[].
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Nicole Gruber wrote:

A warning - y shares the array with your current ReellerVector. So if you change some element in y, you change the same element in the current ReellerVector!

To prevent this, make a copy of your array in the constructor:

This way, each ReellerVector has its own copy of the array, and changes in one object will not affect all other objects. As a bonus, you get a null-check in your constructor - if the array is null a NullPointerException is thrown.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Nicole Gruber wrote:
But I can't write v.dimension[n] because I have no dimension variable in the interface Vector... ( and I am not alowed to change the interface Vector.java)

P.S. I also changed the field dimension to int[].

Then interface Vector is flawed - it should have a method to query the value at a specific index. How can you add two Vectors if you don't know what each element of the Vectors is??

So Vector should have a query method:

Without this method you can't finish your assignment for all Vector types.


It's like like asking me to tell you what's inside a fridge without allowing me to look into the fridge. Unless I develop X-ray eyes I simply can't.
Nicole Gruber
Greenhorn

Joined: Jan 15, 2009
Posts: 10
yes, you were right. The method double getElement(int index); was missing.
Thanks Rob

Now I think I understood all my faults, thank to all of you who helped!!!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with interfaces