Design and write a complete test program to test if the MyArrayList class in Listing 24.3 meets
Question:
Design and write a complete test program to test if the MyArrayList class in Listing 24.3 meets all requirements.
Listing
Transcribed Image Text:
1 public class MyArrayList
1 public class MyArrayList extends MyAbstractList { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[]) new Object[INITIAL_CAPACITY]; 4 2 3 5 /** Create a default list */ public MyArrayList() { 8 /** Create a list from an array of objects */ public MyArrayList(E[] objects) { for (int i = 0; i < objects.length; i++) add(objects[i]); // Warning: don't use super(objects)! 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @Override /** Add a new element at the specified index */ public void add(int index, E e) { ensureCapacity(); // Move the elements to the right after the specified index for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; // Insert new element to data[index] data[index] - e; // Increase size by 1 size++; 26 27 28 29 /** Create a new larger array, double the current size + 1 */ private void ensureCapacity() { if (size >= data.length) { E[] newData = (E[])(new Object[size * 2 + 1]); System.arraycopy(data, 0, newData, 0, size); data = 30 31 32 33 34 35 newData; 36 37 38 39 @Override /** Clear the list */ public void clear() { data = (E[])new Object[INITIAL_CAPACITY]; size = 0; 40 41 42 43 44 45 @Override /** Return true if this list contains the element */ public boolean contains(E e) { for (int i = 0; i < size; i++) if (e.equals(data[i])) return true; 46 47 48 49 50 return false; 51 52 53 GOverride /** Return the element at the specified index */ public E get(int index) { checkIndex (index); return data[index]; 54 55 56 57 58 59 private void checkIndex(int index) { if (index < 0 || index >= size) throw new IndexOut0fBoundsException ("index " + index + " out of bounds"); 60 61 62 63 64 65 @Override /** Return the index of the first matching element * in this list. Return -1 if no match. */ public int index0f(E e) { for (int i = 0; i < size; i++) if (e.equals(data[i])) return i; 66 67 68 69 70 71 return -1; 72 73 74 @Override /** Return the index of the last matching element in this list. Return -1 if no match. */ public int lastIndex0f (E e) { for (int i = size - 1; i >= 0; i--) if (e.equals(data[i])) return i; 75 76 77 78 79 80 return -1; 81 82 83 @Override /** Remove the element at the specified position * in this list. Shift any subsequent elements to the left. Return the element that was removed from the list. */ public E remove(int index) { checkIndex (index); 84 85 86 87 88 E e - data[index]; 89 90 // Shift data to the left for (int j = index; j < size - 1; j++) data[j] = data[j + 1]; 91 92 93 94 data[size - 1] = null; // This element is now null 95 96 // Decrement size size--; 97 98
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 50% (8 reviews)
Program Plan Create an ArrayListTesting class for test the MyArrayList class This MyArrayList class extends MyAbstractList class and that class implements MyList interface These three classes are refe...View the full answer
Answered By
Gilbert Chesire
I am a diligent writer who understands the writing conventions used in the industry and with the expertise to produce high quality papers at all times. I love to write plagiarism free work with which the grammar flows perfectly. I write both academics and articles with a lot of enthusiasm. I am always determined to put the interests of my customers before mine so as to build a cohesive environment where we can benefit from each other. I value all my clients and I pay them back by delivering the quality of work they yearn to get.
4.80+
14+ Reviews
49+ 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
-
Design and write a complete test program to test if the MyLinkedList class in Listing 24.6 meets all requirements. Listing 1 public class MyLinkedList extends MyAbstractlist { private Node head,...
-
Design and write a complete test program to test if the AVLTree class in Listing 26.4 meets all requirements. Listing 1 public class TestAVLTree { public static void main(String[] args) { // Create...
-
Design and write a complete test program to test if the BST class in Listing 25.5 meets all requirements. Listing 1 public class BST 2 extends AbstractTree { protected TreeNode root; protected int...
-
Another liability that employers have is that of unemployment insurance. Class, let's talk about this a little more. At what levels are unemployment liabilities incurred? What are some additional...
-
A subset S of a linear space is a convex cone if and only if x + y S for every x, y S and x, +
-
Explain why bone remodeling is important, and give its steps.
-
CMS is a claims processing company in Mobile, Alabama. Chastity Jones, a black woman, completed an online employment application for a customer service position with CMS. Jones interviewed with a...
-
The Jimenez Corporation's forecasted 2011 financial statements follow, along with some industry average ratios. a. Calculate Jimenez's 2011 forecasted ratios, compare them with the industry average...
-
How would a data analyst evaluate a conference call to get a feel for the sentiment of management?
-
A company has fixed production overhead costs totalling S20,000. The normal production level is 1,000 units per year, yielding a standard fixed overhead rate of $20 per unit. If the actual production...
-
Add the following new methods in?BST. /** Displays the nodes in a breadth-first traversal */ public void breadthFirstTraversal() /** Returns the height of this binary tree */ public int height()
-
Define an iterator class named PrimeIterator for iterating prime numbers. The constructor takes an argument that specifies the limit of the maximum prime number. For example, new PrimeIterator(23302)...
-
A 650- and a 2200- resistor are connected in series with a 12-V battery. What is the voltage across the 2200- resistor?
-
Discuss the concept of "too big to fail" banks and the potential systemic risks they pose to the global economy. What measures can regulators implement to prevent future financial crises and ensure...
-
Cottrell Lamps reported a net income of $530,000 for the current year. In addition Cottrell Lamps had 50,000 common shares outstanding throughout the year, an income tax rate of 34%; and has $100,000...
-
Under what circumstances does an agency acquire assets on behalf of another entity, encapsulating the concept of agency acquisition within the domain of commercial transactions?
-
Viewed the video and read the articles: Briefly summarize the 2 articles that are found in. Discuss 3 things that we learned about supply chains that people did not know prior to this activity....
-
ww R 1. Given the circuit below, solve for G(s)= and G(s)=- If R = 10, C = 1F, L = 1H v(t) mm L 1+ i(t) v(t) 2. Given the circuit below, solve for G (s)=- 192 VR(S) V(s) C W Vo(s) v (s) 1 H 0000 1 F...
-
On October 31, 2014, Lisik Company had a cash balance per books of $8,985. The bank statement on that date showed a balance of $10,173. A comparison of the statement with the Cash account revealed...
-
A heat engine has a heat input of 3 Ã 104 Btu/h and a thermal efficiency of 40 percent. Calculate the power it will produce, in hp. Source 3 x 10 Btu/h 40% HE Sink
-
In the TCP/IP protocol suite, what are the identical objects at the sender and the receiver sites when we think about the logical connection at the application layer?
-
Assume that the number of hosts connected to the Internet at year 2010 is five hundred million. If the number of hosts increases only 20 percent per year, what is the number of hosts in year 2020?
-
A router connects three links (networks). How many of each of the following layers can the router be involved with? a. Physical layer b. Data-link layer c. Network layer
-
Research the different processes involved during the planning phase of a project. Identify the tasks and techniques associated with each of these processes. Prepare diagram where you relate...
-
How do cultural norms and values intersect with social structure, influencing patterns of behavior, identity formation, and societal organization ?
-
We manufacture breakfast cereal at our factory in Toad Suck, AR. Our current selling price is $2.40 per box, F.O.B. our shipping dock (our customers pay shipping expenses). Our gross profit margin is...
Study smarter with the SolutionInn App