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

Homework

Unit 5 Homework (Period 3)

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

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.

Hack 3

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

Hack 4

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

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)

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.

class Minion {
    private String color;
    private String name;
    private int energyLevels;
    private String gadgets;
    private String hair;
    private double height;

    private static int totalEnergyLevels = 0;
    private static int numberOfMinions = 0;

    public Minion() {
        this.color = "Yellow";
        this.name = "Minion";
        this.energyLevels = 100;
        this.gadgets = "None";
        this.hair = "Short";
        this.height = 1.5;
        totalEnergyLevels += this.energyLevels;
        numberOfMinions++;
    }

    public Minion(String color, String name, int energyLevels, String gadgets, String hair, double height) {
        this.color = color;
        this.name = name;
        this.energyLevels = energyLevels;
        this.gadgets = gadgets;
        this.hair = hair;
        this.height = height;
        totalEnergyLevels += this.energyLevels;
        numberOfMinions++;
    }

    public void printDetails() {
        System.out.println("Color: " + color);
        System.out.println("Name: " + name);
        System.out.println("Energy Levels: " + energyLevels);
        System.out.println("Gadgets: " + gadgets);
        System.out.println("Hair: " + hair);
        System.out.println("Height: " + height + " meters");
        System.out.println("Recovery Time: " + calculateRecoveryTime() + " hours\n");
    }

    public double calculateRecoveryTime() {
        double recoveryTime = 0;
        if (color.equalsIgnoreCase("Purple")) {
            recoveryTime += 5;
        }
        recoveryTime += (100 - energyLevels) / 20.0;
        return recoveryTime;
    }

    public static double getAverageEnergyLevels() {
        if (numberOfMinions == 0) {
            return 0;
        }
        return (double) totalEnergyLevels / numberOfMinions;
    }

    public static void main(String[] args) {
        // Create three minions
        Minion minion1 = new Minion("Yellow", "Bob", 80, "Laser", "Short", 1.4);
        Minion minion2 = new Minion("Purple", "Kevin", 60, "Grappling Hook", "Curly", 1.6);
        Minion minion3 = new Minion();

        minion1.printDetails();
        minion2.printDetails();
        minion3.printDetails();

        System.out.println("Average Energy Levels of all Minions: " + getAverageEnergyLevels());
    }
}
Minion.main(null);
Color: Yellow
Name: Bob
Energy Levels: 80
Gadgets: Laser
Hair: Short
Height: 1.4 meters
Recovery Time: 1.0 hours

Color: Purple
Name: Kevin
Energy Levels: 60
Gadgets: Grappling Hook
Hair: Curly
Height: 1.6 meters
Recovery Time: 7.0 hours

Color: Yellow
Name: Minion
Energy Levels: 100
Gadgets: None
Hair: Short
Height: 1.5 meters
Recovery Time: 0.0 hours

Average Energy Levels of all Minions: 80.0