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);
}
}
// Create instances of Student
Student student1 = new Student("Alice", 85);
Student student2 = new Student("Bob", 65);
// Check if students pass
System.out.println(student1.getName() + " passes: " + student1.pass());
System.out.println(student2.getName() + " passes: " + student2.pass());
Alice passes: true
Bob passes: false
// Make your own class with an integer variable
// Make a constructor for that
// Create an object and print the variable
class Cat {
String name;
Cat(String name) {
this.name = name;
}
}
Cat myCat = new Cat("Whiskers");
System.out.println("The name of the cat is: " + myCat.name);
The name of the cat is: Whiskers
// Make a no-arg constructor
class Dog {
String name;
Dog() {
this.name = "Buddy";
}
}
// Make a parameterized constructor
class Bird {
String name;
Bird(String name) {
this.name = name;
}
}
// “Make” a default constructor
class Cat {
String name;
Cat() {
this.name = "Whiskers";
}
}
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;
// Create skillLevel instance variable
private int skillLevel;
public Minion(String n, String t)
{
name = n;
task = t;
}
// Getter Methods
public String getName()
{
return name;
}
public String getTask()
{
return task;
}
// Add getter method here
public int getSkillLevel()
{
return skillLevel;
}
}
Minion Stuart = new Minion("Stuart", "Developing propulsion system");
System.out.println(Stuart.getName());
System.out.println(Stuart.getTask());
// Add print statement to get skillLevel
System.out.println(Stuart.getSkillLevel());
Stuart
Developing propulsion system
0
// 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
{
// Start by defining instance variables that you'll want to access later via the accessor methods
private double height;
private String name;
private String hair;
private int eyes;
// Default Constructor
//String n, int c
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;
}
// Accessor Methods!
public String toString()
{
return "Height: " + height + "\nName: " + name + "\nHair: " + hair + "\nEyes: " + eyes;
}
}
// Create minion object Kevin
Minion kevin = new Minion(4.10,"Kevin","Sprout-Cut",2);
// Print Kevin Object
System.out.println(kevin);
Height: 4.1
Name: Kevin
Hair: Sprout-Cut
Eyes: 2
public class Minion
{
private String name;
// same naming scheme as a getter but with set instead
public void setName(String n) {
name = n;
}
}
public class Cat {
private String name;
// Setter method for name
public void setName(String n) {
name = n;
}
// Getter method for name
public String getName() {
return name;
}
}
Cat myCat = new Cat();
myCat.setName("Whiskers");
System.out.println(myCat.getName());
Whiskers
public class Cat
{
public String name;
public int age;
public Cat(String n, int a)
{
name = n;
age = a;
}
//getter
public String getName()
{
return name;
}
//setter
public void setName(String n)
{
name = n;
}
public void setAge(int a)
{
age = a;
}
}
Cat myCat = new Cat("Whiskers", 3);
System.out.println(myCat.getName());
myCat.setName("Fluffy");
System.out.println(myCat.getName());
Whiskers
Fluffy
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. LOOK HERE!!
public void addMinion(String minion) {
minions.add(minion);
System.out.println(minion + " has been added to " + name + "'s army.");
}
public void deleteMinion(String minion) {
minions.remove(minion);
System.out.println(minion + " has been removed from " + 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;
}
}
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");
gru.deleteMinion("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();
// 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.
Bob has been removed from Gru's army.
Henchman 1 has been added to Vector's army.
=== Villain Descriptions ===
Gru is planning to: steal the moon!
They have 2 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.
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
}
// Override toString method to provide a meaningful representation of the gadget
@Override
public String toString() {
return "Gadget{name='" + gadgetName + "'}";
}
}
public class Main {
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);
System.out.println("Gadget List: " + Gadget.gadgetsList); // Print the list of gadgets
}
}
Main.main(null);
Total gadgets made: 3
Gadget List: [Gadget{name='Freeze Ray'}, Gadget{name='Banana Blaster'}, Gadget{name='Lipstick Taser'}]
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
private String gadgetName; // Instance variable to store the name of the gadget
private double cost; // 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
public Gadget(String gadgetName, double cost) {
this.gadgetName = gadgetName;
this.cost = cost;
totalGadgets++; // Increment the total gadgets count
gadgetsList.add(this); // Add this gadget to the static list
}
// Override toString method to provide a meaningful representation of the gadget
@Override
public String toString() {
return "Gadget{name='" + gadgetName + "', cost=" + cost + "}";
}
// Static method to calculate the total cost of all gadgets
public static double calculateTotalCost() {
double totalCost = 0;
for (Gadget gadget : gadgetsList) {
totalCost += gadget.cost;
}
return totalCost;
}
}
// Create three gadgets with cost
Gadget g1 = new Gadget("Freeze Ray", 1000);
Gadget g2 = new Gadget("Banana Blaster", 500);
Gadget g3 = new Gadget("Lipstick Taser", 750);
// Print the total number of gadgets
System.out.println("Total gadgets made: " + Gadget.totalGadgets);
System.out.println("Gadget List: " + Gadget.gadgetsList); // Print the list of gadgets
// Calculate the total cost of all gadgets
System.out.println("Total cost of all gadgets: $" + Gadget.calculateTotalCost());
Total gadgets made: 3
Gadget List: [Gadget{name='Freeze Ray', cost=1000.0}, Gadget{name='Banana Blaster', cost=500.0}, Gadget{name='Lipstick Taser', cost=750.0}]
Total cost of all gadgets: $2250.0
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.
public class MinionMood {
private int happinessLevel;
private int energyLevel;
private int bananas;
private int tasks;
public MinionMood(int bananas, int tasks) {
this.happinessLevel = 2 * bananas; // Use 'this' to refer to the instance variable
this.energyLevel = tasks; // Use 'this' to refer to the instance variable
}
public String toString() {
return "Happiness Level: " + happinessLevel + "\nEnergy Level: " + energyLevel;
}
}
MinionMood bob = new MinionMood(5, 2);
System.out.println(bob);
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.
public class Minion {
private String speed;
private int height;
// Correct constructor without a return type
public 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;
}
}
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));
minion 1 speed: fast
Is minion1 taller than minion2? true
Is minion1 still taller than minion2? false
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.ArrayList;
import java.util.List;
public class Minion {
private String color;
private String name;
private int energyLevel;
private List<String> gadgets;
private String hair;
private int height;
public Minion(String color, String name, int energyLevel, String hair, int height) {
this.color = color;
this.name = name;
this.energyLevel = energyLevel;
this.gadgets = new ArrayList<>();
this.hair = hair;
this.height = height;
}
public void addGadget(String gadget) {
gadgets.add(gadget);
}
public void describeMinion() {
System.out.println("Minion: " + name);
System.out.println("Color: " + color);
System.out.println("Energy Level: " + energyLevel);
System.out.println("Hair: " + hair);
System.out.println("Height: " + height);
System.out.println("Gadgets: " + gadgets);
}
// Getter method for name
public String getName() {
return name;
}
}
Minion minion1 = new Minion("Purple", "Dave", 5, "Bald", 105);
minion1.addGadget("Freeze Ray");
minion1.addGadget("Lipstick Taser");
minion1.describeMinion();
Minion minion2 = new Minion("Yellow", "Kevin", 3, "Short", 95);
minion2.addGadget("Banana Blaster");
minion2.describeMinion();
// Print out the minion that needs to be cured
System.out.println("Minion that needs to be cured: " + minion1.getName());
Minion: Dave
Color: Purple
Energy Level: 5
Hair: Bald
Height: 105
Gadgets: [Freeze Ray, Lipstick Taser]
Minion: Kevin
Color: Yellow
Energy Level: 3
Hair: Short
Height: 95
Gadgets: [Banana Blaster]
Minion that needs to be cured: Dave
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.
class Minions
{
private String color;
private String name;
private int energyLevel;
private List<String> gadgets;
private String hair;
private int height;
// Default Constructor
public Minions()
{
color = "Yellow";
name = "Bob";
energyLevel = 3;
gadgets = new ArrayList<>();
hair = "Bald";
height = 105;
}
public void addGadget(String gadget) {
gadgets.add(gadget);
}
public void describeMinion() {
System.out.println("Minion: " + name);
System.out.println("Color: " + color);
System.out.println("Energy Level: " + energyLevel);
System.out.println("Hair: " + hair);
System.out.println("Height: " + height);
System.out.println("Gadgets: " + gadgets);
}
// Getter method for name
public String getName() {
return name;
}
}
Hack 3 Now please make a parameterized constructor to create the main-character minions easily.
class Parameterized {
private String name;
private int height;
// Parameterized Constructor
public Parameterized(String n, int h) {
name = n;
height = h;
}
// Getter method for name
public String getName() {
return name;
}
}
Parameterized p = new Parameterized("Bob", 105);
Parameterized p1 = new Parameterized("Kevin", 95);
Parameterized p2 = new Parameterized("Stuart", 100);
Parameterized p3 = new Parameterized("Dave", 110);
// Print out the names of all the minions
System.out.println(p.getName());
System.out.println(p1.getName());
System.out.println(p2.getName());
System.out.println(p3.getName());
Bob
Kevin
Stuart
Dave
Hack 4 Create three minions and print out their values(color, name, energy levels, gadgets, hair, height)
import java.util.ArrayList;
import java.util.List;
class Minions {
private String color;
private String name;
private int energyLevel;
private List<String> gadgets;
private String hair;
private int height;
// Default Constructor
public Minions() {
color = "Yellow";
name = "Bob";
energyLevel = 3;
gadgets = new ArrayList<>();
hair = "Bald";
height = 105;
}
public void addGadget(String gadget) {
gadgets.add(gadget);
}
// Getter method for name
public String getName() {
return name;
}
}
Minions minion = new Minions();
minion.addGadget("Freeze Ray");
minion.addGadget("Lipstick Taser");
// Print out the minion that needs to be cured
System.out.println("Minion that needs to be cured: " + minion.getName());
Minion that needs to be cured: Bob
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)
class EnergyLevels
{
private int energyLevel;
// Default Constructor
public EnergyLevels()
{
energyLevel = 3;
}
// Getter method for energyLevel
public int getEnergyLevel()
{
return energyLevel;
}
}
EnergyLevels e = new EnergyLevels();
System.out.println("Energy Level: " + e.getEnergyLevel());
Energy Level: 3
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;
import java.util.List;
class Minion {
private String color;
private String name;
private int energyLevel;
private List<String> gadgets;
private String hair;
private int height;
private int recoveryTime;
// Default Constructor
public Minion(String color, String name, int energyLevel, String hair, int height) {
this.color = color;
this.name = name;
this.energyLevel = energyLevel;
this.gadgets = new ArrayList<>();
this.hair = hair;
this.height = height;
this.recoveryTime = calculateRecoveryTime();
}
public void addGadget(String gadget) {
gadgets.add(gadget);
}
// Getter method for name
public String getName() {
return name;
}
// Method to calculate recovery time
private int calculateRecoveryTime() {
int baseRecoveryTime = 5; // Base recovery time in days
if (color.equalsIgnoreCase("Purple")) {
baseRecoveryTime += 10; // Additional recovery time for purple minions
}
if (energyLevel < 3) {
baseRecoveryTime += 5; // Additional recovery time for low energy levels
}
return baseRecoveryTime;
}
// Getter method for recovery time
public int getRecoveryTime() {
return recoveryTime;
}
public void describeMinion() {
System.out.println("Minion: " + name);
System.out.println("Color: " + color);
System.out.println("Energy Level: " + energyLevel);
System.out.println("Hair: " + hair);
System.out.println("Height: " + height);
System.out.println("Gadgets: " + gadgets);
System.out.println("Recovery Time: " + recoveryTime + " days");
}
}
Minion minion1 = new Minion("Purple", "Dave", 2, "Bald", 105);
minion1.addGadget("Freeze Ray");
minion1.addGadget("Lipstick Taser");
Minion minion2 = new Minion("Yellow", "Kevin", 4, "Short", 95);
minion2.addGadget("Banana Blaster");
Minion minion3 = new Minion("Yellow", "Stuart", 1, "Spiky", 100);
minion3.addGadget("Fart Gun");
// Describe minions and print their recovery time
minion1.describeMinion();
minion2.describeMinion();
minion3.describeMinion();
Minion: Dave
Color: Purple
Energy Level: 2
Hair: Bald
Height: 105
Gadgets: [Freeze Ray, Lipstick Taser]
Recovery Time: 20 days
Minion: Kevin
Color: Yellow
Energy Level: 4
Hair: Short
Height: 95
Gadgets: [Banana Blaster]
Recovery Time: 5 days
Minion: Stuart
Color: Yellow
Energy Level: 1
Hair: Spiky
Height: 100
Gadgets: [Fart Gun]
Recovery Time: 10 days