Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. In SuperAndSub, what do the default constructors for each of the classes do? 2. In SuperAndSub, when the SubClass object is created which class

1. In SuperAndSub, what do the default constructors for each of the classes do?

2. In SuperAndSub, when the SubClass object is created which class constructor completes execution first? Why do you think that is the case? 3. What was the hardest part of this lab for you and why was it hard.

image text in transcribed

image text in transcribed

image text in transcribed

Arraylisttester;

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

3 Making our ArrayList a Generic Class You will be surprised at how easy this will be. 1. The first thing we have to do is add some code to the line where the class is de- clared public class ArrayList > What does it mean? E is our generic data type and this says that it has to have a compareTo() method so that we can do things like search or sort. The super E> says that E or a subclass of E has to implement Comparable. 2. Next we have to change the data type of the array. Java does not allow generic arrays, but remember that ALL classes extend the Java Object class. It is the ultimate superclass. And remember that instances of a subclass are instances of their superclass. Make the array an array of Java Objects. Simply replace int[] with Object[]. 3. Scroll down slowly and you will see that in the parameterized constructor, an array of int is being created. Again change int[capacity) to Object (capacity]. 4. Scroll down to the add() method. Its parameter is an element to be added to the array, so we have to change its data type, but this time we will use E public boolean add (E element) 5. Scroll down the the next add() method and change the data type of element to E. 6. Scroll down to clear() and change the array declaration as you have done before. 1 7. Scroll down to contains() and change the data type of element to E. Now we have to change the way the equivalence is being tested in the while loop condition. Remember that whatever our data type is, it must implement Comparable. Also remember that compareTo() returns a positive number, negative number or 0. So what we want is for the returned value to be something besides a 0. Since the array is technically full of Object objects, we need to cast it. Change the second condition in the while loop to this: element.compareTo((E) array[i]) != 0 8. The next method that needs to change is get(). It needs to return a type E not an int. The array element being returned must be cast to an E. And the return in the else should return null. 9. Next is indexOf(). An index is an int, but our element and array contents are not. Can you make these changes without more instruction? 10. Remove() needs to return a type E. See if you can make the rest of the angry red lines go away. 11. Next is set() which also needs to return a type E. 10. Remove() needs to return a type E. See if you can make the rest of the angry red lines go away. 11. Next is set() which also needs to return a type E. 12. The next one we need to change is the toArray() method. This one is easy. In this method, every instance of int should be replaced with E. Remember you have to cast the array elements 13. Next is trim ToSize(). Again we can't create an array of E so the array will have to be Object. 14. Last, but not least is the toString() for the ArrayList. Have array[i] explicitly call its toString() method, but then cast that to E. That way it will use the toString() for whatever class E ends up being. If we don't do this, it will use the toString() in the Object class. 4. Update the Tester Program Our Arraylist declarations now need to include a data type in angle brackets just like a Java ArrayList would. Change them both so that they look like this: (one named a and one named b) ArrayList a = new ArrayList(); Then uncomment the section of code at the bottom of main and run it. 5 Superclass - Subclass Interactions On Brightspace is a second project. Download and unzip SuperAndSub.zip. First, please look at the AbstractSuper class. It doesn't do anything exciting, but just see what it has. Do the same with ConcreteSubSuper, and Subclass. Pay attention to the toString() methods in each class. 2 Now look at the driver SuperAndSub class. Run it. Make sure that you understand where each line of output is coming from and why. The constructor chaining is an important concept. You will have to use the tabs at the top to switch from class to class and then use the navigator pane on the left to go directly to a method. Try to trace the program line by line. This is a testable concept. Please notice that line feeds are only introduced in toString() methods if they are some- where in the string to be returned. Don't put line feeds at the end of a string to be returned. Let the driver or calling module take care of that by using either a print or println. 19 public class ArrayList { 20 /** 21 * the length of the underlying array; 22 */ 23 private int capacity; 24 /** 25 * the data will be stored in a static array 26 */ 27 private int[] array; 28 /** 29 * the size is the actual number of elements in the array 30 */ 31 private int size; 32 33 /** 34 * Constructs an empty list with an initial capacity of ten 35 */ 36 public ArrayList() { 37 this(10); 38 } 39 40 /** 41 Constructs an empty list with the specified initial capacity. 42 43 * @param capacity the initial capacity of the arrayList 44 */ 45 public ArrayList(int capacity) { 46 this.capacity = capacity; 47 array = new int[capacity]; 48 size = 0; 49 } 50 /** 51 * to make this more like the Java ArrayList we need this method. 52 * @return the size of the list. 53 */ 54 public int size() { 55 return size; 56 } 57 /* 58 * private validIndex returns true if the index is valid 59 * and false otherwise 60 */ 61 private boolean isValidIndex(int index) { 62 return (index >= 0 && index = 0 && index index; i--) { array[i] = array[i - 1]; } array[index] = element; size++; return true; } 96 97 98 99 100 101 102 103 public int get(int index) { if (isValidIndex(index)) { return array[index]; } else { return -1; } } /** * Finds the element specified and returns the index if found or -1 if not * found. performs a standard linear search if the array contains the * element. * 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 * @param element the element to search for * @return the index of the element or -1 if the element is in in the list */ public int indexOf(int element) { int index = 0; if (contains (element)) { while (index

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions