Home arrow static arrow Java Programming [Archive] - Help please: Vector, its components and the variables of its components
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Help please: Vector, its components and the variables of its components
This topic has 1 reply on 1 page.

Posts:22
Registered: 7/28/04
Help please: Vector, its components and the variables of its components  
Aug 1, 2004 5:43 AM



 

Hi guys,

somebody know why and how I can work out the following problem? The below FIRST small program works, but the problem is that I need to uso the components vectors and their variables many times in different parts of a big program, and they are more complex objects. I need to be able to access any of the components. That's why I have defined the vector.

Therefore I'd like to access the k-th component as I have written in the SECOND program, but if you can check it, you realize it doesn't compile. I don't wanna define other 'temp' variable which is not independent, I am trying to use the v.elementAt(k) object and its variables directly, but Java doesn't allow me.

Does anybody know how I can do that?????

Thank you very much.


FIRST VERSION

import java.util.*;

class square
{
String name;
public double x;
public double y;
double u[][] = new double[2][2];

public square (String nname, double nx, double ny, double [][]nu) {
name = nname;
x = nx;
y = ny;
u = nu;
}
public void display () {
System.out.println ("Name : " + name + "\r\nX: "x" \r\nY: " + y);
}
}

class squares
{
public static void main(String[] args)
{
double u1[][] = new double[2][2];
square s1 = new square ("Square1", 0.11, 0.22, u1);
square s2 = new square ("Square2", 0.33, 0.44, u1);
Vector v = new Vector();
v.addElement (s1);
v.addElement (s2);

v.addElement (new square("new square", 0.112, 2.11, u1));
for (int k=0;k < v.size() ;k++ )
{
square temp;
temp = (square)v.elementAt(k);
temp.display();

double result = temp.x + temp.y;
System.out.println ("Result : "+ result);
}
}
}


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SECOND VERSION

import java.util.*;

class square
{
String name;
public double x;
public double y;
double u[][] = new double[2][2];

public square (String nname, double nx, double ny, double [][]nu) {
name = nname;
x = nx;
y = ny;
u = nu;
}
public void display () {
System.out.println ("Name : " + name + "\r\nX: "x" \r\nY: " + y);
}
}

class squares
{
public static void main(String[] args)
{
double u1[][] = new double[2][2];
square s1 = new square ("Square1", 0.11, 0.22, u1);
square s2 = new square ("Square2", 0.33, 0.44, u1);
Vector v = new Vector();
v.addElement (s1);
v.addElement (s2);

v.addElement (new square("new square", 0.112, 2.11, u1));
for (int k=0;k < v.size() ;k++ )
{
v.elementAt(k).display();

double result = v.elementAt(k).x + v.elementAt(k).y;
System.out.println ("Result : "+ result);
}
}
}

 

Posts:4,130
Registered: 3/31/99
Re: Help please: Vector, its components and the variables of its components  
Aug 1, 2004 6:02 AM (reply 1 of 1)



 
For future posts, ALWAYS include the full, exact error messages and please use the code tags as descibed in the Formatting Help (a link just above the text area when you post) when posting code. Also, it is a convention for class names to start with a captial letter. Following this convention will make helping you easier.

Anyway, one of the errors I get is
C:\Documents and Settings\wxw1028\MyClasses\square.java:37: cannot resolve symbol
symbol : method display ()
location: class java.lang.Object
v.elementAt(k).display();
This error tells you that compiler can not find a method named display() in the class java.lang.Object. The first part of this line, v.elementAt(k) returns a java.lang.Object according to the API documentation, then the code tries to use the display() method with the returned Object. Even though the Object is a square, you can not call methods defined in square using an Object reference - only Object methods can be called by an Object reference.

While you know that the Object is actually a square, the compiler does not. So you must explicitly cast the Object to a square. Java allows this because the Object is a square.
((square) v.elementAt(k)).display();
Similar changes to other lines will clear up this problem.
 
This topic has 1 reply on 1 page.