1.6 HW Hacks
Popcorn Hack 1
int playerScore = 1000;
int playerHealth = 100;
int enemiesDefeated = 0;
// Player defeats an enemy worth 250 points
playerScore += 250;
// Player takes 15 damage
playerHealth -= 15;
// Enemy count goes up
enemiesDefeated++;
// Boss battle: double the current score!
playerScore *= 2;
// Healing potion restores health to 80% of current
playerHealth *=4;
playerHealth /=5;
Popcorn Hack 2
public class Main {
public static void main(String[] args) {
int score = 100;
score += 67;
System.out.println(score);
score -= 100;
System.out.println(score);
score *= 2;
System.out.println(score);
}
}
Main.main(null);
167
67
134
Homework Hack 1
Chosen Option: Bank Account Manager
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Initalize variables
double balance = 10000;
double interestRate = 0.05;
// Print introduction and menu of options
System.out.println("Hello! Welcome to the Bank of the United States");
System.out.println("To interact with our system, select one the following numbers: ");
System.out.println("1. Check your balance");
System.out.println("2. Add to your balance");
System.out.println("3. Withdraw from your balance");
System.out.println("4. Open and deposit into a savings account");
int response = scanner.nextInt();
// React accordingly to their selected option
if (response == 1) {
System.out.printf("Your balance is $%,.2f", balance);
} else if (response == 2) {
// Prompt user
System.out.print("Enter how much money you would like to deposit: ");
double deposit = scanner.nextDouble();
// Add to balance
balance += deposit;
// Print new balance
System.out.printf("\nYour new balance is $%,.2f", balance);
} else if (response == 3) {
// Prompt user
System.out.print("Enter how much money you would like to withdraw: ");
double withdraw = scanner.nextDouble();
// Remove from balance
balance -= withdraw;
// Print new balance
System.out.printf("\nYour new balance is $%,.2f", balance);
} else if (response == 4) {
// Prompt user
System.out.println("The offered interest rate is 5% per year");
System.out.print("Enter how much would you like to deposit to this savings account: ");
double savings = scanner.nextDouble();
savings *= 0.05;
System.out.printf("\nYou will make $%,.2f in the next year", savings);
}
scanner.close();
}
}
Main.main(null);
Hello! Welcome to the Bank of the United States
To interact with our system, select one the following numbers:
1. Check your balance
2. Add to your balance
3. Withdraw from your balance
4. Open and deposit into a savings account
The offered interest rate is 5% per year
Enter how much would you like to deposit to this savings account:
You will make $500.00 in the next year