AP Computer Science A Free Response Practice - Data Analysis and ArrayList - Average Daily Sales
Clock
This single part question is worth 5 points.
AP CSA Alignment
| Unit Alignment | Learning Objectives | Resources | Skills |
|---|---|---|---|
| Unit 1: Using Objects and Methods | 1.3.C Develop code for arithmetic expressions and determine the result of these expressions. | 2.A Write program code to implement an algorithm. | |
| 1.10.A Develop code to call class methods and determine the result of those calls. | 2.C Write program code involving procedural abstractions. | ||
| Unit 2: Selection and Iteration | 2.3.A Develop code to represent branching logical processes by using selection statements and determine the result of these processes. | 2.A Write program code to implement an algorithm. | |
| Unit 4: Data Collections | 4.9.A Develop code used to traverse the elements of an ArrayList and determine the results of these traversals. |
2.B Write program code involving data abstractions. | |
4.10.A Develop code for standard and original algorithms for a particular context or specification that involve ArrayList objects and determine the result of these algorithms. |
Directions
The Sale class is used to record information about a sale made in a store. A declaraion of the Sale class is shown.
The SalesReport class maintains an ArrayList named sales that contains all the sales at the store for a week. A partial declaration of the SalesReport class is shown.
Write the averageForDay method in the SalesReport class. This method should return the average amount of all sales that occurred on the specified day. If there are no sales for that day, return 0.0.
Suppose report is a new SalesReport object and sales contains the following five Sale objects.
For the sales shown, report.averageForDay("Monday") will return 125.0. There are three sales for Monday. They are 100.0, 200.0, and 75.0. The average is (100.0 + 200.0 + 75.0) / 3 = 125.0.
For the sales shown, report.averageForDay("Tuesday") will return 50.0. There is only one sale on Tuesday.
For the sales shown, report.averageForDay("Friday") will return 0.0. There are no sales on Friday.
Write Your Response
- In order to test your program, we have provided some test cases for calls to
averageForDay. - Write the code for the method
averageForDayin the Java Playground as indicated by the commentadd your code here. - All three tests should return
trueif your code is correct.