Lesson 4.15: Using Data Sets with Arrays and ArrayLists

Unit 4 · Lesson 4.15 · Data Sets

Lesson 4.15: Using Data Sets with Arrays and ArrayLists

🕑 40–50 min · 8 Practice Questions · Integration · FRQ #3 Full Pattern

What You'll Learn

  • 4.15.A: Use a data set stored in an array or ArrayList to solve a problem.
  • Load a data set from a file into an ArrayList of objects.
  • Apply multiple algorithms to a loaded data set in sequence.
  • Recognize the complete FRQ #3 workflow: file → objects → ArrayList → algorithms.
  • Write methods that operate on a data set to answer real-world questions.

📌 This Lesson Is the Integration Point

Lessons 4.2 (data sets), 4.7–4.10 (ArrayList), 4.14 (file reading), and 4.5 (algorithms) all come together here. A real program reads data from a file, stores it as objects in an ArrayList, and runs algorithms on the collection to answer questions. That complete workflow is what FRQ #3 tests every year.

The Complete Data Set Workflow

Every data-driven program follows the same three phases:

  1. Load: Read data from a file into an ArrayList of objects.
  2. Store: Each row of data becomes one object; the ArrayList holds all objects.
  3. Process: Apply algorithms (filter, count, aggregate, search) to answer questions.

✅ Complete Example: Student Grade Analysis

Suppose students.txt has one student per line: name, then score, space-separated.

// Phase 1 & 2: Load file into ArrayList of Student objects
ArrayList roster = new ArrayList();
try {
    File f = new File("students.txt");
    Scanner sc = new Scanner(f);
    while (sc.hasNext()) {
        String name = sc.next();
        int score = sc.nextInt();
        roster.add(new Student(name, score));
    }
    sc.close();
} catch (FileNotFoundException e) {
    System.out.println("File not found.");
}

// Phase 3: Process — count passing students
int passing = 0;
for (Student s : roster) {
    if (s.getScore() >= 70) passing++;
}
System.out.println("Passing: " + passing);

Applying Multiple Algorithms to One Data Set

Once loaded, the same ArrayList can be queried multiple ways without reloading:

// Average score
int total = 0;
for (Student s : roster) {
    total += s.getScore();
}
double avg = (double) total / roster.size();

// Highest scorer's name
Student best = roster.get(0);
for (int i = 1; i < roster.size(); i++) {
    if (roster.get(i).getScore() > best.getScore()) {
        best = roster.get(i);
    }
}
System.out.println("Top student: " + best.getName());

// All students above average
ArrayList aboveAvg = new ArrayList();
for (Student s : roster) {
    if (s.getScore() > avg) {
        aboveAvg.add(s);
    }
}

What the AP Exam Tests

FRQ #3 always follows this pattern:

  • A class is given (like Student, Movie, Product) with a few instance variables and getter methods.
  • An ArrayList of that class is given as a parameter.
  • You write 1–2 methods that process the ArrayList using the algorithms you've practiced.

The file-reading code is typically given to you — you're tested on what you do with the data after it's loaded, not on the file-reading syntax itself.

Summary

  • The complete workflow: file → Scanner → objects → ArrayList → algorithms.
  • Load once, query many times — the ArrayList persists in memory for all processing.
  • FRQ #3 gives you the class and ArrayList; you write the algorithm methods.
  • Every algorithm from Lessons 4.5 and 4.10 applies to a loaded data set.

Practice Questions

MCQ 1
A program reads a file of product names and prices into an ArrayList. Which phase correctly describes loading the data?
A Applying a filter algorithm to find products under $10
B Using a Scanner to read each line and calling new Product(...) to add each record to the list
C Calling list.size() to count products
D Writing a method that returns the most expensive product
B. The loading phase reads from the file and creates objects. A, C, and D all describe the processing phase — they operate on the already-loaded data.
MCQ 2
A Movie class has getTitle(), getYear(), and getRating() (double). An ArrayList films is already loaded. Which method correctly returns the number of movies with a rating above 8.0?
Predict before reading the options.
A
int count = 0;
for (Movie m : films) {
    if (m.getYear() > 8.0) count++;
}
return count;
B
return films.size();
C
int count = 0;
for (Movie m : films) {
    if (m.getRating() > 8.0) count++;
}
return count;
D
int count = 0;
for (Movie m : films) {
    if (m.getRating() >= 8.0) count++;
}
return count;
C. A calls getYear() instead of getRating() — wrong getter. B returns total count regardless of rating. D uses >= which includes exactly 8.0 — the problem says "above 8.0" (strictly greater). C is correct.
MCQ 3
After a data set is loaded into an ArrayList, a programmer needs the average, the maximum, and a filtered list. How many times should the file be opened?
A Once — load the data once, then run all three algorithms on the ArrayList.
B Three times — once per algorithm.
C Twice — once for reading, once for processing.
D It depends on the size of the file.
A. File I/O is expensive. Load the data once into memory (the ArrayList), then run all algorithms on the in-memory collection. There's no reason to re-open the file for each algorithm.
MCQ 4
On AP FRQ #3, what is typically given to you and what are you asked to write?
A You are given nothing — you write the entire program from scratch.
B You are given the file and asked to write the Scanner reading code.
C You are given the algorithms and asked to write the class definition.
D You are given the class definition and an ArrayList of objects; you write methods that process the collection.
D. FRQ #3 provides the class (with instance variables and getters), an ArrayList of those objects, and asks you to write 1–2 methods. The file-reading code and class definition are given — your job is the algorithm methods that operate on the data.
Tier 3 · AP Mastery

Mastery: Using Data Sets with Arrays and ArrayLists

MCQ 5
A WeatherReading class has getDate() (String), getHigh() (int), and getLow() (int). An ArrayList readings is loaded from a file. Which method correctly returns the average of all high temperatures?
Predict before reading the options.
A
int total = 0;
for (WeatherReading r : readings) {
    total += r.getLow();
}
return (double) total / readings.size();
B
int total = 0;
for (WeatherReading r : readings) {
    total += r.getHigh();
}
return total / readings.size();
C
int total = 0;
for (WeatherReading r : readings) {
    total += r.getHigh();
}
return (double) total / readings.size();
D
int total = 0;
for (WeatherReading r : readings) {
    total += r.getHigh() + r.getLow();
}
return (double) total / readings.size();
C. A uses getLow() — wrong attribute. B uses integer division — truncates decimal. D adds both high and low — computes average of combined temperatures, not just highs. C correctly uses getHigh() with a cast before division.
MCQ 6
Using the same WeatherReading class, which method correctly returns an ArrayList of all readings where the high exceeded 90?
Predict before reading the options.
A
for (WeatherReading r : readings) {
    if (r.getHigh() > 90) readings.remove(r);
}
return readings;
B
ArrayList hot = new ArrayList();
for (WeatherReading r : readings) {
    if (r.getHigh() > 90) hot.add(r);
}
return hot;
C
ArrayList hot = new ArrayList();
for (WeatherReading r : readings) {
    if (r.getLow() > 90) hot.add(r);
}
return hot;
D
ArrayList hot = new ArrayList();
for (WeatherReading r : readings) {
    if (r.getHigh() > 90) hot.add(r.getHigh());
}
return hot;
B. A removes from the source list inside a for-each — ConcurrentModificationException. C filters by getLow() — wrong attribute. D returns integers (temperatures), not WeatherReading objects — the method should return the objects. B correctly builds and returns a new ArrayList of matching WeatherReading objects.
MCQ 7
A Transaction class has getAmount() (double) and getCategory() (String). Which method correctly returns the total amount for the "Food" category?
Watch for the String comparison trap.
A
double total = 0;
for (Transaction t : ledger) {
    if (t.getCategory() == "Food") {
        total += t.getAmount();
    }
}
return total;
B
double total = 0;
for (Transaction t : ledger) {
    if (t.getAmount() == "Food") {
        total += t.getAmount();
    }
}
return total;
C
double total = 0;
for (Transaction t : ledger) {
    if (t.getCategory().equals("food")) {
        total += t.getAmount();
    }
}
return total;
D
double total = 0;
for (Transaction t : ledger) {
    if (t.getCategory().equals("Food")) {
        total += t.getAmount();
    }
}
return total;
D. A uses == for String comparison — compares references, may not work. B compares a double to a String — won't compile. C uses lowercase "food" — case-sensitive mismatch with "Food". D correctly uses .equals() with the exact matching string "Food".
MCQ 8
In the FRQ #3 context, which statement is most accurate about the relationship between Lessons 4.2, 4.10, 4.14, and 4.15?
A 4.2 defines the data model, 4.14 loads the data, 4.10 provides the algorithms, and 4.15 integrates them into a complete workflow.
B They cover completely independent topics with no relationship.
C 4.14 is required before 4.2 can be understood.
D 4.15 replaces 4.10 — once you reach 4.15, the earlier algorithms are no longer needed.
A. Each lesson builds on the previous: 4.2 establishes the conceptual model (row = object), 4.14 adds file reading as the data source, 4.10 covers the algorithms to apply, and 4.15 connects them all into the complete workflow that FRQ #3 tests.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]