< Unit 2 - Class Structure and Design ('22-'23)

Lesson 8: Mutator Methods

45 minutes

Overview

How can I change the values of an object's instance variables after the object has been created?

Students discover the need for mutator methods to change the values stored in the instance variables of a class. Students also revisit the void keyword and recall edge cases. Students practice writing and calling mutator methods for their classes.

CSA Conceptual Framework
      • CON-1.E.1 - Primitive values and reference values can be compared using relational operators (i.e., == and !=).
      • CON-1.E.2 - Arithmetic expression values can be compared using relational operators (i.e., <, >, <=, >=).
      • CON-1.E.3 - An expression involving relational operators evaluates to a Boolean value.
      • MOD-1.E.7 - Void methods do not have return values and are therefore not called as part of an expression.
      • MOD-1.F.1 - A method signature for a method with parameters consists of the method name and the ordered list of parameter types.
      • MOD-1.F.2 - Values provided in the parameter list need to correspond to the order and type in the method signature.
      • MOD-2.E.1 - A void method does not return a value. Its header contains the keyword void before the method name.
      • MOD-2.E.2 - A mutator (modifier) method is often a void method that changes the values of instance variables or static variables.
      • MOD-3.A.2 - When designing a class, programmers make decisions about what data to make accessible and modifiable from an external class. Data can be either accessible or modifiable, or it can be both or neither.

Agenda

Objectives

Students will be able to:
  • Change the state of an object using mutator methods

Preparation

  • Check the Teacher's Lounge for verified teachers on the CSA Forum to find additional strategies or resources shared by fellow teachers

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the students

Vocabulary

  • Boolean expression - a logical statement that gives either a true or false value
  • Sentinel Value - a special value used to notify the program to stop accepting input
  • mutator method - changes the value stored in an instance variable
  • relational operator - an operator used to compare values or expressions

Teaching Guide

Warm Up (5 minutes)

Revisiting the Painter Class

Remarks

We now know how to access the values stored in private instance variables, but what if we needed to change them after we have already created the object? Let's revisit the Painter class and the methods it used to modify the values stored in its instance variables.

Discuss: Click through the animated slide to display the prompts. Use the Retrieve-Pair-Share strategy to discuss the prompts.

  • What did it mean for a method to return void?
  • What methods in the Painter class were void methods?
  • Why did these methods return void?

Discussion Goal: Students recall that void methods do not return a value and identify the methods in the Painter class that were void methods, such as move() and paint(). Students note that these methods were meant to perform an action and not provide any information.

Activity (30 minutes)

Modifying Private Data (10 minutes)

Remarks

We learned about accessor methods in the previous lesson that we could use to get the value stored in an instance variable. We can also use mutator methods to modify a value stored in an instance variable.

Do This: Review the lesson objectives.

Do This: Direct students to Level 1 on Code Studio to predict the program's outcome, then run the program to compare their predictions to the actual outcome.

Discuss: Click through the animated slide to display the prompts.

  • What do you notice about the code in this program?
  • What do you wonder about the code in this program?

Discussion Goal: Students notice that the methods change the values stored in the instance variables. Students may wonder how these methods are able to change the values since instance variables are private.

Display: Show the video – Mutator Methods.

Remarks

Mutator methods are void methods, which means they do not return a value and therefore cannot be called as part of an expression. As void methods, they can perform an action, like changing the state of an object.

Do This: Click through the animated slide to demonstrate writing and calling a mutator method.

Remarks

When designing a class, programmers make decisions about what data to make accessible and modifiable from an external class. Data can be either accessible or modifiable, or it can be both or neither.

Discuss: Click through the animated slide to display the prompts. Use the Hold That Thought strategy to discuss the prompts.

  • What is different about the way mutator methods work in comparison to accessor methods?
  • Should all instance variables have mutator methods? When would we not want an instance variable to be modified?
  • Are there values that should be considered invalid for certain instance variables?

Discussion Goal: Students identify similarities between mutator methods and accessor methods, such as both working with the instance variables of a class. Students also identify differences, such as mutator methods taking a parameter to set a new value to the instance variable and does not return a value while accessor methods do not have a parameter and return the current value stored in the instance variable.

Edge Cases and User Input (10 minutes)

Remarks

We can use an if statement to check for invalid values. For example, we may want to make sure that a user doesn't enter a negative value for a price or a quantity.

Discuss: Use the Hold That Thought strategy to discuss the prompt.

  • Where have you used operators like less than, greater than, or equal?

Discussion Goal: Students recall using operators in math or previous computer science courses.

Group: Place students in pairs.

Do This: Direct students to Level 2 on Code Studio to investigate the program with a partner. Students make the changes to the program as prompted.

Do This: Click through the animated slide to introduce relational operators.

Do This: Click through the animated slide to define Boolean expression.

Do This: Click through the animated slide to define sentinel value.

Writing Mutator Methods (10 minutes)

Do This: Direct students to Level 3 on Code Studio to complete Levels 3 through 5. Students write mutator methods in the Dessert class on Level 3, then continue to Level 4 to use the Dessert class mutator methods. On Level 5, students complete a choice level to use the Scanner class and a sentinel value to repeatedly take input from a user.

Teaching Tip

Students use the Dessert classes they have developed in previous lessons to write accessor methods for their instance variables. These classes are revisited throughout the curriculum. Students expand on this problem and these classes to add new behaviors and create arrays of Dessert objects in future units.

Remarks

This is a good time to commit our code and save our new version of our Dessert class to the Backpack.

Do This: Play the music clip to cue committing their code and saving the new version of the Dessert class to the Backpack.

Wrap Up (10 minutes)

Debugging Wall

Remarks

We saw some new types of errors today! Let's discuss these errors we encountered and update our Debugging Wall with the strategies we used to debug them.

Discuss: Click through the animated slide to display the prompts. Use the Hold That Thought strategy to discuss the prompts.

  • What errors did you encounter while writing your code?
  • How did you find and fix the error?

Discussion Goal: Students share errors that they encountered while writing their code and strategies they used to debug the errors.

Teaching Tip

Ask students to consider both syntax and logic errors they might have encountered, such as not specifying a parameter for a mutator method or setting an invalid value for an instance variable.

Do This: Have students choose a strategy as a class and add it to the Debugging Wall.

Do This: Review the concepts covered in this lesson.

Display: Key Vocabulary


Assessment: Check for Understanding

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

AP Classroom Topic Questions

To assign questions from the AP Classroom Question Bank that align with this lesson, create a custom quiz in AP Classroom by searching the Question Bank for the Essential Knowledge statements listed at the top of this lesson plan. You can find instructions and video demonstrations to do this on AP Central.

The following Topic Questions in AP Classroom can be assigned as a formative assessment for this lesson:

  • Topic Questions 2.4
  • Topic Questions 3.1
  • Topic Questions 5.5

Note: Some Learning Objectives and Essential Knowledge statements in the suggested Topic Questions are covered in later units.

Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.