Skip to the content.
Unit 5 5.1 5.2 5.4 5.41 5.5 5.6 5.7 5.8 5.9 HW

This Keyword

Team Teach

This Keyword

General

  • this keyword is used as a reference to the object itself
    • It can be used to reference the object’s instance variables, methods, constructors, etc.
    • Enables instance variables to access an object’s variables and methods
  • It is used to avoid confusion between instance variables and parameter values (local variables)
public class Minion {
    private int banana = 5;
    public void setBanana(int banana) {
    this.banana = banana; //this.banana refers to the instance variable banana
    // banana refers to the parameter banana
    }
}

Calling Another Constructor

  • this keyword can be used to invoke another constructor of the same class

Format: this.(argument)

public class MinionGoggles {
    private double lensSize;
    public MinionGoggles(double lensSize) {
        this.lensSize = lensSize; //this keyword references the hidden data field, lensSize
    }
    public MinionGoggles() {
        this(1.0); //passes 1.0 as an argument to the 1st constructor
    }
}

Calling Other Methods

  • this keyword can be used to call other methods in the same class

Format: this.methodName()

public class Minion {
    private String name;

    public Minion(String name) {
        this.name = name;
    }

    public void run() {
        System.out.println(this.name + " is running");
        this.speak();  // Calls the speak method of the current object
    }

    public void speak() {
        System.out.println(this.name + " says: 'Bello!'");
    }
}

Passing this as a Parameter

  • this keyword can be used to pass the reference of the current object to a method or constructor
  • Format: methodName(this)
class Minion {
    String name;

    Minion(String name) {
        this.name = name;
    }

    void printMinion(Minion minion) {
        System.out.println("Minion name: " + minion.name);
    }
    
    void callPrintMinion() {
        printMinion(this); // Passes the current object to the method
    }
}

public class Main {
    public static void main(String[] args) {
        Minion minion1 = new Minion("Kevin");
        minion1.callPrintMinion(); // Output: Minion name: Kevin
    }
}
Main.main(null);
Minion name: Kevin

Popcorn Hacks

The Minions are preparing for a big event where the tallest and fastest minion will get to assist Gru on his next mission! You’ve been called in as the official “Minion Trainer” to help compare the minions. The goal is to see which minion is more prepared for the mission.

Minion Image

public class Minion {
    private String speed;
    private int height;

    public Minion(String speed, int height) {
        this.speed = speed;
        this.height = height;
    }

    public void setHeight(int height) {
        this.height = height;  
    }

    public String getSpeed() {
        return this.speed;  
    }

    public boolean isTallerThan(Minion otherMinion) {
        return this.height > otherMinion.height;  
    }

    public static void main(String[] args) {
        Minion minion1 = new Minion("fast", 43);
        Minion minion2 = new Minion("medium", 28);

        System.out.println("minion 1 speed: " + minion1.getSpeed());
        System.out.println("Is minion1 taller than minion2? " + minion1.isTallerThan(minion2));

        minion2.setHeight(50);
        System.out.println("Is minion1 still taller than minion2? " + minion1.isTallerThan(minion2));
    }
}
Minion.main(null);
minion 1 speed: fast
Is minion1 taller than minion2? true
Is minion1 still taller than minion2? false
  1. What is the output of the statement System.out.println(“minion 1 speed: “ + minion1.speed())? Explain why the this keyword is useful in the getSpeed() method.

  2. What does the isTallerThan() method compare?

  3. What happens to the result of System.out.println(“Is minion1 taller than minion2? “ + minion1.isTallerThan(minion2)) after minion2.setHeight(50) is called?

Who should be selected for the mission?🤔

  1. The output is: “fast”. The this keyword is useful in the getSpeed() method because it refers to the speed instance variable of the current Minion object.

  2. It compares the height of two Minion objects.

The result would be false because minion 2’s height becomes 50 and 43 < 50.

🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌🍌
</br> Pancake Image

In the bustling kitchen of Gru’s secret lab, the Minions were on a mission to create the perfect pancake for their breakfast feast. Kevin needed to make sure each pancake was perfectly round, with the exact right circumference for maximum deliciousness. But how? He needed to figure out the precise relationship between the pancake’s radius and its circumference. Help Kevin create the perfect pancake!

The MinionPancake class below should have:

  • A radius field
  • A method setRadius(double radius): have this set as the radius
  • A method calculateCircumference(): have this calculate the circumference of a circle with radius 5 and print it
    • (hint: use Math.PI for ‘π’)
class MinionPancake {
    private double radius;  // Field to store the radius of the pancake

    // Method to set the radius of the pancake
    public void setRadius(double radius) {
        this.radius = radius;  // Use 'this' to set the radius field
    }

    // Method to calculate the circumference of the pancake
    public void calculateCircumference() {
        double circumference = 2 * Math.PI * this.radius;  // Formula for circumference
        System.out.println("The circumference of the pancake is: " + circumference);
    }
}

// In the main method or testing section:
public class Main {
    public static void main(String[] args) {
        // Create a pancake with radius 5 and calculate its circumference
        MinionPancake pancake = new MinionPancake();
        pancake.setRadius(5);  // Set the radius to 5
        pancake.calculateCircumference();  // Calculate and print the circumference
    }
}

Main.main(null);
The circumference of the pancake is: 31.41592653589793