Deliverables: -the Java source code files -the test report file (test.doc/docx/p
ID: 667044 • 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
Hi,
Below is the solution to your question:
import java.io.BufferedReader;
import java.io.InputStreamReader;
// GenericArray.java
class GenericArray <E>
{
private E [] array;
private int size;
public GenericArray() {
array = (E[]) new Object[10];
size = 0;
}
/*constructor that initalizes a dynamic size to an array
*/
public GenericArray(int E[]){
this.array=E;
}
public E get(int i) {
return array[i]; //Displays the array being created
}
public void set(int i, E value) {
if (i<size) array[i] = value;
else{
System.out.prinltn("No more elemnts can be inserted as array is full");
}
}
public void add(E value,int size) {
System.out.println("Adding values to the array");
if(size>array.sizea[]){
System.out.println(Cannot add more values);
}
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) {
if(size<array.length){
for (int j = size; j >= i; j--)
array[j+1] = array [j];
array[i] = value;
size++;
}
else{
System.out.println("Value cannot be inserted as array is full");
}
}
void reversearray(int i,E value){
for(int i = 0; i < array.length; i++)
{
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - 1] = temp;
System.out.println("myArray[" + i + "] = " + array[i]);
}
}
public void display() {
for (int i = 0; i < size; i++)
System.out.print(array[i] + " ");
System.out.println();
}
public class mainArrayclass extends GenericArray{
public static void main(String args[]) throws IOException {
{
System.out.println("Initialising the array............... ");
System.out.println("Enter the size of the array");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader ab = new BufferedReader(isr);
GenericArray newarray=new GenericArray();
int size=ab.readLine();
int[] r = new int[size];
int values = new String[size];
for(int i=0;i<size;i++)
{
System.out.println("Enter the values of array");
new int a[i] = ab.readLine();
GenericArray t=new GenericArray(new int a[]);//Calling parametrized constructor
}
newarray.get(a[]);//Displays the array being created
System.out.println("Size of the array being created is "+size);
newarray.set(size,a[]) ;
newarray.insert(size,a[]);
newarray.reversearray(size,a[]);
}
}
}
Hope that helps...HAPPY ANSWERING!!!!!!!!!!!!!!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.