• 100% Satisfaction Guarantee
Eric M., Tutor
Category: Homework
Satisfied Customers: 2198
Experience:  9 years of tutoring experience, Bachelor's and Master's in Computing, pursuing PhD
29802289
Eric M. is online now

# Hi, Im having serious problems trying to understand this

Hi,
I'm having serious problems trying to understand this week HW and to be worst my Instructor doesn't provide any guidance. Any help will be appreciated.

Section 10.9 (pages 386 -- 388) shows how to make a StackOfIntegers class using arrays. Modify listing 10.8 to make a generic stack of objects using arrays.
Note 1: The main constructor needs to dynamically allocate an array. To use generic objects with dynamic array allocation, you need to allocate an array of type Object and then cast the newed array to an array of the generic type.
Note 2: The main constructor needs to initialize size to 0. (Listing 10.8 is missing this necessary statement.)

Exercise 10.8
public class StackOfIntegers {
private int[] elements;
private int size;
public static final int DEFAULT_CAPACITY = 16;

/** Construct a stack with the default capacity 16 */
public StackOfIntegers() {
this(DEFAULT_CAPACITY);
}

/** Construct a stack with the specified maximum capacity */
public StackOfIntegers(int capacity) {
elements = new int[capacity];
}

/** Push a new integer into the top of the stack */
public void push(int value) {
if (size >= elements.length) {
int[] temp = new int[elements.length * 2];
System.arraycopy(elements, 0, temp, 0, elements.length);
elements = temp;
}

elements[size++] = value;
}

/** Return and remove the top element from the stack */
public int pop() {
return elements[--size];
}

/** Return the top element from the stack */
public int peek() {
return elements[size - 1];
}

/** Test whether the stack is empty */
public boolean empty() {
return size == 0;
}

/** Return the number of elements in the stack */
public int getSize() {
return size;
}
}

You're right, the directions for this assignment are a bit confusing -- I've been coding for 10 years and I'm not 100% sure what your teacher is asking. As best as I can tell, however, this is what you're looking for: (link)

Thanks! If this answer is acceptable, please remember to click 'Accept' or give positive feedback when prompted. If you need further assistance before this is deemed acceptable, please don't hesitate to ask for more detail. Thanks again!
Customer: replied 3 years ago.

For Erick M.

Thanks for your immediate response Eric!. Question? I noticed that you put a default capacity of 0 when the original code is 16. Also, as per the instructor guidance is the size that should be initialized to 0. I might be confusing between the size and capacity, but I appreciate any clarification.

Sure thing!

You're absolutely right -- I overlooked that the file was distinguishing between capacity and size. In that case, you're right, the initialization line should return to 16, and instead, the following bolded line should be added to the second constructor:

public StackOfObjects(int capacity) {
elements = new Object[capacity];
size=0;
}

Thanks!
Customer: replied 3 years ago.

For Erick,

Do you know why every time I'm trying to run my program in eclipse, the following message is showing "Selection does not contain a main type"?

Hmm, where is that line showing? Can you copy/paste the entire error message if it's an error?

Also, can you copy/paste the main method that uses this class? For example, for me I'm testing it with:

StackOfObjects soo=new StackOfObjects();
soo.push(new String());

That's in my public static void main method.
Customer: replied 3 years ago.

I'm sorry, it was your program that I was trying to run. Here is the window

So this need a test file?

Oh, I'm sorry, I should have caught that. So, this StackOfObjects file is just a file that is used by another class. In order to run and test it, you need something with a public static void main method.

For example, here is mine: (link)

Put those two files in the same folder, compile both, and run this one (not the StackOfObjects one).

Basically, every Java program needs a 'main' method -- this is where the program actually starts. So, that error is saying that there currently is not a 'main' method.
Customer: replied 3 years ago.

For Eric M.

I received feed back from the instructor. It looks like I need to create a generic class that accepts a stack of Strings, Objects, integers. Any help will be appreciated.

Customer: replied 3 years ago.
Received feedback from instructor that mentions there was not a Generic Stack in the code where each element is of the same desired type.

Hmm, I'm having a little trouble parsing what the instructor is saying -- I've tested this out, and it takes Strings, Objects, and integers. The code I use to test it is this:

StackOfObjects soo=new StackOfObjects();
soo.push(new String());
soo.push(5);
soo.push(new Object());

The first line initializes the Stack, the second adds a String, the third adds an Integer, and the fourth adds an Object.

I'm re-uploading the file in which I made these changes -- I don't think I've made any changes since the last time I uploaded it, but I could be wrong. Here it is: (link)

I may have changed line 29 -- formerly, it said:

public void push(int value) {

which means it would only take an integer, but it's now:

public void push(Object value) {

I believe I changed that before uploading the previous file, but I'm not 100% certain -- I know I forgot that change initially. That change is necessary to make it accept Objects instead of just ints, though, so that could be what your instructor is referencing if I did make that mistake.

If not, I would tell your instructor that the class successfully lets you add ints, Strings, and Objects (as given by the above code), and ask how this differs from what he's looking for. I'll be happy to help further, I'm just trying to figure out what it is that he's looking for.

Thanks again!

Customer: replied 3 years ago.

I thinks this might be how the class loos like to make it generic:

public class StackOfObjects <T> {

private T elements;

private int size;

But I'm having problems to make it work in the code.

here is the full feedback:

The problem is that the same stack can contain different types of objects. You can push a String, an Integer, a Double, or an object of any class whatsoever (including a stack!) all onto the same stack. What you want is a generic stack where each element is of the same desired type. You could use the same class to create a stack of Strings, a stack of Integers, or a stack of objects of some class you wrote. For example, if you used the class to create a stack of Strings, then the only thing you could put on the stack are Strings (or objects from a subclass of String). If you tried to push an Integer onto this stack, the compiler would complain about incompatible types.

Customer: replied 3 years ago.
Relist: Other.
Eric M, any luck?
Oh I see! That makes more sense. In my opinion, he should have distinguished whether the class itself as a whole implements generics or if the individual elements are generic, but that may have been provided by the context of the other things covered in the class at the time.

I'm not at the computer that has my compiler right now, but I'll take a look at this by the end of the day if that's alright. I've never done what he's suggesting, but I'm very curious to figure it out!
Customer: replied 3 years ago.

Yeah, I agree. If you can take a look later on will be great. thanks!

You need to spend \$3 to view this post. Add Funds to your account and buy credits.
Customer: replied 3 years ago.

did you make any changes in the main code? I'm getting a message that my object is not generic

You need to spend \$3 to view this post. Add Funds to your account and buy credits.
Customer: replied 3 years ago.

Hey Eric,

you already send this. my question is : do you made any changes from the original code that allows you to test it with your new class?

You need to spend \$3 to view this post. Add Funds to your account and buy credits.
Customer: replied 3 years ago.

Did you change anything from the main Stack of objects file. When I'm testing your code I'm getting

" The type StackOfObjects is not generic; it cannot be parameterized with arguments <String>"

package stackofobjects;

public class StackOfObjects {

private Object[] elements;

private int size;

/** Construct a stack with the default capacity 16 */

public StackOfObjects() {

this(16);

}

/** Construct a stack with the specified maximum capacity
intialize size to 0*/

public StackOfObjects(int capacity) {

elements = new Object[capacity];
size =0;
}

/** Push a new integer into the top of the stack */

public void push(String value) {

if (size >= elements.length) {

Object[] temp = new Object[elements.length * 2];

System.arraycopy(elements, 0, temp, 0, elements.length);

elements = temp;

}

elements[size++] = value;

}

/** Return and remove the top element from the stack */

public Object pop() {

return elements[--size];

}

/** Return the top element from the stack */

public Object peek() {

return elements[size - 1];

}

/** Test whether the stack is empty */

public boolean empty() {

return size == 0;

}

/** Return the number of elements in the stack */

public int getSize() {

return size;
}

}