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, Returns

Mutators are named in the setBlank syntax where the Blank is the name of the field you want to modify. Mutators, like other methods in Java, take one or more parameters. Since mutators are type void as they do not return a value but rather they modify the object’s state.

// A class representing a Car with attributes like make, model, and speed.
public class Car {
    // Private fields (attributes)
    private String make;
    private String model;
    private int speed;
    // Constructor to initialize attributes
    public Car(String make, String model, int speed) {
        this.make = make;
        this.model = model;
        if (speed >= 0) {  // Ensure speed is non-negative
            this.speed = speed;
        } else {
            System.out.println("Speed cannot be negative, setting speed to 0.");
            this.speed = 0;
        }
    }
    // Mutator (Setter) for 'make' field
    public void setMake(String make) {
        this.make = make;
    }
    // Mutator (Setter) for 'model' field
    public void setModel(String model) {
        this.model = model;
    }
    // Mutator (Setter) for 'speed' field
    public void setSpeed(int speed) {
        if (speed >= 0) { // Ensuring the speed is non-negative
            this.speed = speed;
        } else {
            System.out.println("Speed cannot be negative.");
        }
    }
    // Display car details
    public void displayCarInfo() {
        System.out.println("Make: " + make);
        System.out.println("Model: " + model);
        System.out.println("Speed: " + speed + " km/h");
    }
}
// Create an instance of the Car class using the constructor and demonstrate the mutators
Car myCar = new Car("Honda", "Civic", 100);
// Displaying car info to verify the set values from constructor
myCar.displayCarInfo();
// Modifying the cars speed using the mutator
myCar.setSpeed(150); // Valid speed update
myCar.displayCarInfo();
myCar.setSpeed(-50); // Invalid value, should trigger a warning

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.

public class Person {
    private String name;
    private int age;
    private double height;

    public Person(String name, int age, double height) {
        this.setName(name);
        this.setAge(age);
        this.setHeight(height);
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        if (age >= 0) {
            this.age = age;
        } else {
            System.out.println("Age cannot be negative.");
        }
    }

    public void setHeight(double height) {
        if (height >= 0) {
            this.height = height;
        } else {
            System.out.println("Height cannot be negative.");
        }
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getHeight() {
        return height;
    }

    @Override
    public String toString() {
        return "Name: " + name + ", Age: " + age + ", Height: " + height;
    }

    public static void main(String[] args) {
        Person person = new Person("John Doe", 25, 5.9);

        System.out.println("Initial Details: " + person);

        person.setName("Jane Doe");
        System.out.println("After Name Change: " + person);

        person.setAge(30);
        System.out.println("After Age Change: " + person);

        person.setHeight(6.1);
        System.out.println("After Height Change: " + person);

        person.setAge(-5);  
        System.out.println("After Invalid Age Change: " + person);

        person.setHeight(-3.2);  
        System.out.println("After Invalid Height Change: " + person);
    }
}