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

dt View Go Tools Window Help D Arrays Lab.pdf (1 page)t\". Edited Q Search Array

ID: 3592727 • Letter: D

Question


dt View Go Tools Window Help D Arrays Lab.pdf (1 page)t". Edited Q Search Arrays Lab 1 Create a class called Cust omertduterAtray with a main method that doclares and instantiates an aay of Stringobjects called cust ome rName. The array should have room for seven String objects. Assign each of the following Strings to sequential locations in the array beginning at array index 0. Do not use an array initializer list to create the customerName TRy Beth 2 3Summer 4 Rick Write a for each loop (enhanced for loop) to display the array of names so that each name is on a separate line. (After printing the contents of the array, print a blank new line for formatting) In the comments text box in the Blackboard assignment link answer the following questions: f L.Whartis dasplayes for the hoser woaay élemenesh 2. Why is it that value? Move the extisring eiemens from indenes 3 and & so that you can ad the Strings Jerry and "Jessica into index 3, and 4, respectively, and the array contains the following elements in this order 0Beth th 2Rick Rick ca 5 Sumimer 5 Summer 6 Morty Write anotier for euch loop (enhaneed or loop to sdispluy the arney of numes, (After printing the contents of the array, print a blank new line for formatting) Write u second, traditional for loop that checks cuch element for the String Jerry"t if found in the array, remove it, shift the remaining elements, and display the array of names ? In the comments text box for your Blackboard submission, unswer the question 3. Are both instances of "Jerry removed correcty from the array? 4. Why or why not? In other words, expluin what you observe in your code

Explanation / Answer

The Required code is as follows:

import java.io.*;

import java.util.*;

import java.lang.*;

class CustomerListerArray {

public static void main (String[] args) {

//System.out.println("GfG!");

String ar[] = new String[7];

ar[0] = "Beth";

ar[1] = "Jerry";

ar[2] = "Rick";

ar[3] = "Summer";

ar[4] = "Morty";

for(int i=0;i<7;i++)

{

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

}

System.out.println();

ar[3] = "Jerry";

ar[4] = "Jessica";

ar[5] = "Summer";

ar[6] = "Morty";

for(int i=0;i<7;i++)

{

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

}

System.out.println();

String names[] = new String[7];

int j=0;

for(int i=0;i<7;i++)

{

if(ar[i] == "Jerry")

{

names[j++] = ar[i+1];

i++;

}

else

{

names[j++] = ar[i];

}

}

for(int i=0;i<7;i++)

{

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

}

System.out.println();

}

}

The output is shown below:

Beth
Jerry
Rick
Summer
Morty
null
null

Beth
Jerry
Rick
Jerry
Jessica
Summer
Morty

Beth
Rick
Jessica
Summer
Morty
null
null

The answer to the questions are:

1. Null is displayed for last two elements.

2. This is because, when an array is initialised with a given size, all the elements are equated to null by default.

3. Yes, both the instances of "Jerry have been removed".

4. To delete an element from a given array, another array is required to copy and shift the value of array elements to the required position.