Answer to Question #264051 in Java | JSP | JSF for mysti

Question #264051

Simulate or explain the code


public Act4B_Grp3_DAT2A(int size) {


A = new int[size];

N = 0;

}


public void insertItem(int item) {

if (N < A.length) {

int insertLocation = findLocationInsert(item);

for (int i = N - 1; i >= insertLocation; i--) {

A[i + 1] = A[i];

}

A[insertLocation] = item;

N++;

}


}


private int findLocationInsert(int item) {

if (N < A.length){

for (int i = 0; i < N; i++) {

if (A[i] > item) {

return i;

}

else {

System.out.print("This is full!");

}

}

return N;

}

return N;

}


public void deleteItem(int item) {

int deleteLocation = findLocationDelete(item);

if (deleteLocation != -1) {

for (int i = deleteLocation + 1; i < N; i++) {

A[i - 1] = A[i];

}

N--;

}

}


private int findLocationDelete(int item) {

for (int i = 0; i < N; i++) {

if (A[i] == item) {

return i;

}

else {

System.out.print("Element not found!");

}

return -1;

}

return -1;

}


1
Expert's answer
2021-11-10T14:51:01-0500


SOLUTION TO THE ABOVE QUESTION.

EXPLANATION OF THE CODE


The program above has a function called Act4B_Grp3_DAT2A(int size), which is a public method meaning it can be accessed outside the class, that takes an integer value and creates an array of that given size.


It also has a function called  int findLocationInsert(int item), which is a private method meaning it can only be accessed within the class that receives an integer value representing an item and returns the location where that item should be inserted in the array.


We also have a function called insertItem(int item), which returns nothing and it can be accessed outside the class because it is a public method. The methods adds the item received as parameter to the array.


The program has a function called  findLocationDelete(int item) ,which is a private method meaning it can only be accessed within the class that receives an integer value representing an item and returns the location of an iem which should be deleted from an array. If the item received as parameter is not found in the array, the function prints element not found and returns -1


We also have a function called public void deleteItem(int item), which returns nothing and it can be accessed outside the class because it is a public method. The methods deletes the item received as parameter from the array.




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS