Skip to main content

Section 4.72 Unit 4B Projects

90 minutes
These projects were created by Leigh Anne Fitz and Lisa Ferran to provide engaging, standards-aligned learning experiences for AP Computer Science A students. The autograding tests were designed by Kate McDonnell.
Now that you’ve learned to work with arrays, it’s time to explore ArrayListsβ€”a flexible data structure that can grow and shrink as your program runs. In these projects, you’ll learn how to create and manipulate ArrayList objects, access and update elements, add and remove items, and develop algorithms that process dynamic collections of data.
Each project is designed to reinforce these concepts through authentic programming tasks. As you complete them, focus on selecting the appropriate ArrayList methods, using loops to process collections efficiently, and carefully managing element positions as data is added or removed. Mastering ArrayLists will help you build programs that are more adaptable and better suited for managing real-world data.

Subsection 4.72.1 Unit 4B Project 1 - Remove Duplicates

Activity 4.72.1.

Create a program that will remove duplicate values from an ArrayList. In the main method, create an ArrayList of Integers. Fill the ArrayList with 20 random Integers in the range 1 to 10 using a for loop. Print your ArrayList with all values on one line with a space between them using a for loop. Write a static method called removeDuplicates that will receive your ArrayList as a parameter and return the ArrayList with all duplicate values removed. The method should also print out the values that it is removing as they are being removed. You are to do this with nested for loops (use i and j as your loop variables). Remember to think about what happens with your index values when you remove an element. Back in main, print out the ArrayList returned by the method.
Example:
Original list:  3 5 1 2 3 9 10 10 7 1 3 4 8 10 7 5 2 6 4 2
Removed:  3
Removed:  3
Removed:  5
Removed:  1
Removed:  2
Removed:  2
Removed:  10
Removed:  10
Removed:  7
Removed:  4
List without Duplicates:  3 5 1 2 9 10 7 4 8 6

Subsection 4.72.2 Unit 4B Project 2 - ArrayList Operations

Activity 4.72.2.

Create a program that utilizes common ArrayList algorithms. In main, create an ArrayList of Integers. Use a for loop to fill the ArrayList with 50 random Integers in the range 0 to 100. Create static methods to return the following values:
Table 4.72.1. ArrayList Operations - Static Methods
Statement Return Value (blank if no value) Explanation
// list1 = [14, 42, 73, 89, 31]
Assume list1 has been created properly and that the values have been added
sum(list1)
249 Returns the sum of all the numbers in list1
average(list1)
49.8 Returns the average of all the numbers in list1
maximum(list1)
89 Returns the maximum of all the numbers in list1
minimum(list1)
14 Returns the minimum of all the numbers in list1
// list2 = [18, 45, 81, 62, 27, 94]
Assume list2 has been created property and that the values have been added
sum(list2)
327 Returns the sum of all the numbers in list2
average(list2)
46.6 Returns the average of all the numbers in list2
maximum(list2)
81 Returns the maximum of all the numbers in list2
minimum(list2)
18 Returns the minimum of all the numbers in list2
Call each of the methods in the main method and print out the results. All methods should be static. Be sure to label your output with the corresponding information for each number.

Subsection 4.72.3 Unit 4B Project 3 - List of Students

Activity 4.72.3.

Create a program that uses ArrayLists with a Student class. Leave Main alone for now and create a class called Student (do not make it public). Use the following specifications for the Student class:
  • A student should have a name and a grade level
  • Create a single one parameter constructor where the name is passed in as a parameter and the grade level is created randomly (9 - 12)
  • Create getters and setters for each instance variable and create a toString method
Back in Main, create an ArrayList of Students. Do the following:
  • Use a for loop to fill the ArrayList with 10 Students
  • You will need to prompt the user to add the name for each student
  • Create four more ArrayLists, one for each grade level, to hold Student objects
  • Fill each of these, using a for-each loop, with students from your original ArrayList with the corresponding grade level (all 9th grade students should be added to your ArrayList for 9th graders, all 10th grade students should be added to your ArrayList for 10th graders, and so on)
  • Print the number of students in each grade level using the size of the four grade level ArrayLists using four separate println statements
  • Be sure to label which grade level each number represents
You have attempted of activities on this page.