array object class problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: array object class problem

  1. #1
    Join Date
    Jul 2004
    Posts
    92

    array object class problem

    hi there. i am trying to figure out what i am doing wrong here.

    i have a class called Bag. in this class, the constructor is to create an array of type int called "x".

    then i have a class called main that will call provide the interface and testing for the Bag class.

    i am not declaring my Bag object correctly, though . . . i think.

    also, do i need any packages imported . . . ? could that be what i am doing wrong here?

    public class Bag{
    private int i;
    private int [] x;
    private int top_pos = 0;

    public Bag (){ //def. const.
    i = 10;
    int [] x = new int [i];
    }

    // method to add values to x
    public void add (int val){
    x[top_pos] = val;
    top_pos++;
    }


    and then the class main is as follows . . .

    public class main {
    public static void main(String[] args) {
    Bag b = new Bag();
    b.add(5);

    }

    i tried making other method too. but as with the add method, when i call them, i get a null pointer exception.

    i can't find an example of how to declare an array object or of an array constructor. have any?

    thanks
    crq
    }

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    in your Bag constructor, change the array initialization to this:

    x = new int[i];

    instead of

    int [] x = new int [i];


    when you redeclare x in the constructor, that x is local to it. Since the x variable for the class hasn't actually been initialized yet, you get a null pointer exception because the array has no elements.

  3. #3
    Join Date
    Jul 2004
    Posts
    92
    what should my default constructor and my constructor with perameters do? do i even have to have a constructor with perameters? (seems like i always see two constructors).

    i am thinking that the default constructor should make an array with a specific length and initialize everything to 0. or should it just make the array and leave it empty?

    i am thinking that the constructor with perameters should get input from the user about the length of the array and create one of that length. but should i initialize the array elements to zero? leave them empty?

    my methods consist of add (to add elements), remove (to remove an element), search for (boolean, is element present), and toString . . . maybe a few more.

    so far i ALSO have a class variable, i, that is for the length of the array and a class variable, top_pos, that represents the last empty (or would it be better to have it represent the last occupied?) element.

    thanks
    crq

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    what should my default constructor and my constructor with perameters do? do i even have to have a constructor with perameters? (seems like i always see two constructors).
    Well what they do is entirely dependant on the specification you've been given. Without knowing the specification I think it would be sensible that the default constructor makes a bag of a default size with everything initialised to 0 since they're ints. Ints are always initialised to 0 as far as I know, so you shouldn't need to make them 0 manually.

    As for the constructor with paramters, there are two sensible options I can think of. First as you said, let the user specify a length and make an array of that length with everything set to 0. Second, let the user pass an array of ints and use that one. Again, see what the specification says.

    You never need a constructor with paramters, you can always make a class without one. In some cases the class wouldn't work very well without a constructor with paramters though.

    so far i ALSO have a class variable, i, that is for the length of the array and a class variable, top_pos, that represents the last empty (or would it be better to have it represent the last occupied?) element.
    I would get rid of the variable i, you don't need it. The array has a length stored in it, x.length. Unless the specification specifically says you should store it in a seperate variable of course, you should always follow the specification to the letter.

    As for the other variable, I guess that depends on what it will be used for. What will you be using the most in your class, looking at the last occupied position, or putting things in the first empty position? Whichever one you will be doing most use this variable for that job.

  5. #5
    Join Date
    Jul 2004
    Posts
    92
    good point . . . x.length. forgot about that one.

    well, and my instructions are pretty wide open . . . i think it was intended to be that way. i am to make a bag class which consists of an array with methods and a main class that will test it and implement the Bag class.

    we are to have an add mehtod, a remove method, a find method, a number of items method, and a display (toString) method.

    basically, that is all of our instructions.

    the skeleton code DOES include a class value called int top_pos. i THINK it is supposed to represent the last element in the array that HAS a value . . . but i seem to be doing better by thinking of it as the first element in the array WITHOUT a value . . .

    public class Bag {
    private int i=10;
    private int [] x;
    private int top_pos;

    public Bag (){
    x = new int [i];
    }


    this is my class declaration and default constructor so far.

    but yes, i have too many length, counter, and place holder variables. i am not sure what to do with this default constructor, though. you can't ever ASSIGN x.length a value, though, right?

    constructor with perameter is . . . . . . .
    public Bag (int num){
    x = new int [num];
    }

    look ok?

    and my toString . . .

    public String toString (){
    String tmpStr = "";
    for (i=0; i<10; i++){
    tmpStr += "" +x[i]+ " ";
    }
    return tmpStr;
    }

    so basically i am using the class variable i for many things in this program.

    one last thing, how do you remove a value and then close up the hole again . . . i asssume that is what i will be using my top_pos variable for also.

    thanks
    crq

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    the skeleton code DOES include a class value called int top_pos. i THINK it is supposed to represent the last element in the array that HAS a value . . . but i seem to be doing better by thinking of it as the first element in the array WITHOUT a value . . .
    I would think the most useful option would be to represent the first position without a value. That way you can use it in a for loop to loop through all the existing values and you can use it to add something straight into the first empty position, all without having to alter it.

    you can't ever ASSIGN x.length a value, though, right?
    Nope, x.length is constant. The only way you can ever assign anything to it is when you initialise an array.

    public String toString (){
    String tmpStr = "";
    for (i=0; i<10; i++){
    tmpStr += "" +x[i]+ " ";
    }
    return tmpStr;
    }

    so basically i am using the class variable i for many things in this program.
    The i you use here is not the same i as the classvariable. The i in the for loop is declared and initialised in the for loop and so is local to the for loop. The for loop would work even without the class variable named i

    Oh, and you shouldn't put i<10 into the for loop. What happens if they create a bag with only 5 spaces, you'll get an out of bounds exception thrown here. You should use x.length to control the loop

    one last thing, how do you remove a value and then close up the hole again . . . i asssume that is what i will be using my top_pos variable for also.
    By close up the hole, do you mean move everything up by one and leave the empty space at the end? If so, you would just loop through the array moving everything after the deleted index up one.

  7. #7
    Join Date
    Jul 2004
    Posts
    92
    thanks mike.

    yeah, i want to be able to remove an item from an array and then shift everything in the array forward. i think i have done that . . .

    might should go back, though and set that last array element that is empty to zero . . . don't know, though. zero is an integer too. i will just let the value top_pos keep up with what is shown from the array. i don't know how to TRULY empty an array element.

    if you would, though, could you look over the Bag class methods that i have here? everything works ok with the testing that i have done, but i am afraid the guy that is grading it gets a bit crazy on the style issues. so if you could check to be sure i have used a FOR when i should or a WHILE when i should . . . if i should catch any exceptions . . . .import any packages . . . consolodate some variables . . . (maybe the x.length . . . keep forgetting to use that . . . ) . . . anything else you can think of that will clean it up a bit or add some bells and whistles!!

    thanks
    crq

  8. #8
    Join Date
    Jul 2004
    Posts
    92
    guess i should attach the code!!
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center