-
writing an array class
public class Vector {
private double [] data;
// Creates the Vector
public Vector ( int N )
{
int[] data = new int [N];
}
public double getElement ( int index )
{
return data [index];
}
public void setElement (int index, double value)
{
data [index] = value;
}
Is the coding in the getter and setter methods correct for what was asked?
Also how would i fill the vector with random numbers between 0 and 10. I know could use the Math.random function and say something like Math.random ( ) * 10 ...but how do i apply that to filling the vector.
-
Quote:
for what was asked?
Where is the question?
Vector is the name of a java class. Could you change the name?
You need to move the definition of the array outside of the constructor.
You have two variables named data.
-
I'm asking are my Setter and Getter methods correct. Also In my class I was told to put it there..what else would go in the constructor.... heres the first part of my assignment
1)
Instance variables:
/* 1-dimensional array of size N (an N-element vector). */
private double[] data;
2)
Constructor:
public Vector ( int N )
{
/* allocates storage for the N-element vector */
}
3)
Get/Set functions:
public double getElement ( int index )
{
/* returns the vector element with index index. */
}
public void setElement ( int index, double value )
{
/* sets the vector element with index index to value. */
}
-
In your constructor there is variable hiding is take place. I mean you have two arrays name data in two different data types. One is a double type and the other is int type.
In that scene you don't have initialize data array of type double. So if you run and call the getElement() you have a null pointer exception.
Basically definitions of two methods is fine.
-
I guess I don't understand what your saying.... with those requirements how would i fix it or change it?
-
To test your program after you get the compile errors corrected, add a main() method that uses the methods of the class.
Create an instance of the class, add a value to it(usedset) and the get that value and display it with println().
As you have problems copy the console with the error messages and paste them here.