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

i need help with this program public class Loop6ChangeList { /*TODO: Define a me

ID: 3575694 • Letter: I

Question

i need help with this program

public class Loop6ChangeList {
/*TODO: Define a method that change the ith element of the list to "hello"
the jth element of the list "bye". No loop is needed.
*/

  

  
  
public static void printList(String[] list) {
System.out.print("{");
for (int i=0; i<list.length; i++) {
System.out.print(list[i]);
if (i != list.length - 1)
System.out.print(", ");
else
System.out.println("}");
}
}
  
public static void main(String[] args) {
String[] list1 = {"apple", "pear", "kiwi", "last"};
System.out.print("Original list1: ");
printList(list1);
setListElements(list1, 1, 3); //This should set list1[1] to "hello" and list[3] to "bye"
System.out.print("Changed list1:");
printList(list1);
  
String[] list2 = {"good", "morning", "bob", "and", "dana"};
System.out.print("Original list2: ");
printList(list2);
setListElements(list2, 0, 2); //This should set list1[0] to "hello" and list[2] to "bye"
System.out.print("Changed list2:");
printList(list2);
}
}

Explanation / Answer

/*the following setListElement method will do the change in original array. it is recommanded to return the string array so that we will get changes in actual array also.*/

public static String[] setListElements(String[] list,int i, int j) {
// to change the value of ith element

list[i]="hello";

// to change the value of jth element
list[j]="bye";
return list;
}

// there will be slight changes in main method

public static void main(String[] args) {
String[] list1 = {"apple", "pear", "kiwi", "last"};
System.out.print("Original list1: ");
printList(list1);

/* the method will change the values of list1 array. so to get changes in original array accept the returned array in same array.*/

list1=setListElements(list1, 1, 3); //This should set list1[1] to "hello" and list[3] to "bye"
System.out.print("Changed list1:");
printList(list1);
  
String[] list2 = {"good", "morning", "bob", "and", "dana"};
System.out.print("Original list2: ");
printList(list2);

/* do the same changes here also. */
list2=setListElements(list2, 0, 2); //This should set list1[0] to "hello" and list[2] to "bye"
System.out.print("Changed list2:");
printList(list2);
}