Deliverables: -the Java source code files -the test report file (test.doc/docx/p
ID: 667035 • Letter: D
Question
Deliverables:
-the Java source code files
-the test report file (test.doc/docx/pdf/…)
The requirements of this homework are:
(1) Add a constructor that allows the creation of an array of specified size, not necessarily 10 elements, as does the default constructor.
(2) Correct the “add()” method, because adding an element when the array is full is no longer possible; to deal with this, you should increase the size of the stored array.
(3) Correct the “insert()” method, because inserting an element when the array is full is no longer possible; to deal with this, you should increase the size of the stored array.
(4) Add a metod “reverse()” that reverses the array without using an additional array of elements. Perform the reverse “in situ” using only swap operations.
(5) Write a test program for the generic array, which should do the following:
(a) Instantiate the generic array with: E = String.
(b) Allow the user to test all the methods available,
(c) Providing a text-based menu interface OR a GUI (the choice is yours).
(6) Create a test report (test.doc/docx/pdf/…) showing the correct behaviour of all the methods; include screenshots of your running program.
..............
// GenericArray.java
class GenericArray <E>
{
private E [] array;
private int size;
public GenericArray() {
array = (E[]) new Object[10];
size = 0;
}
public E get(int i) {
return array[i];
}
public void set(int i, E value) {
if (i<size) array[i] = value;
}
public void add(E value) {
array[size++] = value;
}
public boolean isFull(){
return size == array.length;
}
public void remove(int i) {
for (int j = i; j < size; j++)
array[j] = array[j+1];
size--;
}
public void insert(int i, E value) {
for (int j = size; j >= i; j--)
array[j+1] = array [j];
array[i] = value;
size++;
}
public void display() {
for (int i = 0; i < size; i++)
System.out.print(array[i] + " ");
System.out.println();
}
}
Explanation / Answer
class GenericArray <E>
{
E[] a;
a = newArray(size);
static <E> E[] newArray(int length, E... array)
{
return Arrays.copyOf(array, length);
}
}
public E get(int i) {
return array[i];
}
public void set(int i, E value) {
if (i<size) array[i] = value;
}
public void add(E value)
{
i[counter]=i;
i++;
}
public boolean isFull(){
return size == array.length;
}
public void remove(int i) {
for (int j = i; j < size; j++)
array[j] = array[j+1];
size--;
}
public void insert(int i, E value) {
for (int j = size; j >= i; j--)
array[j+1] = array [j];
array[i] = value;
size++;
}
public E reverse()
{
Collections.reverse(Arrays.asList(array));
}
public void display() {
for (int i = 0; i < size; i++)
System.out.print(array[i] + " ");
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.