Unit 4B Projects

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java2 class periods3 exercises30 graded tests

Files

README.mdDownload

Unit 4B Projects

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

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.

Unit 4B Project 1 - Remove Duplicates

Exercise 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

Open the 01-unit4b-project1-remove-duplicates folder and edit Main.java. Press Run to try it; Run Tests checks it (10 tests). Its tests call code you write, so they all fail until that code exists.

Unit 4B Project 2 - ArrayList Operations

Exercise 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:

ArrayList Operations - Static Methods

StatementReturn 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)249Returns the sum of all the numbers in list1
average(list1)49.8Returns the average of all the numbers in list1
maximum(list1)89Returns the maximum of all the numbers in list1
minimum(list1)14Returns the minimum of all the numbers in list1
// list2 = [18, 45, 81, 62, 27, 94]Assume list2 has been created properly and that the values have been added
sum(list2)327Returns the sum of all the numbers in list2
average(list2)54.5Returns the average of all the numbers in list2
maximum(list2)94Returns the maximum of all the numbers in list2
minimum(list2)18Returns 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.

Open the 02-unit4b-project2-arraylist-operations folder and edit Main.java. Press Run to try it; Run Tests checks it (10 tests). Its tests call code you write, so they all fail until that code exists.

Unit 4B Project 3 - List of Students

Exercise 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

Open the 03-unit4b-project3-arraylist-students folder and edit Main.java. Press Run to try it; Run Tests checks it (10 tests). Run feeds it the input in input.txt; change that file to try other input.

For teachers

Unit 4B Projects

Students read CSAwesome2's “Unit 4B Projects” in CS Room and work through its 3 Java coding exercises.

  • Time: 2 class periods
  • Tests: 30 in 3 graded exercises, set up for you when you import the lesson

Goals

  • Practice for Unit 4: Data Collections.

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–03, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-unit4b-project1-remove-duplicatesMain.javaAll or nothing until it compiles10
02-unit4b-project2-arraylist-operationsMain.javaAll or nothing until it compiles10
03-unit4b-project3-arraylist-studentsMain.javaAutograded10

Teachers can download a reference solution after signing in.