Java ArrayList Assignment

Spec Sheet

Assignment Title: Java ArrayList Demonstration

File Name: PX_ArrayListDemo_lastname.java

Objective

Create a Java program that demonstrates how to use an ArrayList by storing, displaying, modifying, and analyzing a list of items.

Program Requirements

  1. Import and use ArrayList
  2. Create an ArrayList of String values
  3. Add at least 5 items to the list
  4. Display all items in the list
  5. Display the total number of items in the list
  6. Remove one item from the list
  7. Change one item in the list using .set()
  8. Display the updated list
  9. Check whether a specific item exists in the list using .contains()
  10. Use a loop to print each item with its position number

Suggested Theme

This sample program uses a list of favorite foods.

Input

No user input is required for this version.

Output

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.

Pseudocode

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

Full Java Program with Detailed Comments

// 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");

//************************
//Fill in this area with missing code
//************************

        // Print a blank line and closing message
        System.out.println();
        System.out.println("ArrayList demonstration complete.");
    }
}

Example Output

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.

Files to be dropped off.

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