Home | 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.
Hack 1
public class Minion {
// Instance variables
private String color;
private String name;
private int energyLevel;
private String gadget;
private String hair;
private double height;
// Constructor
public Minion(String color, String name, int energyLevel, String gadget, String hair, double height) {
this.color = color;
this.name = name;
this.energyLevel = energyLevel;
this.gadget = gadget;
this.hair = hair;
this.height = height;
}
// Getter method to return minion details
public String getDetails() {
return "Color: " + color + ", Name: " + name + ", Energy Level: " + energyLevel + ", Gadget: " + gadget + ", Hair: " + hair + ", Height: " + height;
}
// Main method to run the program
public static void main(String[] args) {
// Creating a Minion object
Minion minion = new Minion("Yellow", "Dave", 80, "Laser Gun", "Short", 3.5);
// Displaying the minion's details
System.out.println(minion.getDetails());
}
}
Minion.main(null);
Color: Yellow, Name: Dave, Energy Level: 80, Gadget: Laser Gun, Hair: Short, Height: 3.5
Hack 2
public class Minion {
// Attributes
String color;
String name;
int energyLevel;
String gadget;
String hair;
double height;
// Default constructor
public Minion() {
this.color = "Yellow";
this.name = "Bob";
this.energyLevel = 100;
this.gadget = "None";
this.hair = "None";
this.height = 3.0; // Height in feet
}
// Main method to test the Minion class
public static void main(String[] args) {
// Create a Minion object using the default constructor
Minion minion = new Minion();
// Output the details of the Minion object
System.out.println("Minion details:");
System.out.println("Name: " + minion.name);
System.out.println("Color: " + minion.color);
System.out.println("Energy Level: " + minion.energyLevel);
System.out.println("Gadget: " + minion.gadget);
System.out.println("Hair: " + minion.hair);
System.out.println("Height: " + minion.height + " feet");
}
}
Minion.main(null);
Minion details:
Name: Bob
Color: Yellow
Energy Level: 100
Gadget: None
Hair: None
Height: 3.0 feet
Hack 3
The parameterized constructor is already implemented in Hack 1, but you can call it while creating instances of the Minion class.
Hack 4
public class Main {
public static void main(String[] args) {
// Creating three minions
Minion minion1 = new Minion("Purple", "Stuart", 80, "Jetpack", "Spiky", 4.0);
Minion minion2 = new Minion("Yellow", "Kevin", 90, "Laser Gun", "None", 3.5);
Minion minion3 = new Minion("Purple", "Bob", 70, "Banana Launcher", "Curly", 3.2);
// Print details of the minions
System.out.println(minion1.getDetails());
System.out.println(minion2.getDetails());
System.out.println(minion3.getDetails());
}
}
Main.main(null);
Color: Purple, Name: Stuart, Energy Level: 80, Gadget: Jetpack, Hair: Spiky, Height: 4.0
Color: Yellow, Name: Kevin, Energy Level: 90, Gadget: Laser Gun, Hair: None, Height: 3.5
Color: Purple, Name: Bob, Energy Level: 70, Gadget: Banana Launcher, Hair: Curly, Height: 3.2
Hack 5
public class Minion {
// Instance variables
private String color;
private String name;
private int energyLevel;
private String gadget;
private String hair;
private double height;
// Static variables to calculate average energy levels
private static int totalEnergy = 0;
private static int minionCount = 0;
// Constructor
public Minion(String color, String name, int energyLevel, String gadget, String hair, double height) {
this.color = color;
this.name = name;
this.energyLevel = energyLevel;
this.gadget = gadget;
this.hair = hair;
this.height = height;
totalEnergy += energyLevel;
minionCount++;
}
// Method to calculate average energy level
public static double getAverageEnergyLevel() {
return (minionCount > 0) ? (double) totalEnergy / minionCount : 0;
}
}
// In the main class
public class Main {
public static void main(String[] args) {
// Create three minions
Minion minion1 = new Minion("Purple", "Stuart", 80, "Jetpack", "Spiky", 4.0);
Minion minion2 = new Minion("Yellow", "Kevin", 90, "Laser Gun", "None", 3.5);
Minion minion3 = new Minion("Purple", "Bob", 70, "Banana Launcher", "Curly", 3.2);
// Print the average energy level of all minions
System.out.println("Average Energy Level: " + Minion.getAverageEnergyLevel());
}
}
Main.main(null);
Average Energy Level: 80.0