Skip Top Navigation Bar

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 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.
Unit 1: Using Objects and Methods 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.
Unit 4: Data Collections 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. 2.B Write program code involving data abstractions.

Directions

The Sale class is used to record information about a sale made in a store. A declaraion of the Sale class is shown.

public class Sale {
    private double amount;
    private String day;

    /** Constructs a Sale with the given amount and day. */
    public Sale(double amt, String d) {
        amount = amt;
        day = d;
    }

    /** Returns the sale amount. */
    public double getAmount() {
        return amount;
    }

    /** Returns the day of the sale. */
    public String getDay() {
        return day;
    }
}

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.

public class SalesReport {
    private ArrayList<Sale> sales;

    /** Constructs an empty sales report. */
    public SalesReport() {
        sales = new ArrayList<Sale>();
    }

    /** Adds a Sale to the report. */
    public void addSale(Sale s) {
        sales.add(s);
    }

    /** Returns the average amount of all sales that occurred on the specified day.
      * Returns 0.0 if there are no sales on that day.
      * @param day the day of the week
      * @return the average sale amount for that day, or 0.0 if none
      */
    public double averageForDay(String day) {
        // To be implemented
    }
}

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 averageForDay in the Java Playground as indicated by the comment add your code here.
  • All three tests should return true if your code is correct.