join the ap-comp-sci-a course

๐ŸŽ–๏ธfiveable certificate upon completion

โฑ๏ธ 3 min read

๐Ÿ“ย ย 

ap comp sci a

ย โ€ขย 

study guide

ย โ€ขย 

Unit 5

5.5 Mutator Methods


written by

Peter Cao

peter cao

published on september 20, 2020

Last updated on September 20, 2020

Introduction to Mutator Methods

Now that we are done with accessor methods, we will move on to mutator methods (also called setter methods). Mutator methods change the values of instance variables of an object by setting them to another value as a parameter. The headers for these methods are basically the opposite of accessor methods.

While accessor methods have a return type and no parameters, mutator methods have no return type (they are void methods) and have a parameter (the value which to change the object to). Like accessor methods, sometimes we don't want other clients to change the data of an object, thus there will not be a mutator method for that instance variable.

If a class has no mutator methods, then that class is said to be immutable. In our example, we will have the Assignment class be immutable with no mutator methods.

Writing Mutator Methods for the Student Class

Now, we will write mutator methods for the Student classes for the variables that we want the client to be able to change: name and gradeLevel.

Fiveable

join more students

create an account on fiveable

/** Represents an assignment that a student will complete */ public class Assignment { private boolean correctAnswer; // represents the answer to an assignment, either T/F /** Makes a new assignment with one True/False question and sets the correct answer */ public Assignment(boolean answer) { correctAnswer = answer; } /** Prints details about the assignment */ @Override public String toString() { return "This is an assignment with correct answer " + answer; } } /** Represents a high school student */ public class Student { private int gradeLevel; // a grade between 9-12 private String name; // the students name in the form "FirstName LastName" private int age; // the student's age, must be positive private Assignment assignment; // the current assignment the student is working on /** Makes a new student with grade gradeLev, name fullName, and age ageNum */ public Student(int gradeLev, String fullName, int ageNum) { gradeLevel = gradeLev; name = fullName; age = ageNum; assignment = null; // There is no active assignment at the moment } /** Returns the student's grade level */ public int getGradeLevel() { return gradeLevel; } /** Returns the student's name */ public String getName() { return name; } /** Returns the current assignment the student is working on */ public Assignment returnCurrentAssignment() { return assignment; } /** Prints details about the student */ @Override public String toString() { return name + ", a " + gradeLevel + "th grade high school student"; } /** Changes the student's name */ public void setName(String fullName) { name = fullName; } /** Changes the student's grade level */ public void setGradeLevel(int gradeLev) { gradeLevel = gradeLev; } }
Fiveable Logo

2550 north lake drive
suite 2
milwaukee, wi 53211

92% of Fiveable students earned a 3 or higher on their 2020 AP Exams.

*apยฎ and advanced placementยฎ are registered trademarks of the college board, which was not involved in the production of, and does not endorse, this product.

ยฉ fiveable 2020 | all rights reserved.