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

1. Create a class called CustomerListerArray with a main method that declares an

ID: 3674640 • Letter: 1

Question

1. Create a class called CustomerListerArray with a main method that declares and instantiates an array of String objects called customerName.

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 array.

Chris

Lois

Meg

Peter

Stewie

Write an enhanced for loop to display the array of names so that each name is on a separate line

answer the following questions:

• What is displayed for the last two array elements?

• Why is it that value?

Add the Strings “Meg” and “Brian” into index 3, and 4, respectively, so that the array contains the following elements in this order:

Chris Lois Meg Meg Brian Peter Stewie

Write another enhanced for loop to display the array of names.

Write a second, traditional for loop that checks each element for the String “Meg”, if found in the array, remove it, shift the remaining elements, and display the array of names.

answer the question:

• Are both instances of “Meg” removed correctly from the array?

• Why or why not? In other words, explain what you observe in your code.

this needs to work in the latest version of dr java

Explanation / Answer

The first solution is as below,

public class CustomerListerArray {

/**
* @param args
*/
public static void main(String[] args) {
  // TODO Auto-generated method stub
  String[] customerName = new String[7];
  //Assignment of Strings
  customerName[0] = "Chris";
  customerName[1] = "Lois";
  customerName[2] = "Meg";
  customerName[3] = "Peter";
  customerName[4] = "Stewie";
  
  //Display
  for(String name : customerName){
   System.out.println(name);
  }
  
  //
}

}


The last two displayed array elements are null. Since 5th and 6th elements were not initialised for
the array and thats why they will be null.

The 3rd solution is as below,

public class CustomerListerArray {

/**
* @param args
*/
public static void main(String[] args) {
  // TODO Auto-generated method stub
  String[] customerName = new String[7];
  //Assignment of Strings
  customerName[0] = "Chris";
  customerName[1] = "Lois";
  customerName[2] = "Meg";
  customerName[3] = "Meg";
  customerName[4] = "Brian";
  customerName[5] = "Peter";
  customerName[6] = "Stewie";
  
customerName
  //Display
  for(String name : customerName){
   System.out.println(name);
  }
  
  //
}

}

The 4th solution is as below,
  
  //After removing the Meg, remaining elements are shifted.
  for(int i =0; i<7;i++){
   String name = customerName[i];
   if(name == "Meg"){
    customerName[i]=null;
    
   }
  }
  
  
  for(int i=0;i<7;i++){
   String name = customerName[i];
   if(name== null){
    int k=i;
    for(int j=i; j+1<7;j++){
     customerName[j] = customerName[j+1];
     k=j;
    }
    customerName[k+1] = null;
    break;
   }
   System.out.println(name);
  }
  
  for(String name : customerName){
   System.out.println(name);
  }