Question
package hw3; public class FancyArray { private T[] array; private int size; public FancyArray() { this(10); } public FancyArray(int capacity) { if (capacity = size)
package hw3; public class FancyArray{ private T[] array; private int size; public FancyArray() { this(10); } public FancyArray(int capacity) { if (capacity = size) { throw new IndexOutOfBoundsException("invalid index"); } return array[index]; } public T set(int index, T newElement) { if (index = size) { throw new IndexOutOfBoundsException("invalid index"); } T oldElement = array[index]; array[index] = newElement; return oldElement; } public boolean contains(T obj) { for (int i = 0; i other = (FancyArray>) o; if (this.size != other.size) { return false; } for (int i = 0; i = 0; i--) { if (e.equals(array[i])) { return i; } } return -1; } public void add(int index, T element){ if(size == array.length){ throw new IllegalStateException("array is full."); } else if(index size){ throw new IndexOutOfBoundsException("invalid index"); } if(index == size){ array[index] = element; size++; } else if(index = index; i--) { array[i + 1] = array[i]; } array[index] = element; size++; } } public T remove(int index) { if (index = size) { throw new IndexOutOfBoundsException("invalid index"); } if (index == size - 1) { T temp = array[size - 1]; size--; return temp; } else { T temp = array[index]; for (int i = index; i
there are the test codes:
// DO NOT MODIFY THIS FILE.
public class FancyArrayTest5 { public static void main(String[] args) { testAddTo(); }
private static void testAddTo() { System.out.println("Testing addFrom");
FancyArray
fa1 = new FancyArray(); fa1.add(1); fa1.add(2); FancyArray
fa2 = new FancyArray(); fa2.add(3); fa2.add(4); fa2.add(5); fa1.addTo(fa2); System.out.println(fa1); // expected: [1, 2] System.out.println(fa2); // expected: [3, 4, 5, 1, 2]
/**********************/
FancyArray
fa3 = new FancyArray(); fa3.add(1); FancyArray
fa4 = new FancyArray(); fa4.add(2.5); fa4.add(3.5); fa3.addTo(fa4); System.out.println(fa4); // expected: [2.5, 3.5, 1] System.out.println(fa3); // expected: [1] } }
Add to the FancyArray class a method named addTo with one parameter: another FancyArray. The method should add all the elements of this FancyArray (the one that is calling the method) to the other FancyArray (the parameter). You can assume that the other FancyArray has enough space for all the new elements. For example, suppose fa1 is a FancyArray> representing [1, 2] and fa2 is a FancyArray> represen ting [3,4,5]. If we say fa1.addTo(fa2), then fa2 should now represent [3,4,5,1,2]; fa1 should remain unchanged. This method should work even if the two FancyArrays do not store elements of exactly the same type. For exam ple, if fa3 is a FancyArray> and fa4 is a FancyArray>, then the method should allow us to say f a3.addTo(fa4), since it is perfectly fine to add Integers to a FancyArray. However, it should not work in reverse: the method should not allow us to say fa4.addTo(fa3), since we cannot just add any Number to a Fancy Array . Note: do not use ArrayList (or any other kind of List or Collection) in the FancyArray class. Additional Notes: Regarding your code's standard output, CodeLab will check for case errors but will ignore whitespace (tabs, spaces, newlines) altogether
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started