What is a Method?!

A method is a block of code that belong to a class, very similar to a function.

Methods in Java can take inputs (parameters), perform actions, and return a value (or void if no value is returned).

Methods that are created by the programmer to perform tasks are called user-defined methods, while other methods can be built in (like System.out.println()).

Types of methods:

There are many different types of methods in Java, but here I’ll only highlight the two most common ones and the ones used by College board.

Instance Methods: Methods that belong to an instance of a class Instance methods require an object of the class to be used. They operate on objects of the class.

  • Can access instance variables and other instance methods within the class
  • Can access static variables and methods

Static Methods: Methods that belong to the class itself trather than any instance of the class. Trhey are used for operations taht do not depend on instance-spefific data.

  • Can only directly access other static variables and methods.

Here’s a quick example!

  • Example of an instance method: addMinion()
    • Adds a minion to the list of a villain
    • Parameters: String minon
    • Example call: gru.addMinion(“Kevin”)
  • Second example of an instance method: Villain()
    • Changes instance data
    • Parameters: string Name, String evilPlan
  • Example of a static method: getVillainCount()
    • Returns the total amount of Villain instances
    • No parameters
    • Example call: System.out.println(“There are “ + Villain.getVillainCount() + “ villains in the world.”);

Popcorn hack:

  • Add another static method to the Villain class
  • Keep it minion themed and fun!
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Villain {
    // Instance variables
    public String name;
    public String evilPlan;
    public List<String> minions;
    public static int villainCount = 0;

    // Constructor for name, plan, and minions
    public Villain(String name, String evilPlan) {
        this.name = name;
        this.evilPlan = evilPlan;
        this.minions = new ArrayList<>();
        villainCount++;
    }

    // Instance method to add a minion
    public void addMinion(String minion) {
        minions.add(minion);
        System.out.println(minion + " has been added to " + name + "'s army.");
    }

    // Instance method to describe the villain
    public void describeVillain() {
        System.out.println(name + " is planning to: " + evilPlan);
        System.out.println("They have " + minions.size() + " minions.");
    }

    // Static method to get the total count of villains
    public static int getVillainCount() {
        return villainCount;
    }

    // New static method to assign random tasks to minions
    public static String assignRandomMinionTask(String minionName) {
        String[] tasks = {
            "steal bananas from the grocery store",
            "build a rocket for the moon mission",
            "guard the villain's secret lair",
            "spy on the hero's headquarters",
            "prepare an evil gadget",
            "plan the villain's evil party"
        };
        Random rand = new Random();
        String assignedTask = tasks[rand.nextInt(tasks.length)];
        return minionName + " is now assigned to: " + assignedTask + "!";
    }
}

public class Main {
    public static void main(String[] args) {
        Villain.villainCount = 0;

        // Create new villains
        Villain gru = new Villain("Gru", "steal the moon!");
        Villain vector = new Villain("Vector", "take over the world with magnitude and direction!");

        System.out.println("=== Adding Minions ===");
        // Create some minions
        gru.addMinion("Kevin");
        gru.addMinion("Stuart");
        gru.addMinion("Bob");

        // Create some minions for Vector
        vector.addMinion("Henchman 1");

        System.out.println();

        // Describe the villains and their plans
        System.out.println("=== Villain Descriptions ===");
        gru.describeVillain();
        System.out.println();
        vector.describeVillain();
        System.out.println();

        // Assign random tasks to minions
        System.out.println("=== Assigning Random Minion Tasks ===");
        System.out.println(Villain.assignRandomMinionTask("Kevin"));
        System.out.println(Villain.assignRandomMinionTask("Stuart"));
        System.out.println(Villain.assignRandomMinionTask("Bob"));
        System.out.println(Villain.assignRandomMinionTask("Henchman 1"));
        System.out.println();

        // Get the total count of villains
        System.out.println("=== Total Villain Count ===");
        System.out.println("There are " + Villain.getVillainCount() + " villains in the world.");
    }
}

Main.main(null);

=== Adding Minions ===
Kevin has been added to Gru's army.
Stuart has been added to Gru's army.
Bob has been added to Gru's army.
Henchman 1 has been added to Vector's army.

=== Villain Descriptions ===
Gru is planning to: steal the moon!
They have 3 minions.

Vector is planning to: take over the world with magnitude and direction!
They have 1 minions.

=== Assigning Random Minion Tasks ===
Kevin is now assigned to: plan the villain's evil party!
Stuart is now assigned to: build a rocket for the moon mission!
Bob is now assigned to: guard the villain's secret lair!
Henchman 1 is now assigned to: plan the villain's evil party!

=== Total Villain Count ===
There are 2 villains in the world.

Popcorn hack:

Dr. Nefario is busy assigning work for the minions, and he needs your help to organize his group. Your mission is to write and implement a Java classes for each minion which includes their name, gadgets, personality, and more. Get ready to make Dr. Nefario’s life easier and keep the minions organized!

import java.util.ArrayList;
import java.util.List;

public class Minion {
    // Instance variables
    private String name;
    private List<String> gadgets;
    private String personality;
    private int skillLevel;

    // Constructor to initialize the minion with basic details
    public Minion(String name, String personality, int skillLevel) {
        this.name = name;
        this.personality = personality;
        this.skillLevel = skillLevel;
        this.gadgets = new ArrayList<>();
    }

    // Method to add a gadget to the minion's list of gadgets
    public void addGadget(String gadget) {
        gadgets.add(gadget);
        System.out.println(gadget + " has been added to " + name + "'s gadgets.");
    }

    // Method to describe the minion
    public void describeMinion() {
        System.out.println("Minion Name: " + name);
        System.out.println("Personality: " + personality);
        System.out.println("Skill Level: " + skillLevel);
        System.out.println("Gadgets: " + (gadgets.isEmpty() ? "No gadgets assigned" : gadgets));
    }

    // Method to increase the minion's skill level
    public void increaseSkillLevel(int increment) {
        skillLevel += increment;
        System.out.println(name + "'s skill level has increased to: " + skillLevel);
    }

    // Method to check if the minion is ready for a mission
    public boolean isReadyForMission() {
        return skillLevel >= 5 && gadgets.size() >= 3;
    }

    public static void main(String[] args) {
        // Create some minions
        Minion kevin = new Minion("Kevin", "Cheerful", 4);
        Minion stuart = new Minion("Stuart", "Mischievous", 6);

        // Add gadgets to Kevin
        kevin.addGadget("Laser Gun");
        kevin.addGadget("Jetpack");
        kevin.addGadget("Banana Launcher");

        // Add gadgets to Stuart
        stuart.addGadget("Shrink Ray");
        stuart.addGadget("Fart Gun");

        // Describe the minions
        System.out.println("\n=== Minion Descriptions ===");
        kevin.describeMinion();
        System.out.println();
        stuart.describeMinion();

        // Increase skill level for Kevin
        kevin.increaseSkillLevel(2);

        // Check if the minions are ready for a mission
        System.out.println("\n=== Mission Readiness ===");
        System.out.println(kevin.name + " is ready for the mission: " + kevin.isReadyForMission());
        System.out.println(stuart.name + " is ready for the mission: " + stuart.isReadyForMission());
    }
}


Minion.main(null);
Laser Gun has been added to Kevin's gadgets.
Jetpack has been added to Kevin's gadgets.
Banana Launcher has been added to Kevin's gadgets.
Shrink Ray has been added to Stuart's gadgets.
Fart Gun has been added to Stuart's gadgets.

=== Minion Descriptions ===
Minion Name: Kevin
Personality: Cheerful
Skill Level: 4
Gadgets: [Laser Gun, Jetpack, Banana Launcher]

Minion Name: Stuart
Personality: Mischievous
Skill Level: 6
Gadgets: [Shrink Ray, Fart Gun]
Kevin's skill level has increased to: 6

=== Mission Readiness ===
Kevin is ready for the mission: true
Stuart is ready for the mission: false