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

Java - Exercise 18 - please complete all 4 steps Modify the program by replacing

ID: 660970 • Letter: J

Question

Java - Exercise 18 - please complete all 4 steps

Modify the program by replacing the section that outputs the index where the number was found with the algorithm to delete the number. How do you delete an element in the array? You overwrite it by shifting all the elements. Delete 48. Starting from index 3, copy element at index 3 into index 2, copy element at index 4 into index 3, and so on. Make sure the last element to copy is the last element in the array. Since an element has been deleted, the array is now shorter; therefore, you will need to use a variable (and not length) to keep track of the number of elements in the array. Add another FOR loop to re- print the array at the end of the program (even if the element was not found). Modify the program by adding a DO/WHILE loop that allows the user to delete repeatedly.

Explanation / Answer

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class DeleteElement {

static int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

static int[] b = new int[10];

static int i, k, f, j = 0;

static int l = a.length;

void DeletElnt() {

for (i = 0; i < l; i++) {

if (i != k) {

if (i < k) {

b[i] = a[i];

} else {

b[i - 1] = a[i];

}

}

}

l = l - 1;

System.out.println("New Array ");

for (i = 0; i < l; i++) {

System.out.println(b[i]);

}

}

public static void main(String[] args) {

DeleteElement d = new DeleteElement();

System.out.println("Array Elements are ");

for (i = 0; i < l; i++)

System.out.println(a[i]);

InputStreamReader is = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(is);

System.out.println("Enter the Element for Delete");

try {

String s = br.readLine();

f = Integer.parseInt(s);

for (i = 0; i < l; i++) {

if (f == a[i]) {

System.out.println("DeleteElement found from given array");

k = i;

j++;

d.DeletElnt();

}

}

if (j == 0) {

System.out

.println("Entered Element does not found from given array");

}

} catch (IOException e) {

System.out.println(e);

}

}

}

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