'F' → Fullscreen
ArrayList objects using core methods. // Figure 1 — Create an ArrayList and add itemsimport java.util.ArrayList;
public class Program{ public static void main(String[] args) { ArrayList<String> foods = new ArrayList<String>();
foods.add("pizza"); foods.add("tacos"); foods.add("sushi");
System.out.println(foods); }}// Figure 2 — get, set, sizeimport java.util.ArrayList;
public class Program{ public static void main(String[] args) { ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(84); scores.add(91); scores.add(76);
System.out.println("First score: " + scores.get(0)); System.out.println("Count: " + scores.size());
scores.set(1, 100); // replace value at index 1 System.out.println(scores); }}// Figure 3 — Traversal (indexed vs for-each)import java.util.ArrayList;
public class Program{ public static void main(String[] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(4); nums.add(7); nums.add(2);
// Indexed loop (best when you need the index) for (int i = 0; i < nums.size(); i++) { System.out.println(i + ": " + nums.get(i)); }
// For-each loop (best when you only need values) for (int value : nums) { System.out.println(value); } }}// Figure 4 — remove shifts elements leftimport java.util.ArrayList;
public class Program{ public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(); names.add("Ava"); names.add("Ben"); names.add("Chris");
names.remove(1); // removes "Ben"
// Now "Chris" moved to index 1 System.out.println(names); }}// Task 1 — ArrayList Basics (Strings)//// Create an ArrayList<String> named "movies".// 1) Add 5 movie titles.// 2) Print the entire list.// 3) Print the first movie using get(0).// 4) Replace the third movie (index 2) with a new title using set.// 5) Print the list again.
import java.util.ArrayList;
public class Program{ public static void main(String[] args) { ArrayList<String> movies = new ArrayList<String>();
movies.add("The Incredibles"); movies.add("Spider-Man"); movies.add("Toy Story"); movies.add("Shrek"); movies.add("Star Wars");
System.out.println("Movies: " + movies); System.out.println("First: " + movies.get(0));
movies.set(2, "Finding Nemo");
System.out.println("Updated: " + movies); }}// Task 2 — Numeric List + Stats//// Create an ArrayList<Integer> named "temps".// 1) Add 7 temperatures (integers).// 2) Compute and print the average.// 3) Find and print the max temperature.// 4) Count and print how many temps are >= 80.//// Rules:// - Use loops (you choose indexed or for-each).// - Average must be a double.
import java.util.ArrayList;
public class Program{ public static void main(String[] args) { ArrayList<Integer> temps = new ArrayList<Integer>();
temps.add(72); temps.add(68); temps.add(75); temps.add(70); temps.add(69); temps.add(82); temps.add(84);
int sum = 0; int max = temps.get(0); int hotCount = 0;
for (int t : temps) { sum += t;
if (t > max) { max = t; }
if (t >= 80) { hotCount++; } }
double avg = (double) sum / temps.size();
System.out.println("Temps: " + temps); System.out.println("Average: " + avg); System.out.println("Max: " + max); System.out.println("Count >= 80: " + hotCount); }}// Task 3 — Search + Update (Dynamic Scoreboard)//// Build a "scoreboard" using two parallel ArrayLists:// - ArrayList<String> names// - ArrayList<Integer> scores//// 1) Add 5 players and their scores (same index = same player).// 2) Print the scoreboard (name: score).// 3) Ask the user for a player name.// - If found, add 10 points to their score.// - If not found, print "Player not found."// 4) Print the updated scoreboard.//// Hint: use equalsIgnoreCase for name matching.
import java.util.ArrayList;import java.util.Scanner;
public class Program{ public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(); ArrayList<Integer> scores = new ArrayList<Integer>();
names.add("Ava"); scores.add(40);
names.add("Ben"); scores.add(55);
names.add("Chris"); scores.add(20);
names.add("Dina"); scores.add(65);
names.add("Eli"); scores.add(35);
System.out.println("Scoreboard:"); printScoreboard(names, scores);
Scanner scanner = new Scanner(System.in); System.out.print("Enter a player name: "); String target = scanner.nextLine();
int index = findIndex(names, target);
if (index == -1) { System.out.println("Player not found."); } else { scores.set(index, scores.get(index) + 10); System.out.println("Added 10 points to " + names.get(index) + "!"); }
System.out.println("Updated scoreboard:"); printScoreboard(names, scores);
scanner.close(); }
public static int findIndex(ArrayList<String> names, String target) { for (int i = 0; i < names.size(); i++) { if (names.get(i).equalsIgnoreCase(target)) { return i; } }
return -1; }
public static void printScoreboard(ArrayList<String> names, ArrayList<Integer> scores) { for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i) + ": " + scores.get(i)); } }}// Task 4 — Remove + Insert (Playlist Editor)//// Create an ArrayList<String> named "playlist".// 1) Add 6 song titles.// 2) Remove the song at index 2.// 3) Insert a new song at index 0.// 4) Print the playlist with indexes like:// 0: SongTitle// 1: SongTitle// ...//// Hint:// - playlist.add(index, value) inserts and shifts right// - playlist.remove(index) removes and shifts left
import java.util.ArrayList;
public class Program{ public static void main(String[] args) { ArrayList<String> playlist = new ArrayList<String>();
playlist.add("Song A"); playlist.add("Song B"); playlist.add("Song C"); playlist.add("Song D"); playlist.add("Song E"); playlist.add("Song F");
playlist.remove(2); playlist.add(0, "Intro Track");
for (int i = 0; i < playlist.size(); i++) { System.out.println(i + ": " + playlist.get(i)); } }}Your program output should something similar to the sample output below.
Movies: [The Incredibles, Spider-Man, Toy Story, Shrek, Star Wars]First: The IncrediblesUpdated: [The Incredibles, Spider-Man, Finding Nemo, Shrek, Star Wars]
Temps: [72, 68, 75, 70, 69, 82, 84]Average: 74.28571428571429Max: 84Count >= 80: 2
Scoreboard:Ava: 40Ben: 55Chris: 20Dina: 65Eli: 35Enter a player name: BenAdded 10 points to Ben!Updated scoreboard:Ava: 40Ben: 65Chris: 20Dina: 65Eli: 35
0: Intro Track1: Song A2: Song B3: Song D4: Song E5: Song FYou may write your reflection answers as comments at the bottom of your code.
size() instead of length?Integer instead of int in an ArrayList?Submit your activity and reflection answers to the appropriate dropbox.