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.
import java.util.ArrayList;
class Minion {
private String color;
private String name;
private int energyLevel;
private String[] gadgets;
private String hair;
private int height;
private static ArrayList<Integer> energyLevels = new ArrayList<Integer>();
/**
* Creates a default minion
*/
public Minion() {
this("yellow", "skibidi", 23, new String[] {"sword"}, "curly", 1);
}
/**
* Creates a new minion.
*
* @param color The color of the minion.
* @param name The name of the minion
* @param energyLevel The energy level of the minion
* @param gadgeets The minion's gadgets
* @param hair The minion's hair type
* @param height The minion's height, in feet
*/
public Minion(String color, String name, int energyLevel, String[] gadgets, String hair, int height) {
this.color = color;
this.name = name;
this.energyLevel = energyLevel;
this.gadgets = gadgets;
this.hair = hair;
this.height = height;
this.energyLevels.add(energyLevel);
}
/**
* Prints information about the minion
*
* @return A string containing information about the minion
*/
public String toString() {
String gadgetStr = "";
for (int i = 0; i < gadgets.length; i++) {
gadgetStr += gadgets[i];
if (i < gadgets.length-1) {
gadgetStr += ", ";
}
}
return "Color: " + color + "\nName: " + name + "\nEnergy Level: " + energyLevel + "\nHair: " + hair + "\nHeight: " + height + "\nGadgets: " + gadgetStr + "\nRecovery Time: " + getRecoveryTime() + " days";
}
/**
* Gets the time the amount of time the minion must rest in order to recover
*
* @return The amount of days the minion must rest
*/
public int getRecoveryTime() {
// Formula:
// If yellow, then there is a minimum of 5 days of rest and a multiplier of 2 for additional rest
// Additional rest: 1/energyLevel * constant
// >>> After experimentation on the minions, the best constant was found to be 510
int recoveryTime = 0;
double constant = 510;
if (color.toLowerCase().equals("yellow")) {
recoveryTime += 5;
constant *= 2;
}
recoveryTime += (int) (1 / ((double) energyLevel) * constant);
return recoveryTime;
}
/**
* Gets the average energy level of all the minions
*
* @return The average energy level of all the minions
*/
public static int averageEnergyLevel() {
int sum = 0;
for (int i = 0; i < energyLevels.size(); i++) {
sum += energyLevels.get(i);
}
return sum/energyLevels.size();
}
}
class Main {
public static void main(String[] args) {
Minion basic = new Minion();
Minion ohio = new Minion("purple", "ohio", 234, new String[]{"shield", "asdfghjkl"}, "straight", 23);
Minion bob = new Minion("yellow", "bob", 234, new String[]{"monkey"}, "bald", 4278);
System.out.println(basic + "\n");
System.out.println(ohio + "\n");
System.out.println(bob + "\n");
System.out.println("Average Energy Level: " + Minion.averageEnergyLevel());
}
}
Main.main(null);
Color: yellow
Name: skibidi
Energy Level: 23
Hair: curly
Height: 1
Gadgets: sword
Recovery Time: 49 days
Color: purple
Name: ohio
Energy Level: 234
Hair: straight
Height: 23
Gadgets: shield, asdfghjkl
Recovery Time: 2 days
Color: yellow
Name: bob
Energy Level: 234
Hair: bald
Height: 4278
Gadgets: monkey
Recovery Time: 9 days
Average Energy Level: 163