Unit5.5
Mutators/Setters
Mutators are used to modify the attribute of an object. They are typically public methods to allow external code to modify the object state.
Naming & Parameters
Mutators are named in the setBlank syntax where “Blank” is the name of the field you want to modify. Mutators, like other methods in Java, take one or more parameters.
Return Type
Since mutators are type void as they do not return a value but rather they modify the object’s state.
Validation
Mutators often include validation logic to ensure that the new values are being set within acceptable bounds. An example of validation being used with mutators can be seen with the setSpeed mutator that doesn’t allow negative values.
Lets take a look at an example!
This example discusses showcases some of the most vital use cases for mutators
// A class representing a Car with attributes like make, model, and speed.
public class Car {
private String brand;
private String model;
private int speed;
// Constructor to initialize attributes
public Car(String brand, String model, int speed) {
this.brand = brand;
this.model = model;
if (speed >= 0) {
this.speed = speed;
} else {
System.out.println("Speed cannot be negative, setting speed to 0.");
this.speed = 0;
}
}
//Mutators
public void setBrand(String brand) {
this.brand = brand;
}
public void setModel(String model) {
this.model = model;
}
public void setSpeed(int speed) {
if (speed >= 0) { //Validation so speed is not negative
this.speed = speed;
} else {
System.out.println("Speed cannot be negative.");
}
}
// Display car details
public void displayCarInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Speed: " + speed + " mph");
}
}
Car myCar = new Car("Honda", "Civic", 100);
myCar.displayCarInfo();
// Modifying attributes mutators
myCar.setSpeed(150);
myCar.setBrand("Rolls Royce");
myCar.setModel("Phantom");
myCar.displayCarInfo();
//Testing Validation with invalid value (Should trigger warning)
myCar.setSpeed(-50);
Brand: Honda
Model: Civic
Speed: 100 mph
Brand: Rolls Royce
Model: Phantom
Speed: 150 mph
Speed cannot be negative.
Popcorn Hack
1. Create a Class
Define a class Person with private fields: String name, int age, and double height. This is done for you.
2. Create Mutators
Write mutator methods setName(String name), setAge(int age) (with validation for non-negative age), and setHeight(double height) (with validation for non-negative height).
3. Write a Constructor
Create a constructor that initializes name, age, and height with provided values.
4. Test the Mutators
In the main method, create a Person object, modify its fields using the mutators, and print the details after each modification.
class Person {
private String name;
private int age;
private double height;
// the constructor
Person(String name, int age, double height) {
this.name = name;
this.age = (age >= 0) ? age : 0; // this and the next line make sure age or height aint negative
this.height = (height >= 0) ? height : 0;
}
// mutator
void setName(String name) {
this.name = name;
}
// mutator again for nonnegative values of age
void setAge(int age) {
if (age >= 0) this.age = age;
}
// mutator again for nonnegative values of height
void setHeight(double height) {
if (height >= 0) this.height = height;
}
// prints the values of the person
void printDetails() {
System.out.println("Name: " + name + ", Age: " + age + ", Height: " + height);
}
}
// executes code
Person person = new Person("Diddy", 25, 1.75);
person.printDetails();
// modifies
person.setName("Faze Bugha");
person.setAge(30);
person.setHeight(1.68);
// prints the updated details
person.printDetails();
Name: Diddy, Age: 25, Height: 1.75
Name: Faze Bugha, Age: 30, Height: 1.68