Results 1 to 6 of 6
  1. #1
    wardd85 is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  3. #3
    wardd85 is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

    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. */
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  5. #5
    wardd85 is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

    I guess I don't understand what your saying.... with those requirements how would i fix it or change it?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

Similar Threads

  1. Writing a countdown array to a file.
    By kewlgeye in forum New To Java
    Replies: 6
    Last Post: 05-25-2008, 06:09 AM
  2. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •