Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the GenericArray class below, which implements a data structure for sto

ID: 667109 • Letter: C

Question

Consider the GenericArray class below, which implements a data structure for storing an array of elements of type E

// 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();

   }

}

The requirements of this homework are:

Add a constructor that allows the creation of an array of specified size, not necessarily 10 elements, as does the default constructor.
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.
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.
Add a metod “reverse()” that reverses the array without using an additional array of elements. Perform the reverse “in situ” using only swap operations.
Write a test program for the generic array, which should do the following:

Instantiate the generic array with: E = String.
Allow the user to test all the methods available,
Providing a text-based menu interface OR a GUI (the choice is yours).

Create a test report (test.doc/docx/pdf/…) showing the correct behaviour of all the methods; include screenshots of your running program.

Deliverables:

-the Java source code files

-the test report file (test.doc/docx/pdf/…)

.

Explanation / Answer

Below, is the required Java program (console application) fulfilling the requirement.

//GenericAarray.java

class GenericArray <E>
{   
private E [] array;
private int size;
public GenericArray() {
array = (E[]) new Object[10];
size = 0;
}
public GenericArray(int length) {
array = (E[]) new Object[length];
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) {
int length = array.length;
int newSize = size++;
// New item can added as array has space
if(newSize < length {
array[newSize] = value;
}
else{
E[] tempArray = array;
array = (E[]) new Object[length*2];
for(int i=0; i<tempArray.length; i++){
       array[i]=tempArray[i];  
   }
array[newSize] = 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--;
}

/*
* Here also it is verified whether (size +1) is greater than length or not.If yes, in that case
* a new will be created with double the existing one.Then items from old array will be
* copied to the new array.
/*
public void insert(int i, E value) {
int length = array.length;
if((i+1) >= length -1){
E[] tempArray = array;
array = (E[]) new Object[length*2];
for(int i=0; i<tempArray.length; i++){
       array[i]=tempArray[i];  
   }
}

for (int j = size; j >= i; j--)
array[j+1] = array [j];
array[i] = value;
size++;
}

/*
* Swap first with last, second with last -1 and so on.
*/
public void reverse(){
int length = array.length;
for(int j=0; j< length/2;j++){
E item = array[(length -1) - j];
array[(length -1) - j] = array[j];
array[j]=item;
}
}
public void display() {
for (int i = 0; i < size; i++)
System.out.print(array[i] + " ");
System.out.println();
}
}

//Tester.java

public class Tester
{
public static void main(){
GenericArray<String> array= null;
Console console = System.Console();
System.out.Println("Please enter your choice....0-> Instantiation, 1 ->Get item, 2-> Set Item, 3->add, 4->remove, 5->insert, 6->reverse, 7-> display, 99->quit");

int choice = Integer.parseInt(console.readLine());
while(choice != 99){
switch(choice){
case 0:
System.out.Println("Enter the size of the array...");
int size = Integer.parseInt(console.readLine());
array= new GenericArray<String>(size);
break;

case 1:
if(array==null){
System.out.Println("Invalid choice");
}
else{
System.out.Println("Enter the index to be get...");
int index= Integer.parseInt(console.readLine());
System.out.Println(array.get(index));
}
break;

case 2:
   if(array==null){
System.out.Println("Invalid choice");
}
else{
System.out.Println("Enter the index to be set...");
int index= Integer.parseInt(console.readLine());
System.out.Println("Enter the item to be set...");
String item= console.readLine();
array.set(index,item);
}
break;


case 3:
if(array==null){
System.out.Println("Invalid choice");
}
else{
System.out.Println("Enter the item to be add...");
String item= console.readLine();

array.add(item);
}
break;

case 4:
if(array==null){
System.out.Println("Invalid choice");

}
else{
System.out.Println("Enter the index to be removed...");
int index= Integer.parseInt(console.readLine());
array.remove(index);
}
break;


case 5:
if(array==null){
System.out.Println("Invalid choice");
}
else{
System.out.Println("Enter the index to be set...");
int index= Integer.parseInt(console.readLine());
System.out.Println("Enter the item to be set...");
String item= console.readLine();
array.insert(index, item);
}
break;

case 6:
if(array==null){
System.out.Println("Invalid choice");
}
else{
array.reverse();
}
break;

case 7:
if(array==null){
System.out.Println("Invalid choice");
}
else{
array.display();
}
break;

}
  
   choice = Integer.parseInt(console.readLine());
}

}

}

On execution, console will show different options.Base on enter option functionlity will be executed.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote