• 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

### Resolved Question:

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;
}
}
Submitted: 2 years ago.
Category: Homework
Expert:  Eric M. replied 2 years ago.

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

Expert:  Eric M. replied 2 years ago.
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 2 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"?

Expert:  Eric M. replied 2 years ago.
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 2 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?

Expert:  Eric M. replied 2 years ago.
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.
Eric M., Tutor
Category: Homework
Satisfied Customers: 2198
Experience: 9 years of tutoring experience, Bachelor's and Master's in Computing, pursuing PhD
Customer: replied 2 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 2 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.
Expert:  Eric M. replied 2 years ago.

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 2 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 2 years ago.
Relist: Other.
Eric M, any luck?
Expert:  Eric M. replied 2 years ago.
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 2 years ago.

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

Expert:  Eric M. replied 2 years ago.

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

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

Expert:  Eric M. replied 2 years ago.

You need to spend \$3 to view this post. Add Funds to your account and buy credits.
Customer: replied 2 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?

Expert:  Eric M. replied 2 years ago.

You need to spend \$3 to view this post. Add Funds to your account and buy credits.
Customer: replied 2 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;
}

}

Expert:  Eric M. replied 2 years ago.

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

No worries, it works. thank you!

 Attachments are only available to registered users.Register Here Attachments are only available to registered users.Register Here Attachments are only available to registered users.Register Here View Full Image Attachments are only available to registered users.Register Here

Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.

### What Customers are Saying:

• Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
< Previous | Next >
• Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
• This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
• Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
• I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
• Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
• Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
• He answered my question promptly and gave me accurate, detailed information. If all of your experts are half as good, you have a great thing going here. Diane Dallas, TX

• ### LogicPro

#### Satisfied Customers:

4925
Expert in Java C++ C C# VB Javascript Design SQL HTML
< Last | Next >

### LogicPro

#### Satisfied Customers:

4925
Expert in Java C++ C C# VB Javascript Design SQL HTML

### Manal Elkhoshkhany

#### Satisfied Customers:

4538
More than 5000 online tutoring sessions.

### Linda_us

#### Satisfied Customers:

3138
Post Graduate Diploma in Management (MBA)

### Chris M.

#### Satisfied Customers:

2602
Master's Degree, strong math and writing skills, experience in one-on-one tutoring (college English)

### F. Naz

#### Satisfied Customers:

2126
Experience with chartered accountancy

### Bizhelp

#### Satisfied Customers:

1887
Bachelors Degree and CPA with Accounting work experience