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

HERE IN ORDER IS: 1- QUESTIONS 2- ARRAYLISTDEMO.JAVA 3- SAMPLE OUTPUTS ---------

ID: 3548055 • Letter: H

Question

HERE IN ORDER IS:

1- QUESTIONS

2- ARRAYLISTDEMO.JAVA

3- SAMPLE OUTPUTS


---------


This lab assignment will ask you to write a program using an ArrayList.


Exercise 1


1.  Create a New Java Application named ArrayListDemo and copy the file ArrayListDemo.java into the project.

2.  Compile and run the program.

3.  Modify the program so that it asks the user to input a String after the list has been input and tells the user if that String exists in the list.

4. Modify the program so that if the String in 3 has been found, it asks the user to input another String and it replaces the original String with the new String.  Print out the list after the String has been replaced.



arraylistdemo.java:


SAMPLE OUTPUT 1:



Enter items for the list, when prompted.

Type an entry:

Alice


More items for the list? yes

Type an entry:

Bob


More items for the list? yes

Type an entry:

Carol


More items for the list? no

The list contains:

Alice

Bob

Carol


Enter a String to search for:

Bob


Enter a String to replace with:

Bill

Bob found!


The list contains:

Alice

Bill

Carol


SAMPLE OUTPUT 2:


Enter items for the list, when prompted.

Type an entry:

Alice


More items for the list? yes

Type an entry:

Bob


More items for the list? yes

Type an entry:

Carol


More items for the list? no

The list contains:

Alice

Bob

Carol


Enter a String to search for:

Bill

Bill not found!


The list contains:

Alice

Bob

Carol

Explanation / Answer

The output which I got after running the application :


Enter items for the list, when prompted.

Type an entry:

alice

More items for the list? yes

Type an entry:

bob

More items for the list? yes

Type an entry:

carol

More items for the list? no

The list contains:

alice

bob

carol

String to search for:

alice

Enter a String to replace with:

shara

alice found !

More items for the list? no

Now the list contains:

bob

carol

shara


Code :

import java.util.ArrayList;

import java.util.Scanner;

public class ArrayListDemo {

public static void main(String[] args) {

ArrayList toDoList = new ArrayList();

System.out.println("Enter items for the list, when prompted.");

boolean done = false;

boolean searchAndReplace = false;

Scanner keyboard = new Scanner(System.in);

while (!done) {

System.out.println("Type an entry:");

String entry = keyboard.nextLine( );

// code for point 3 of question

if(toDoList.contains(entry)){

System.out.println("Entry already exist in the list, please enter another value");

continue;

}

toDoList.add(entry);

System.out.print("More items for the list? ");

String ans = keyboard.nextLine( );

if (!ans.equalsIgnoreCase("yes"))

done = true;

}

System.out.println("The list contains:");

int listSize = toDoList.size( );

for (int position = 0; position < listSize; position++){

System.out.println(toDoList.get(position));

}

while (!searchAndReplace) {

System.out.println("String to search for:");

String search = keyboard.nextLine( );

System.out.println("Enter a String to replace with:");

String replace = keyboard.nextLine( );

// code for point 3 of question

if(toDoList.contains(search)){

System.out.println(search + " found !");

toDoList.remove(search);

toDoList.add(replace);

}else {

System.out.println(search + " not found !");

}

System.out.print("More items for the list? ");

String ans = keyboard.nextLine( );

if (!ans.equalsIgnoreCase("yes"))

searchAndReplace = true;

}

System.out.println("Now the list contains:");

int listSizeNew = toDoList.size( );

for (int position = 0; position < listSizeNew; position++){

System.out.println(toDoList.get(position));

}

}

}