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.
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.
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.
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];
}
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.
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.
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!!