Revise the GenericStack class in Listing 19.1 to implement it using an array rather than an ArrayList.
Question:
Revise the GenericStack class in Listing 19.1 to implement it using an array rather than an ArrayList. You should check the array size before adding a new element to the stack. If the array is full, create a new array that doubles the current array size and copy the elements from the current array to the new array.
Listing
Transcribed Image Text:
1 public class GenericStack
1 public class GenericStack { private java.util.ArrayList list = new java.util.ArrayList<>(); 3 public int getSize() { return list.size(); 4. 5 6. public E peek() { return list.get (getSize() - 1); 10 11 public void push(E o) { list.add(o); 12 13 14 15 16 17 18 19 20 public E pop() { E o = list.get(getSize() - 1); list.remove(getSize() - 1); return o; 21 22 public boolean isEmpty() { return list.isEmpty(); 23 24 25 @0verride public String toString() { return "stack: 26 27 + list.toString(); 28 29 30 }
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 83% (6 reviews)
Program Plan Create a class named GenericStck that implements the stack operations using an array of objects o Implement a default constructor creates ...View the full answer
Answered By
Nazrin Ziad
I am a post graduate in Zoology with specialization in Entomology.I also have a Bachelor degree in Education.I posess more than 10 years of teaching as well as tutoring experience.I have done a project on histopathological analysis on alcohol treated liver of Albino Mice.
I can deal with every field under Biology from basic to advanced level.I can also guide you for your project works related to biological subjects other than tutoring.You can also seek my help for cracking competitive exams with biology as one of the subjects.
3.30+
2+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Redesign and implement the Rational class in Listing 13.13 using BigInteger for the numerator and denominator. 1 public class Rectangle extends Geometricobject { 3 }
-
The heap presented in the text is also known as a max-heap, in which each node is greater than or equal to any of its children. A min-heap is a heap in which each node is less than or equal to any of...
-
Revise the Course class as follows:? The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the...
-
What risks accompany a standby credit letter for (a) the issuer and (b) the beneficiary?
-
Let a, b be elements in a lattice L with a b. Then the subsets (b), (a) and [a, b] are sublattices. The sublattices are complete if L is complete.
-
Identify whether each of the following transactions will take place in an input market or in an output market, and whether firms or households are demanding the good or service, or supplying the good...
-
By using six factor formula for \(k\), derive the Eqs. (7.93), (7.94) of Section 7.7.1. dkoo dp= k MB dM dB 8 + (7.93) 1+M B M B2
-
American Fabrics has budgeted overhead costs of $990,000. It has allocated overhead on a plantwide basis to its two products (wool and cotton) using direct labor hours which are estimated to be...
-
Kevin made two investments over the past two years. His first investment wasabaseball card thatcost$50.00,whichhe sold three months later for$55.00.The second wasashare of stockinastart-upcompany....
-
During 2020, the following transactions were recorded by the Port Hudson Community Hospital, a private sector not-for-profit institution. 1. Gross charges for patient services, all charged to Patient...
-
In Listing 19.1, GenericStack is implemented using composition. Define a new stack class that extends ArrayList. Draw the UML diagram for the classes and then implement GenericStack. Write a test...
-
Revise Programming Exercise 18.38 to move the tree to where the mouse is dragged. Write a program to display a recursive tree as shown in Figure 18.20. 1 Enter an order: Enter an order: Enter an...
-
A fair coin is tossed. If a head turns up, a fair die is tossed; if a tail turns up, two fair dice are tossed. What is the probability that the face (or the sum of the faces) showing on the die (or...
-
Hyperion, Inc. currently sells its latest high-speed color printer, the Hyper 500, for $350. Its cost of goods sold for the Hyper 500 is $150 per unit, and this year's sales are 10,000 units....
-
Suppose that the cost of producing n pairs of shoes is given by the expression C(n)=30+5n. The average cost of producing a pair of shoes is given by A(n)=C(n)/n. What is the amount n for which the...
-
What can you say about the difference in R 2 where R 2 in a simple linear regression is 56% and in multiple regression R 2 is 80%
-
At one point in their orbits, Earth and Mars are separated by a distance of 8 . 1 4 x 1 0 ^ 7 km . If the mass of the Earth is 5 . 9 7 x 1 0 ^ 2 4 kg and the mass of Mars is 6 . 4 2 x 1 0 ^ 2 3 kg ,...
-
make an organizational chart that shows the leadership positions of WALMART beginning with the chief executive officer/owner at the top and continuing downward to first-line management positions or...
-
Kreter, Inc. earned net income of $300,000 last year. This year it wants to earn net income of $450,000. The company's fixed costs are expected to be $300,000, and variable costs are expected to be...
-
Using Gauss-Jordan elimination, invert this matrix ONLY 0 0 0 0 1
-
Show that the summation n i=1 logi is (nlogn).
-
An array A contains n1 unique integers in the range [0,n1], that is, there is one number from this range that is not in A. Design an O(n)-time algorithm for finding that number. You are only allowed...
-
Bob built a website and gave the URL only to his n friends, which he numbered from 1 to n. He told friend number i that he/she can visit the website at most i times. Now Bob has a counter, C, keeping...
-
Sapsora Company uses ROI to measure the performance of its operating divisions and to reward division managers. A summary of the annual reports from two divisions is shown as follows. The companys...
-
The company applies overhead cost to jobs on the basis of machine-hours. For the current year, the company estimated that it would work 42,000 machine-hours and incur $191,100 in manufacturing...
-
Explain performance management and the roles it plays within an organization. Describe a performance appraisal process and assess the importance of performance appraisals in an effective performance...
Study smarter with the SolutionInn App