💻AP Computer Science A
3 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
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 usually have no return type (they are usually 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.
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.
/** 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;
}**
}
Note that if a method does not return anything, the keyword void appears before the method name. If a method does return something, we put the return type (String, boolean, int, double, float, char, etc.) in this location instead of void.
In some cases, a class might have mutator methods that are not void. There can be mutator methods that return the original value of the variables they change. However, mutator methods are typically void methods.
Let's see how mutator methods work in practice.
Assume that this is in the main method of some other class:
Student bob = new Student(10, "Bob Smith", 16);
If we check the state of bob, gradeLevel is 10, name is "Bob Smith," age is 16, and assignment is null.
Now let's say that we realized we forgot Bob's middle name and input the wrong grade level. We can change these features through the following lines:
bob.setName("Bob John Smith");
bob.setGradeLevel(11);
Now if we check the state of bob, gradeLevel is 11, name is "Bob John Smith," age is 16, and assignment is null.
Note that once we construct bob, we have no way of changing the values of age or assignment because there are no mutator methods for these two variables.
An assignment class refers to a class used to store values assigned to variables. It is commonly used in programming to hold and manipulate data.
Term 1 of 10
An assignment class refers to a class used to store values assigned to variables. It is commonly used in programming to hold and manipulate data.
Term 1 of 10
An assignment class refers to a class used to store values assigned to variables. It is commonly used in programming to hold and manipulate data.
Term 1 of 10
Mutator methods are special types of methods in object-oriented programming that allow modification or mutation of an object's attributes or properties.
Accessor methods: Methods that provide access to an object's attributes without modifying them.
Encapsulation: The concept of bundling data and the methods that operate on that data into a single unit, known as an object.
Inheritance: A mechanism in object-oriented programming where one class inherits properties and behaviors from another class.
Setter methods, also known as mutator methods, are a type of method in object-oriented programming that allows the modification or update of the values of instance variables within an object.
Mutator Methods: These are another name for setter methods and serve the same purpose - modifying instance variable values.
Instance Variables: These are variables declared within a class and hold data specific to each instance (object) created from that class.
Accessor Methods: In contrast to setter (mutator) methods, accessor methods retrieve or get the values stored in instance variables without modifying them.
Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.
Methods: Methods are actions or behaviors associated with objects in a class. They define what an object can do or how it can interact with other objects.
Constructor: A constructor is a special method within a class that is automatically called when an object is created from that class. It initializes the object's state by assigning initial values to its instance variables.
Access Modifiers: Access modifiers determine the accessibility or visibility of classes, methods, and variables in object-oriented programming. They control which parts of a program can access certain elements and help enforce encapsulation and data hiding principles.
Void methods are methods in programming that do not return a value. They perform a specific task or action, but they do not produce any output.
Parameters: Parameters are variables that are passed into a method. They allow us to provide input values for the method to use.
Input Parameters: Input parameters are specific types of parameters that receive input values from the caller of a method.
Return Type: Return type is the data type of the value that a method returns. In void methods, there is no return type because they don't return any value.
Immutable refers to an object or data structure that cannot be changed after it is created. Once created, its state remains constant throughout its lifetime.
Mutable: This term describes objects or data structures that can be modified after creation. Unlike immutable objects, mutable ones can have their state altered.
Final Keyword: In Java programming language, using the "final" keyword makes variables constant and classes uninheritable. It ensures immutability and prevents modification.
String Class: Strings in many programming languages are often immutable objects by default. Any modifications to strings create new string objects rather than modifying existing ones.
An assignment class refers to a class used to store values assigned to variables. It is commonly used in programming to hold and manipulate data.
Variables: These are named memory locations that store values. They are often associated with specific data types and can be assigned new values using assignment classes.
Data Types: In programming, each variable has a specific data type that determines the kind of values it can hold. Assignment classes help manage these different data types.
Object-Oriented Programming (OOP): OOP is a programming paradigm that uses objects and classes to structure code. Assignment classes play a crucial role in implementing OOP concepts by storing object instances and their associated data.