Skip Top Navigation Bar

AP Computer Science A Free Response Practice - Data Analysis and ArrayList - Workout Time

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 Workout class is used to record information for a fitness app about a workout. A Workout object has a type of exercise (e.g."Running", "Cycling", "Swimming"), the duration in minutes, and the calories burned. A declaraion of the Workout class is shown.

public class Workout {
    private String type;
    private int duration;
    private int calories;

    /** Constructs a Workout with the given type, 
     * duration, and calories burned. 
     * @precondition: d > 0
     */
    public Workout(String t, int d, int c) {
        type = t;
        duration = d;
        calories = c;
    }

    /** Returns the type of the workout. */
    public String getType() {
        return type;
    }

    /** Returns the duration of the workout in minutes. */
    public int getDuration() {
        return duration;
    }

    /** Returns the calories burned for the workout. */
    public int getCalories() {
        return calories;
    }
}

The WorkoutLog class maintains an ArrayList named workouts that contains all the workouts a person logs for a week. A partial declaration of the WorkoutLog class is shown.

public class WorkoutLog {
    private ArrayList<Workout> workouts;

    /** Constructs an empty workout log. */
    public WorkoutLog() {
        workouts = new ArrayList<Workout>();
    }

    /** Adds a workout to the log, with a duration > 0. */
    public void addWorkout(Workout w) {
        workouts.add(w);
    }

    /**
     * Compares two workout types to determine which one
     * burned more calories per minute.
     * Returns the type with the higher average calories 
     * per minute across all workouts of that type.
     * If both have the same average, either type can be
     * returned.
     * @precondition typeA and typeB are workout types in 
     * workouts
     * @return the workout type with the higher average 
     * calories per minute
     */
    public String moreEfficientType(String typeA, String typeB) {
        // To be implemented
    }
}

Assume that typeA and typeB workouts exist in workouts and that the duration of each entry is not 0. Further assume that workouts of the same type are uniformly recorded.

Write the moreEfficientType method in the WorkoutLog class. This method should return the type of workout that has the higher average calories burned per minute across all workouts in workouts. It the two workout types have the same average calories burned per minute, either type can be returned.

Example 1

Suppose workouts contains the following Workout objects for the following workouts:

  • Running for 30 minutes burning 300 calories
  • Running for 15 minutes burning 150 calories
  • Cycling for 40 minutes burning 320 calories
  • Cycling for 20 minutes burning 180 calories

Based on these entries, Running burned an average of 10 calories per minute and Cycling burned an average of 8.33 calories per minute. Running burns more aveage calories. Running should be returned.

Example 2

Suppose workouts contains the following Workout objects for the following workouts:

  • Swimming for 30 minutes burning 300 calories
  • Swimming for 10 minutes burning 100 calories
  • Yoga for 40 minutes burning 400 calories

Based on these entries, both Swimming and Yoga burn 10 calories per minute. Either type can be returned.

Write Your Response

  • In order to test your program, we have provided some test cases for calls to moreEfficientType.
  • Write the code for the method moreEfficientType in the Java Playground as indicated by the comment add your code here.
  • All tests should return true if your code is correct.