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.
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.
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:
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.
Subsection4.72.3Unit 4B Project 3 - List of Students
Activity4.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:
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)