Classes
hacks for clases
Homework Hacks
import java.awt.Color;
class Gadget{
String name;
Gadget(){
this.name = "{Null}";
}
Gadget(String name){
this.name = name;
}
public String toString(){
return "Gadget: " + this.name;
}
}
class Minion{
static int[] energyLevels = new int[0];
static double averageEnergyLevels(){
double sum = 0;
for (int value : Minion.energyLevels){
sum += (Integer.valueOf(value)).doubleValue();
}
return sum/(Minion.energyLevels.length);
}
String name;
Color color;
int energyLevel;
Gadget[] gadgets;
String hair;
double height;
Minion(){
this.name = "{Null}";
this.color = new Color(255,255,0);
this.energyLevel = 0;
this.gadgets = new Gadget[1];
this.hair = "Bald";
this.height = 3.583;
int[] tempArray = Minion.energyLevels;
Minion.energyLevels = new int[tempArray.length + 1];
for (int i = 0; i< tempArray.length; i++){
Minion.energyLevels[i] = tempArray[i];
}
Minion.energyLevels[Minion.energyLevels.length - 1] = this.energyLevel;
}
Minion(String name, Color color, int energyLevel, Gadget[] gadgets, String hair, double height){
this.name = name;
this.color = color;
this.energyLevel = energyLevel;
this.gadgets = gadgets;
this.hair = hair;
this.height = height;
int[] tempArray = Minion.energyLevels;
Minion.energyLevels = new int[tempArray.length + 1];
for (int i = 0; i< tempArray.length; i++){
Minion.energyLevels[i] = tempArray[i];
}
Minion.energyLevels[Minion.energyLevels.length - 1] = this.energyLevel;
}
public double recoveryTime(){
int red = this.color.getRed();
int green = this.color.getGreen();
int blue = this.color.getBlue();
Double dRed = new Double(255-red);
Double dGreen = new Double(255-green);
Double dBlue = new Double(blue);
Double distance = Math.sqrt(Math.pow(dRed,2) + Math.pow(dGreen,2) + Math.pow(dBlue,2));
return distance.doubleValue()/(this.energyLevel + 1);
}
@Override
public String toString(){
String returnMe = "[";
returnMe = returnMe + this.name + ", ";
returnMe = returnMe + this.color.toString() + ", ";
returnMe = returnMe + (new Integer(this.energyLevel)).toString() + ", ";
returnMe = returnMe + Arrays.toString(this.gadgets) + ", ";
returnMe = returnMe + this.hair + ", ";
returnMe = returnMe + (new Double(height)).toString() + "]";
return returnMe;
}
}
//minion made with default constructor
Minion myMinion = new Minion();
//minion made with ~permartized constructor
Gadget[] gads = new Gadget[1];
Minion Bob = new Minion("Bob",new Color(255,0,255),8,gads,"Brown",4);
//another one
Gadget[] gads2 = new Gadget[2];
gads2[0] = new Gadget("Freeze Ray");
gads2[1] = new Gadget("Fart Gun");
Minion Jerry = new Minion("Jerry",new Color(55,255,100),3,gads2,"Black",3);
//print out minions
System.out.println(myMinion.toString());
System.out.println(Bob.toString());
System.out.println(Jerry.toString());
//print out average energy of all minions
System.out.println(Minion.averageEnergyLevels());
//print out recovery time of the minions
System.out.println(myMinion.recoveryTime());
System.out.println(Jerry.recoveryTime());
System.out.println(Bob.recoveryTime());
[{Null}, java.awt.Color[r=255,g=255,b=0], 0, [null], Bald, 3.583]
[Bob, java.awt.Color[r=255,g=0,b=255], 8, [null], Brown, 4.0]
[Jerry, java.awt.Color[r=55,g=255,b=100], 3, [Gadget: Freeze Ray, Gadget: Fart Gun], Black, 3.0]
3.6666666666666665
0.0
55.90169943749474
40.069384267237695
Popcorn Hacks
class Student{
String name;
int grade;
Student(String name, int grade){
this.name = name;
this.grade = grade;
}
public String getName(){
return this.name;
}
public int getGrade(){
return this.grade;
}
public boolean hasPassed(){
return this.grade >= 70;
}
}
Student mort = new Student("Mort", 65);
System.out.println(mort.hasPassed());
false
class Water{
String type;
Water(){
this.type = "static";
}
Water(String type){
this.type = type;
}
public String getType(){
return this.type;
}
}
Water staticW = new Water();
Water moving = new Water("flowing");
System.out.println(staticW.getType());
System.out.println(moving.getType());
public class Minion {
private String name;
private String task;
private int skillLevel;
// Create skillLevel instance variable
public Minion(String n, String t, int s)
{
name = n;
task = t;
this.skillLevel = s;
}
// Getter Methods
public String getName()
{
return name;
}
public String getTask()
{
return task;
}
public String getSkillLevel()
{
return this.skillLevel;
}
// Add getter method here
}
Minion Stuart = new Minion("Stuart", "Developing propulsion system");
System.out.println(Stuart.getName());
System.out.println(Stuart.getTask());
System.out.println(Stuart.getSkillLevel());
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 "Minion: " + this.name + ", " + this.height + ", " + this.hair + ", " + this.eyes;
}
}
// Create minion object Kevin
Minion kevin = new Minion(4.10,"Kevin","Sprout-Cut",2);
// Print Kevin Object
System.out.println(kevin);
class Banana{
int rarity;
Banana(int rarity){
this.rarity = rarity;
}
public setRarity(int r){
this.rarity = r;
}
public int getRarity(){
return this.rarity;
}
}
Banana myBanana = new Banana(0);
myBanana.setRarity(2);
System.out.println(myBanana.getRarity());
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.");
}
// 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 static boolean enoughVillainsToGetTheMoon() {
return villainCount > 37;
}
}
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");
// 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);
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
private double price;
// Constructor to set the gadget name and increment totalGadgets
public Gadget(String gadgetName, double price) {
this.price = price;
this.gadgetName = gadgetName;
totalGadgets++; // Increment the total gadgets count
gadgetsList.add(this); // Add this gadget to the static list
}
// Getter for the gadget name
public String getGadgetName() {
return gadgetName;
}
// Static method to print all gadgets in the list
public static void printAllGadgets() {
System.out.println("Gadgets created:");
for (int i = 0; i < gadgetsList.size(); i++) {
System.out.println("- " + gadgetsList.get(i).getGadgetName());
}
}
// Static method to print all gadgets in the list
public static void printAllGadgetsPrice() {
double sum = 0;
gadgetsList.forEach(Gadget gad, {
sum += gad.price;
})
System.out.println("Price: " + gad.price);
}
}
// In the Main class:
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);
// Print all gadgets stored in the static list
Gadget.printAllGadgets();
Gadget.printAllGadgetsPrice();
}
}
Main.main(null);