Skip to the content.

Unit 5 Hacks

Popcorn Hack 1.

Write a Student class. - Each student has a name - Each student has a score (out of 100) - A student passes if their score is above 70

Check which student passes (write a class header, instance variables, constructors, and methods)

public class Student {
    private String name;
    private int score;
    
    // Default constructor
    public Student() {
        name = "";
        score = 0;   
    }

    // Overloaded constructor
    public Student(String n, int s) {
        name = n;
        score = s;
    }

    //Accessor Methods
    public String getName() {
        return name;
    }

    public int getScore(){
        return score;
    }

    //Mutator Methods
    public void setName(String n){
        name = n;
    }

    public void setScore(int s){
        if(s < 0 || s > 100){
            score = 0; //default value
        }
        else {
            score = s;
        }
    }

    public boolean pass(){
        return (score >= 70);
    }
}

Your turn (yes this is a popcorn hack)

  • Make your own class with an integer variable
  • Make a constructor for that
  • Create an object and print the variable
public class Banana {
    private int numberOfBananas;

    public Banana(int n) {
        numberOfBananas = n;
    }

    public int getNumberOfBananas() {
        return numberOfBananas;
    }

    public void printBananaInfo() {
        System.out.println("Minions have " + numberOfBananas + " bananas! 🍌");
    }
}

public class Main {
    public static void main(String[] args) {
        Banana minionBanana = new Banana(10);

        System.out.println("Number of bananas: " + minionBanana.getNumberOfBananas());

        minionBanana.printBananaInfo();
    }
}

Main.main(null);
Number of bananas: 10
Minions have 10 bananas! 🍌

Your turn (aren’t you having fun!!!)

Using the code you wrote before (you did do the previous popcorn hack… right?), complete the following

  • Make a no-arg constructor
  • Make a parameterized constructor
  • “Make” a default constructor

Please seperate all of those three into different code cells.

No-Argument Constructor:

public class Banana {
    private int numberOfBananas;

    // No-arg constructor (default value set to 5)
    public Banana() {
        numberOfBananas = 5;
    }

    // Getter method to retrieve the number of bananas
    public int getNumberOfBananas() {
        return numberOfBananas;
    }

    // Print a minion-themed message
    public void printBananaInfo() {
        System.out.println("Minions have " + numberOfBananas + " bananas! 🍌");
    }
}

// Main code
public class Main {
    public static void main(String[] args) {
        // Create a Banana object using the no-arg constructor
        Banana minionBanana = new Banana();

        // Print the number of bananas
        minionBanana.printBananaInfo();
    }
}
Main.main(null);
Minions have 5 bananas! 🍌

Parameterized Constructor:

public class Banana {
    private int numberOfBananas;

    // Parameterized constructor to set the number of bananas
    public Banana(int n) {
        numberOfBananas = n;
    }

    // Getter method to retrieve the number of bananas
    public int getNumberOfBananas() {
        return numberOfBananas;
    }

    // Print a minion-themed message
    public void printBananaInfo() {
        System.out.println("Minions have " + numberOfBananas + " bananas! 🍌");
    }
}

// Main code
public class Main {
    public static void main(String[] args) {
        // Create a Banana object using the parameterized constructor
        Banana minionBanana = new Banana(15);

        // Print the number of bananas
        minionBanana.printBananaInfo();
    }
}
Main.main(null);
Minions have 15 bananas! 🍌

Default Constructor:

public class Banana {
    private int numberOfBananas;

    // Default constructor (same as no-arg constructor, sets default value)
    public Banana() {
        numberOfBananas = 5;
    }

    // Getter method to retrieve the number of bananas
    public int getNumberOfBananas() {
        return numberOfBananas;
    }

    // Print a minion-themed message
    public void printBananaInfo() {
        System.out.println("Minions have " + numberOfBananas + " bananas! 🍌");
    }
}

// Main code
public class Main {
    public static void main(String[] args) {
        // Create a Banana object using the default constructor
        Banana minionBanana = new Banana();

        // Print the number of bananas
        minionBanana.printBananaInfo();
    }
}
Main.main(null);
Minions have 5 bananas! 🍌

Your turn

  • Make a overloaded constructor
public class Minion {
    private String speed;
    private int height;

    // Overloaded Constructor 1: Takes both speed and height
    public Minion(String speed, int height) {
        this.speed = speed;
        this.height = height;
    }

    // Overloaded Constructor 2: Takes only speed
    public Minion(String speed) {
        this.speed = speed;
        this.height = 0;  // Default height if not specified
    }

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

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

    // Method to compare heights
    public boolean isTallerThan(Minion otherMinion) {
        return this.height > otherMinion.height;
    }

    public static void main(String[] args) {
        // Using overloaded constructor with both speed and height
        Minion minion1 = new Minion("fast", 43);
        System.out.println("minion1 speed: " + minion1.getSpeed());

        // Using overloaded constructor with only speed
        Minion minion2 = new Minion("medium");
        System.out.println("minion2 speed: " + minion2.getSpeed());
    }
}
Minion.main(null);
minion1 speed: fast
minion2 speed: medium

Popcorn Hack

Gru is preparing for his big mission to steal the moon, and he needs to assign his minions various tasks based on their skills. Create a new getter method called skillLevel and print out the Minion’s skillLevelMinion class with the attributes name, task, and skillLevel. Implement some getter accessor methods, and then create a Minion object to retrieve its values.

public class Minion {
    private String name;
    private String task;
    private int skillLevel;  // Create skillLevel instance variable

    // Constructor to initialize name, task, and skillLevel
    public Minion(String n, String t, int s) {
        name = n;
        task = t;
        skillLevel = s;
    }

    // Getter Methods
    public String getName() {
        return name;
    }

    public String getTask() {
        return task;
    }

    // Getter method for skillLevel
    public int getSkillLevel() {
        return skillLevel;
    }
}

// Main code
public class Main {
    public static void main(String[] args) {
        // Create a Minion object with name, task, and skillLevel
        Minion Stuart = new Minion("Stuart", "Developing propulsion system", 85);

        // Print out the minion's name, task, and skillLevel
        System.out.println("Name: " + Stuart.getName());
        System.out.println("Task: " + Stuart.getTask());
        System.out.println("Skill Level: " + Stuart.getSkillLevel());  // Print skillLevel
    }
}
Main.main(null);
Name: Stuart
Task: Developing propulsion system
Skill Level: 85

Popcorn Hack

Return all the instance variables in Minion so that we can directly print the object values of the minion Kevin.

public class Minion {
    // Instance variables
    private double height;
    private String name;
    private String hair;
    private int eyes;

    // Default Constructor
    public Minion() {
        height = 3.7;
        name = "Bob";
        hair = "None";
        eyes = 2;
    }

    // Overloaded Constructor
    public Minion(double h, String n, String hr, int e) {
        height = h;
        name = n;
        hair = hr;
        eyes = e;
    }

    // Overridden toString() method to return a formatted string of instance variables
    @Override
    public String toString() {
        return "Minion Details:\n" +
               "Name: " + name + "\n" +
               "Height: " + height + " feet\n" +
               "Hair Style: " + hair + "\n" +
               "Number of Eyes: " + eyes;
    }
}

// Main code
public class Main {
    public static void main(String[] args) {
        // Create Minion object Kevin
        Minion kevin = new Minion(4.10, "Kevin", "Sprout-Cut", 2);

        // Print Kevin object
        System.out.println(kevin);
    }
}
Main.main(null);
Minion Details:
Name: Kevin
Height: 4.1 feet
Hair Style: Sprout-Cut
Number of Eyes: 2

Your turn!

  • Make your own class with your own setter method
public class Car {
    // the car's make (brand)
    private String make;

    // Constructor to set the make
    public Car(String m) {
        make = m;
    }

    // getter for make
    public String getMake() {
        return make;
    }

    // setter for make
    public void setMake(String m) {
        make = m;
    }
}

// Main code to interact with the Car object
public class Main {
    public static void main(String[] args) {
        // Create a Car object with initial make
        Car myCar = new Car("Toyota");

        // Get and print the car's make
        System.out.println(myCar.getMake());  // Output: Toyota

        // Change the car's make using the setter
        myCar.setMake("Honda");

        // Print the updated car's make
        System.out.println(myCar.getMake());  // Output: Honda
    }
}
Main.main(null);
Toyota
Honda

Your turn!

Make your own class with:

  • A constructor
  • A getter
  • A setter

Create an object and interact with it! Make sure you use both your setter and getter methods at least once.

public class Phone {
    private String model;

    public Phone(String m) {
        model = m;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String m) {
        model = m;
    }
}

public class Main {
    public static void main(String[] args) {
        Phone myPhone = new Phone("iPhone 16");

        System.out.println("Phone model: " + myPhone.getModel()); 

        myPhone.setModel("Samsung Galaxy S24");

        // Print the updated phone's model
        System.out.println("Updated phone model: " + myPhone.getModel());
    }
}
Main.main(null);
Phone model: iPhone 16
Updated phone model: Samsung Galaxy S24

Popcorn hack:

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

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 announce the plans of all villains
    public static void announceVillainPlan(List<Villain> villains) {
        System.out.println("=== Villains' Plans Announcement ===");
        for (Villain v : villains) {
            System.out.println(v.name + " is plotting to: " + v.evilPlan + "!");
        }
    }
}

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!");

        // Add minions to villains
        System.out.println("=== Adding Minions ===");
        gru.addMinion("Kevin");
        gru.addMinion("Stuart");
        gru.addMinion("Bob");

        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();

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

        // Announce the plans of all villains
        List<Villain> villainList = new ArrayList<>();
        villainList.add(gru);
        villainList.add(vector);

        Villain.announceVillainPlan(villainList);  // Call the static method
    }
}
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.

=== Total Villain Count ===
There are 2 villains in the world.
=== Villains' Plans Announcement ===
Gru is plotting to: steal the moon!!
Vector is plotting to: take over the world with magnitude and direction!!

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 for minion 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
    public void addGadget(String gadget) {
        gadgets.add(gadget);
        System.out.println(gadget + " has been assigned to " + name + ".");
    }

    // Method to describe the minion's personality
    public String getPersonality() {
        return personality;
    }

    // Method to display the minion's gadgets
    public void displayGadgets() {
        System.out.println(name + "'s Gadgets: " + gadgets);
    }

    // Method to describe the minion
    public void describeMinion() {
        System.out.println("Name: " + name);
        System.out.println("Personality: " + personality);
        System.out.println("Skill Level: " + skillLevel);
        displayGadgets();
    }

    // Static method to describe group of minions
    public static void organizeMinions(List<Minion> minions) {
        System.out.println("=== Organizing Minions ===");
        for (Minion minion : minions) {
            minion.describeMinion();
            System.out.println("-------------------------");
        }
    }
}
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.

=== Total Villain Count ===
There are 2 villains in the world.
=== Villains' Plans Announcement ===
Gru is plotting to: steal the moon!!
Vector is plotting to: take over the world with magnitude and direction!!

Popcorn hacks:

  • Look at some of the code I’ve commented out and try experimenting with gadgetsList if you want. Otherwise, just make a static variable that serves a purpose in the program.
import java.util.ArrayList;
import java.util.List;

public class Gadget {
    public static int totalGadgets = 0;  // Static variable to track total gadgets made
    private String gadgetName;  // Instance variable to store the name of the gadget
    public static List<Gadget> gadgetsList = new ArrayList<>();  // Static list to track all gadgets

    // Constructor to set the gadget name and increment totalGadgets
    public Gadget(String gadgetName) {
        this.gadgetName = gadgetName;
        totalGadgets++;  // Increment the total gadgets count
        gadgetsList.add(this);  // Add this gadget to the static list
    }

    // Method to return the gadget's name
    public String getGadgetName() {
        return gadgetName;
    }

    // Static method to display all gadgets in the list
    public static void displayAllGadgets() {
        System.out.println("=== List of All Gadgets ===");
        for (Gadget gadget : gadgetsList) {
            System.out.println(gadget.getGadgetName());
        }
    }

    // Main method to test the Gadget class
    public static void main(String[] args) {
        // Create three gadgets
        Gadget g1 = new Gadget("Freeze Ray");
        Gadget g2 = new Gadget("Banana Blaster");
        Gadget g3 = new Gadget("Lipstick Taser");

        // Print the total number of gadgets
        System.out.println("Total gadgets made: " + Gadget.totalGadgets);

        // Display all the gadgets in the list
        Gadget.displayAllGadgets();
    }
}
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.

=== Total Villain Count ===
There are 2 villains in the world.
=== Villains' Plans Announcement ===
Gru is plotting to: steal the moon!!
Vector is plotting to: take over the world with magnitude and direction!!

Popcorn hack:

Dr. Nefario and Gru need to calculate the cost of their equipment to remain under the budget for this year! Add a second parameter to the Gadget constructor to include cost for Gadget instances, and make a static method to calculate the price of all gadgets that have been made so far.

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

public class Gadget {
    public static int totalGadgets = 0;  // Static variable to track total gadgets made
    public static double totalCost = 0;  // Static variable to track the total cost of all gadgets
    private String gadgetName;  // Instance variable to store the name of the gadget
    private double gadgetCost;  // Instance variable to store the cost of the gadget
    public static List<Gadget> gadgetsList = new ArrayList<>();  // Static list to track all gadgets

    // Constructor to set the gadget name, cost, and increment totalGadgets and totalCost
    public Gadget(String gadgetName, double gadgetCost) {
        this.gadgetName = gadgetName;
        this.gadgetCost = gadgetCost;
        totalGadgets++;  // Increment the total gadgets count
        totalCost += gadgetCost;  // Add the cost to the total cost
        gadgetsList.add(this);  // Add this gadget to the static list
    }

    // Method to return the gadget's name
    public String getGadgetName() {
        return gadgetName;
    }

    // Method to return the gadget's cost
    public double getGadgetCost() {
        return gadgetCost;
    }

    // Static method to calculate the total cost of all gadgets
    public static double calculateTotalCost() {
        return totalCost;
    }

    // Static method to display all gadgets in the list
    public static void displayAllGadgets() {
        System.out.println("=== List of All Gadgets ===");
        for (Gadget gadget : gadgetsList) {
            System.out.println(gadget.getGadgetName() + " - Cost: $" + gadget.getGadgetCost());
        }
    }

    // Main method to test the Gadget class
    public static void main(String[] args) {
        // Create three gadgets with names and costs
        Gadget g1 = new Gadget("Freeze Ray", 5000.99);
        Gadget g2 = new Gadget("Banana Blaster", 1200.50);
        Gadget g3 = new Gadget("Lipstick Taser", 300.75);

        // Print the total number of gadgets
        System.out.println("Total gadgets made: " + Gadget.totalGadgets);

        // Display all the gadgets in the list with their costs
        Gadget.displayAllGadgets();

        // Print the total cost of all gadgets
        System.out.println("Total cost of all gadgets: $" + Gadget.calculateTotalCost());
    }
}
Gadget.main(null);
Total gadgets made: 3
=== List of All Gadgets ===
Freeze Ray - Cost: $5000.99
Banana Blaster - Cost: $1200.5
Lipstick Taser - Cost: $300.75
Total cost of all gadgets: $6502.24

Popcorn Hack

  • Figure out why the happiness level and the energy level is not showing up the way we want it to. First one to do so will get a high five from Trevor Huang.

Answer: The reason the happinessLevel and energyLevel are not showing up as expected is due to variable shadowing. In the constructor of MinionMood, the variables happinessLevel and energyLevel are being re-declared as local variables inside the constructor, which hides (or shadows) the instance variables of the same names.

public class MinionMood
{
    private int happinessLevel;
    private int energyLevel;

    public MinionMood(int bananas, int tasks)
    {
        // Use "this" to refer to the instance variables
        this.happinessLevel = 2 * bananas;
        this.energyLevel = tasks;
    }

    public String toString()
    {
        return "Happiness Level: " + happinessLevel + "\nEnergy Level: " + energyLevel;
    }
}

public class Main {
    public static void main(String[] args) {
        MinionMood bob = new MinionMood(5, 2);
        System.out.println(bob);
    }
}
Main.main(null);
Happiness Level: 10
Energy Level: 2

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.

  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?🤔

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

    public void 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));
    }
}

Answers:

  1. The output would be minion 1 speed: fast

  2. The isTallerThan() method compares the heights of two minions. Specifically, it compares the height of the current minion (this.height) with the height of another minion passed as an argument (otherMinion.height). If the current minion is taller than the other minion, the method returns true; otherwise, it returns false.

  3. After minion2.setHeight(50) is called, minion2’s height is updated to 50, which is greater than minion1’s height of 43. So the next time the comparison is made, the output becomes: Is minion1 still taller than minion2? false

  4. Based on the comparison of speed and height, the selection would depend on the criteria Gru values more. If Gru prioritizes speed, then minion1 should be selected because minion1 is faster ("fast" vs. "medium"). However, if height is more important, then after calling minion2.setHeight(50), minion2 would be taller, so minion2 might be a better choice

Hack 1

Gru has just recently stopped El Macho from destroying the world. But now, Gru needs to separate the leftover purple minions and the yellow minions so that he can cure the infected minions. He then needs to organize the minions in terms of recovery time and usefulness. To do this, Gru needs you to make a minion class with the instance variables color, name, energy levels, gadgets, hair, height

import java.util.List;

public class Minion {
    private String color;
    private String name;
    private int energyLevel;
    private List<String> gadgets;
    private String hair;
    private double height;

    public Minion(String color, String name, int energyLevel, List<String> gadgets, String hair, double height) {
        this.color = color;
        this.name = name;
        this.energyLevel = energyLevel;
        this.gadgets = gadgets;
        this.hair = hair;
        this.height = height;
    }

    // Getter and Setter for color
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    // Getter and Setter for name
    public String getName() {
        return name;
    }

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

    // Getter and Setter for energy level
    public int getEnergyLevel() {
        return energyLevel;
    }

    public void setEnergyLevel(int energyLevel) {
        this.energyLevel = energyLevel;
    }

    // Getter and Setter for gadgets
    public List<String> getGadgets() {
        return gadgets;
    }

    public void setGadgets(List<String> gadgets) {
        this.gadgets = gadgets;
    }

    // Getter and Setter for hair
    public String getHair() {
        return hair;
    }

    public void setHair(String hair) {
        this.hair = hair;
    }

    // Getter and Setter for height
    public double getHeight() {
        return height;
    }

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

    // toString method to print minion details
    public String toString() {
        return "Minion{name='" + name + "', color='" + color + "', energyLevel=" + energyLevel + ", gadgets=" + gadgets 
               + ", hair='" + hair + "', height=" + height + "}";
    }
}

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // Creating a list of gadgets
        List<String> gadgets = Arrays.asList("Freeze Ray", "Banana Blaster");

        // Creating a minion object
        Minion kevin = new Minion("Yellow", "Kevin", 85, gadgets, "Sprout", 4.1);

        // Print the minion details
        System.out.println(kevin);

        // Changing the minion's energy level
        kevin.setEnergyLevel(95);

        // Print the updated energy level
        System.out.println(kevin.getName() + "'s updated energy level: " + kevin.getEnergyLevel());
    }
}
Main.main(null);
Minion{name='Kevin', color='Yellow', energyLevel=85, gadgets=[Freeze Ray, Banana Blaster], hair='Sprout', height=4.1}
Kevin's updated energy level: 95

Hack 2

Now Gru needs you to make a default constructor for all the NPC minions. Assign each default minion a default color,name,energy level, gadget, hair, and height.

import java.util.List;
import java.util.Arrays;

public class Minion {
    // Instance variables
    private String color;
    private String name;
    private int energyLevel;
    private List<String> gadgets;
    private String hair;
    private double height;

    // Default constructor for NPC Minions
    public Minion() {
        this.color = "Yellow";
        this.name = "Bob";
        this.energyLevel = 70;
        this.gadgets = Arrays.asList("Banana Launcher", "Fart Gun");
        this.hair = "None";
        this.height = 3.5;
    }

    // Constructor with all variables
    public Minion(String color, String name, int energyLevel, List<String> gadgets, String hair, double height) {
        this.color = color;
        this.name = name;
        this.energyLevel = energyLevel;
        this.gadgets = gadgets;
        this.hair = hair;
        this.height = height;
    }

    // Getter and Setter for color
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    // Getter and Setter for name
    public String getName() {
        return name;
    }

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

    // Getter and Setter for energy level
    public int getEnergyLevel() {
        return energyLevel;
    }

    public void setEnergyLevel(int energyLevel) {
        this.energyLevel = energyLevel;
    }

    // Getter and Setter for gadgets
    public List<String> getGadgets() {
        return gadgets;
    }

    public void setGadgets(List<String> gadgets) {
        this.gadgets = gadgets;
    }

    // Getter and Setter for hair
    public String getHair() {
        return hair;
    }

    public void setHair(String hair) {
        this.hair = hair;
    }

    // Getter and Setter for height
    public double getHeight() {
        return height;
    }

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

    // toString method to print minion details
    public String toString() {
        return "Minion{name='" + name + "', color='" + color + "', energyLevel=" + energyLevel + ", gadgets=" + gadgets 
               + ", hair='" + hair + "', height=" + height + "}";
    }
}

// Main class to test the Minion class
public class Main {
    public static void main(String[] args) {
        // Creating a default NPC minion
        Minion defaultMinion = new Minion();

        // Print the default minion details
        System.out.println("=== Default NPC Minion ===");
        System.out.println(defaultMinion);

        // Creating a custom minion
        List<String> gadgets = Arrays.asList("Freeze Ray", "Banana Blaster");
        Minion kevin = new Minion("Yellow", "Kevin", 85, gadgets, "Sprout", 4.1);

        // Print the custom minion details
        System.out.println("\n=== Custom Minion ===");
        System.out.println(kevin);
    }
}
Main.main(null);
=== Default NPC Minion ===
Minion{name='Bob', color='Yellow', energyLevel=70, gadgets=[Banana Launcher, Fart Gun], hair='None', height=3.5}

=== Custom Minion ===
Minion{name='Kevin', color='Yellow', energyLevel=85, gadgets=[Freeze Ray, Banana Blaster], hair='Sprout', height=4.1}

Hack 3

Now please make a parameterized constructor to create the main-character minions easily

import java.util.List;
import java.util.Arrays;

public class Minion {
    // Instance variables
    private String color;
    private String name;
    private int energyLevel;
    private List<String> gadgets;
    private String hair;
    private double height;

    // Parameterized constructor for main-character minions
    public Minion(String color, String name, int energyLevel, List<String> gadgets, String hair, double height) {
        this.color = color;
        this.name = name;
        this.energyLevel = energyLevel;
        this.gadgets = gadgets;
        this.hair = hair;
        this.height = height;
    }

    // Getter and Setter for color
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    // Getter and Setter for name
    public String getName() {
        return name;
    }

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

    // Getter and Setter for energy level
    public int getEnergyLevel() {
        return energyLevel;
    }

    public void setEnergyLevel(int energyLevel) {
        this.energyLevel = energyLevel;
    }

    // Getter and Setter for gadgets
    public List<String> getGadgets() {
        return gadgets;
    }

    public void setGadgets(List<String> gadgets) {
        this.gadgets = gadgets;
    }

    // Getter and Setter for hair
    public String getHair() {
        return hair;
    }

    public void setHair(String hair) {
        this.hair = hair;
    }

    // Getter and Setter for height
    public double getHeight() {
        return height;
    }

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

    // toString method to print minion details
    public String toString() {
        return "Minion{name='" + name + "', color='" + color + "', energyLevel=" + energyLevel + ", gadgets=" + gadgets 
               + ", hair='" + hair + "', height=" + height + "}";
    }
}

// Main class to test the Minion class
public class Main {
    public static void main(String[] args) {
        // Create a custom main-character minion using the parameterized constructor
        List<String> gadgetsKevin = Arrays.asList("Freeze Ray", "Banana Blaster");
        Minion kevin = new Minion("Yellow", "Kevin", 85, gadgetsKevin, "Sprout", 4.1);

        List<String> gadgetsStuart = Arrays.asList("Fart Gun", "Hypno Hat");
        Minion stuart = new Minion("Yellow", "Stuart", 75, gadgetsStuart, "Spiked", 3.8);

        // Print the custom minion details
        System.out.println("=== Main-Character Minions ===");
        System.out.println(kevin);
        System.out.println(stuart);
    }
}
Main.main(null);
=== Main-Character Minions ===
Minion{name='Kevin', color='Yellow', energyLevel=85, gadgets=[Freeze Ray, Banana Blaster], hair='Sprout', height=4.1}
Minion{name='Stuart', color='Yellow', energyLevel=75, gadgets=[Fart Gun, Hypno Hat], hair='Spiked', height=3.8}

Hack 4

Create three minions and print out their values(color, name, energy levels, gadgets, hair, height)

import java.util.List;
import java.util.Arrays;

public class Minion {
    // Instance variables
    private String color;
    private String name;
    private int energyLevel;
    private List<String> gadgets;
    private String hair;
    private double height;

    // Parameterized constructor for minions
    public Minion(String color, String name, int energyLevel, List<String> gadgets, String hair, double height) {
        this.color = color;
        this.name = name;
        this.energyLevel = energyLevel;
        this.gadgets = gadgets;
        this.hair = hair;
        this.height = height;
    }

    // toString method to print minion details
    public String toString() {
        return "Minion{name='" + name + "', color='" + color + "', energyLevel=" + energyLevel + ", gadgets=" + gadgets 
               + ", hair='" + hair + "', height=" + height + "}";
    }
}

public class Main {
    public static void main(String[] args) {
        // Create three minions with different attributes
        List<String> gadgetsKevin = Arrays.asList("Freeze Ray", "Banana Blaster");
        Minion kevin = new Minion("Yellow", "Kevin", 85, gadgetsKevin, "Sprout", 4.1);

        List<String> gadgetsStuart = Arrays.asList("Fart Gun", "Hypno Hat");
        Minion stuart = new Minion("Yellow", "Stuart", 75, gadgetsStuart, "Spiked", 3.8);

        List<String> gadgetsBob = Arrays.asList("Teddy Bear", "Jelly Blaster");
        Minion bob = new Minion("Yellow", "Bob", 90, gadgetsBob, "Bald", 3.5);

        // Print minion details
        System.out.println("=== Minions Details ===");
        System.out.println(kevin);
        System.out.println(stuart);
        System.out.println(bob);
    }
}
Main.main(null);
=== Minions Details ===
Minion{name='Kevin', color='Yellow', energyLevel=85, gadgets=[Freeze Ray, Banana Blaster], hair='Sprout', height=4.1}
Minion{name='Stuart', color='Yellow', energyLevel=75, gadgets=[Fart Gun, Hypno Hat], hair='Spiked', height=3.8}
Minion{name='Bob', color='Yellow', energyLevel=90, gadgets=[Teddy Bear, Jelly Blaster], hair='Bald', height=3.5}

Hack 5

Gru wants to make sure his workers are not overworked as per OSHA. So, Gru wants you to print out the average energy levels of all his Minions. (Hint: you should use static variables)

import java.util.List;
import java.util.Arrays;

public class Minion {
    // Instance variables
    private String color;
    private String name;
    private int energyLevel;
    private List<String> gadgets;
    private String hair;
    private double height;

    // Static variables to keep track of total energy and number of minions
    private static int totalEnergyLevel = 0;
    private static int totalMinions = 0;

    // Parameterized constructor for minions
    public Minion(String color, String name, int energyLevel, List<String> gadgets, String hair, double height) {
        this.color = color;
        this.name = name;
        this.energyLevel = energyLevel;
        this.gadgets = gadgets;
        this.hair = hair;
        this.height = height;
        
        // Update total energy and number of minions whenever a new minion is created
        totalEnergyLevel += energyLevel;
        totalMinions++;
    }

    // Static method to calculate and return average energy level
    public static double getAverageEnergyLevel() {
        return (double) totalEnergyLevel / totalMinions;
    }

    // toString method to print minion details
    public String toString() {
        return "Minion{name='" + name + "', color='" + color + "', energyLevel=" + energyLevel + ", gadgets=" + gadgets 
               + ", hair='" + hair + "', height=" + height + "}";
    }
}

public class Main {
    public static void main(String[] args) {
        // Create three minions with different attributes
        List<String> gadgetsKevin = Arrays.asList("Freeze Ray", "Banana Blaster");
        Minion kevin = new Minion("Yellow", "Kevin", 85, gadgetsKevin, "Sprout", 4.1);

        List<String> gadgetsStuart = Arrays.asList("Fart Gun", "Hypno Hat");
        Minion stuart = new Minion("Yellow", "Stuart", 75, gadgetsStuart, "Spiked", 3.8);

        List<String> gadgetsBob = Arrays.asList("Teddy Bear", "Jelly Blaster");
        Minion bob = new Minion("Yellow", "Bob", 90, gadgetsBob, "Bald", 3.5);

        // Print minion details
        System.out.println("=== Minions Details ===");
        System.out.println(kevin);
        System.out.println(stuart);
        System.out.println(bob);

        // Print the average energy level of all minions
        System.out.println("=== Average Energy Level ===");
        System.out.printf("The average energy level of the minions is: %.2f\n", Minion.getAverageEnergyLevel());
    }
}
Main.main(null);
=== Minions Details ===
Minion{name='Kevin', color='Yellow', energyLevel=85, gadgets=[Freeze Ray, Banana Blaster], hair='Sprout', height=4.1}
Minion{name='Stuart', color='Yellow', energyLevel=75, gadgets=[Fart Gun, Hypno Hat], hair='Spiked', height=3.8}
Minion{name='Bob', color='Yellow', energyLevel=90, gadgets=[Teddy Bear, Jelly Blaster], hair='Bald', height=3.5}
=== Average Energy Level ===
The average energy level of the minions is: 83.33

For 0.90+

Dr. Nefario is trying to assign a recovery time for each minion! Minions who were purple and got cured are very tired, and so are a lot of minions with low energy levels. Create a simple algorithm to calculate how long each minion needs to recover based on their color and energy levels.

import java.util.List;
import java.util.Arrays;

public class Minion {
    // Instance variables
    private String color;
    private String name;
    private int energyLevel;
    private List<String> gadgets;
    private String hair;
    private double height;

    // Static variables to track total energy and number of minions
    private static int totalEnergyLevel = 0;
    private static int totalMinions = 0;

    // Parameterized constructor for minions
    public Minion(String color, String name, int energyLevel, List<String> gadgets, String hair, double height) {
        this.color = color;
        this.name = name;
        this.energyLevel = energyLevel;
        this.gadgets = gadgets;
        this.hair = hair;
        this.height = height;
        
        totalEnergyLevel += energyLevel;
        totalMinions++;
    }

    // Getter methods
    public String getColor() {
        return this.color;
    }

    public int getEnergyLevel() {
        return this.energyLevel;
    }

    // Method to calculate recovery time based on color and energy level
    public int calculateRecoveryTime() {
        int recoveryTime = 0;

        // Add base recovery time for purple minions
        if (this.color.equalsIgnoreCase("purple")) {
            recoveryTime += 10;  // Purple minions need more time to recover
        }

        // Add recovery time based on low energy levels
        if (this.energyLevel < 50) {
            recoveryTime += (50 - this.energyLevel) / 5;  // The lower the energy, the longer the recovery
        }

        // Base recovery time for all minions
        recoveryTime += 5;

        return recoveryTime;
    }

    // toString method to print minion details
    public String toString() {
        return "Minion{name='" + name + "', color='" + color + "', energyLevel=" + energyLevel + ", gadgets=" + gadgets 
               + ", hair='" + hair + "', height=" + height + "}";
    }
}

public class Main {
    public static void main(String[] args) {
        // Create minions with different attributes
        List<String> gadgetsKevin = Arrays.asList("Freeze Ray", "Banana Blaster");
        Minion kevin = new Minion("Yellow", "Kevin", 85, gadgetsKevin, "Sprout", 4.1);

        List<String> gadgetsStuart = Arrays.asList("Fart Gun", "Hypno Hat");
        Minion stuart = new Minion("Purple", "Stuart", 30, gadgetsStuart, "Spiked", 3.8);

        List<String> gadgetsBob = Arrays.asList("Teddy Bear", "Jelly Blaster");
        Minion bob = new Minion("Yellow", "Bob", 45, gadgetsBob, "Bald", 3.5);

        // Print minion details and their calculated recovery times
        System.out.println("=== Minions and Recovery Times ===");
        System.out.println(kevin);
        System.out.println("Kevin's recovery time: " + kevin.calculateRecoveryTime() + " days\n");

        System.out.println(stuart);
        System.out.println("Stuart's recovery time: " + stuart.calculateRecoveryTime() + " days\n");

        System.out.println(bob);
        System.out.println("Bob's recovery time: " + bob.calculateRecoveryTime() + " days\n");
    }
}
Main.main(null);
=== Minions and Recovery Times ===
Minion{name='Kevin', color='Yellow', energyLevel=85, gadgets=[Freeze Ray, Banana Blaster], hair='Sprout', height=4.1}
Kevin's recovery time: 5 days

Minion{name='Stuart', color='Purple', energyLevel=30, gadgets=[Fart Gun, Hypno Hat], hair='Spiked', height=3.8}
Stuart's recovery time: 19 days

Minion{name='Bob', color='Yellow', energyLevel=45, gadgets=[Teddy Bear, Jelly Blaster], hair='Bald', height=3.5}
Bob's recovery time: 6 days