Assignment Title: Java ArrayList Demonstration
File Name: PX_ArrayListDemo_lastname.java
Create a Java program that demonstrates how to use an ArrayList by storing, displaying, modifying, and analyzing a list of items.
ArrayListArrayList of String values.set().contains()No user input is required for this version.
The program should clearly show the original list, number of items, removed item, changed item, updated list, whether an item exists, and a numbered list of all items.
Start program
Import ArrayList class
Create an ArrayList of Strings called foods
Add 5 food items to the ArrayList
Print "Original food list"
Loop through the ArrayList
Print each food item
Print the total number of items in the ArrayList
Remove one item from the ArrayList
Print which item was removed
Replace one item in the ArrayList with a new item
Print what item was changed
Print "Updated food list"
Loop through the ArrayList
Print each updated food item
Check if a specific food is in the ArrayList
If it is found
Print that the item is in the list
Else
Print that the item is not in the list
Print "Numbered food list"
Loop through the ArrayList using index numbers
Print position number and food item
End program
// Program Name: PX_ArrayListDemo_lastname.java
// Author: Your Name
// Description:
// This program demonstrates how to use an ArrayList in Java.
// It adds items, displays them, removes an item, updates an item,
// checks for an item, and prints the list with numbered positions.
// Import the ArrayList class from the Java utility library
import java.util.ArrayList;
public class PX_ArrayListDemo_lastname
{
public static void main(String[] args)
{
// Create an ArrayList that will store String values
// In this example, the ArrayList will store favorite foods
ArrayList<String> foods = new ArrayList<String>();
// Add items to the ArrayList using the add() method
foods.add("Pizza");
foods.add("Tacos");
foods.add("Burgers");
foods.add("Pasta");
foods.add("Ice Cream");
// Display a heading for the original list
System.out.println("Original Food List:");
System.out.println("-------------------");
// Use an enhanced for loop to display each item in the ArrayList
//************************
//Fill in this area with missing code
//************************
// Print a blank line to make the output easier to read
//************************
//Fill in this area with missing code
//************************;
// Display the total number of items in the ArrayList using size()
//************************
//Fill in this area with missing code
//************************
// Print a blank line
//************************
//Fill in this area with missing code
//************************
// Remove one item from the ArrayList
// This removes the item at index 2, which is "Burgers"
//************************
//Fill in this area with missing code
//************************
// Display which item was removed
//************************
//Fill in this area with missing code
//************************
// Replace an item in the ArrayList using set()
// This changes the item at index 1 from "Tacos" to "Salad"
//************************
//Fill in this area with missing code
//************************
// Display what item was changed
//************************
//Fill in this area with missing code
//************************
// Print a blank line
//************************
//Fill in this area with missing code
//************************
// Display the updated list after removing and changing items
//************************
//Fill in this area with missing code
//************************
// Use another enhanced for loop to display the updated items
//************************
//Fill in this area with missing code
//************************
// Print a blank line
//************************
//Fill in this area with missing code
//************************
// Check whether a certain item exists in the ArrayList
// The contains() method returns true if the item is found
//************************
//Fill in this area with missing code
//************************
// Check for another item that may not be in the list
//************************
//Fill in this area with missing code
//************************
// Print a blank line
//************************
//Fill in this area with missing code
//************************
// Display the list again, but this time with position numbers
//************************
//Fill in this area with missing code
//************************
// Use a traditional for loop so we can access index numbers
//************************
//Fill in this area with missing code
//************************
// Print a blank line and closing message
System.out.println();
System.out.println("ArrayList demonstration complete.");
}
}
Original Food List: ------------------- Pizza Tacos Burgers Pasta Ice Cream Total number of food items: 5 Removed item: Burgers Changed item: Tacos was replaced with Salad Updated Food List: ------------------ Pizza Salad Pasta Ice Cream Pizza is in the list. Tacos is not in the list. Numbered Food List: ------------------- 1. Pizza 2. Salad 3. Pasta 4. Ice Cream ArrayList demonstration complete.
• Your files will be: • PX_ArrayListDemo_lastname.java (java program) • PX_ArrayListDemo_lastname.png (Screen print) • PX_ArrayListDemo_lastname.mp4 (video of you running your program) • Drop off all 3 files into google classroom.