Trace the following program step by step, to find the program modification of th
ID: 3711161 • Letter: T
Question
Trace the following program step by step, to find the program modification of the names array list when user enter ahmed. Write the output when the user enters ahemd and when enters salem.
import java.util.Scanner;
public class Student_ArrayList {
public static void main(String[] args) {
java.util.ArrayList<String> names = new java.util.ArrayList<String>();
names.add("Eman");
names.add("Rwan");
names.add("Ahmed");
names.add("Kaled");
System.out.print("Names: ");
System.out.println(names);
Scanner in = new Scanner(System.in);
System.out.print("Enter a name: ");
String x = in.next();
boolean removed_flag= false;
for (int i = 0; i < names.size(); i++)
{
if (i < names.size()) {
String pName = names.get(i);
if (pName.equalsIgnoreCase(x)) {
names.remove(i);
removed_flag= true;
}
}
}
if (removed_flag==false) {
names.add(x);
}
System.out.print("Updated names: ");
System.out.println(names);
}
}
note: make it as simple as possible this is my first time learning about java and i don't want the teacher to know that im getting an outside help, thank you.
Explanation / Answer
Firstly I have added comments to your program to help you understand it ... Please go through the program and comments before going through the trace.
//Class declaration
public class Student_ArrayList {
//main method declaration
public static void main(String[] args) {
//declaring and defining an ArrayList with string
java.util.ArrayList<String> names = new java.util.ArrayList<String>();
//adding names to list
names.add("Eman");
names.add("Rwan");
names.add("Ahmed");
names.add("Kaled");
//printing names
System.out.print("Names: ");
System.out.println(names);
//getting input from user
Scanner in = new Scanner(System.in);
System.out.print("Enter a name: ");
String x = in.next();
//flag to check if name is removed
boolean removed_flag = false;
//loop from 0 to size of Arraylist
//searching if name is there than removing it and setting the flag to true
for (int i = 0; i < names.size(); i++)
{
//this if block is not needed because
//this check if performed by for loop itself
if (i < names.size()) {
//getting each name from arraylist
String pName = names.get(i);
//comparing the name from list with entered name
// if equal then remove it an dset removed_flag to true
if (pName.equalsIgnoreCase(x)) {
names.remove(i);
removed_flag = true;
}
}
}
//If removed flag is false that means the name is not there.
// so it is added to the list
if (removed_flag == false) {
names.add(x);
}
//printing the updated list
System.out.print("Updated names: ");
System.out.println(names);
}
}
When user enter ahmed
Firstly the program prints the initial names list as below
Names: [Eman, Rwan, Ahmed, Kaled]
Now user is asked to enter the name , and when it enters ahmed, the loop is executed.
For i ranging from 0 to 3( names.size() is 4 and we are checking that i is less than size. So we will only iterate till i is 3),
for each iteration we get the name out of list.
for i = 0, pName = Eman, and x = ahmed
we compare pName and x with equalsIgnoreCase, it means Case insensitive comparision is done.. So eman will be compared to ahmed, as we move to next iteration
for i = 1, pName = Rwan and x = ahmed,
again not equal , so moving to next iteration
for i = 2, pName = Ahmed and x = ahmed,
As it is case insensitive comparision, so (pName.equalsIgnoreCase(x)) comes out to be true, So Ahmed is removed from list. The new list becomes as follows: -
[Eman, Rwan, Kaled] and removed_flag is set to true.
for i = 3, pName = Kaled and x = ahmed,
again not equal , so moving to next iteration. But we are checking till i = 3 only, So for loop ends, Next we check if removed_flag is false, if it is false we need to add the element in the list. But it is true, so list is not changed.
So final list is [Eman, Rwan, Kaled]
Output when user enter ahmed
Names: [Eman, Rwan, Ahmed, Kaled]
Enter a name: ahmed
Updated names: [Eman, Rwan, Kaled]
Output when user enter ahemd
Names: [Eman, Rwan, Ahmed, Kaled]
Enter a name: ahemd
Updated names: [Eman, Rwan, Ahmed, Kaled, ahemd]
Output when user enter salem
Names: [Eman, Rwan, Ahmed, Kaled]
Enter a name: salem
Updated names: [Eman, Rwan, Ahmed, Kaled, salem]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.