Unit 1.6 - Compound Assignment Operators
Categories: homeworkJava homework assignment covering compound assignment operators and their use cases
Option B: Bank Account Manager I wanted to create a Bank Account Manager simulation that tracks financial transactions over a month.
public class BankAccountManager {
public static void main(String[] args) {
// Initial account setup
int balance = 5000;
int transactions = 0;
System.out.println("=== BANK ACCOUNT MANAGER ===\n");
System.out.println("Starting balance: $" + balance + "\n");
// Salary deposit
balance += 3500;
transactions++;
System.out.println("Salary deposited: $" + balance);
// Rent payment
balance -= 1800;
transactions++;
System.out.println("Rent paid: $" + balance);
// Groceries
balance -= 450;
transactions++;
System.out.println("Groceries: $" + balance);
// Apply 1.5% interest (multiply by 1.015)
balance *= 1.015;
System.out.println("Interest applied: $" + balance);
// Calculate average spent per transaction
int totalSpent = 1800 + 450;
int avgSpent = totalSpent / transactions;
System.out.println("Average spent: $" + avgSpent);
// Check account tier eligibility
int tier = balance % 5;
System.out.println("Tier check: " + tier);
System.out.println("\n=== FINAL SUMMARY ===");
System.out.println("Transactions: " + transactions);
System.out.println("Final balance: $" + balance);
}
}
=== BANK ACCOUNT MANAGER ===
Starting balance: $5000
Salary deposited: $8500
Rent paid: $6700
Groceries: $6250
Interest applied: $6343
Average spent: $750
Tier check: 3
=== FINAL SUMMARY ===
Transactions: 3
Final balance: $6343
Popcorn Hack #1 Original Code:
int playerScore = 1000;
int playerHealth = 100;
int enemiesDefeated = 0;
// Player defeats an enemy worth 250 points
playerScore = playerScore + 250;
// Player takes 15 damage
playerHealth = playerHealth - 15;
// Enemy count goes up
enemiesDefeated = enemiesDefeated + 1;
// Boss battle: double the current score!
playerScore = playerScore * 2;
// Healing potion restores health to 80% of current
playerHealth = playerHealth * 4;
playerHealth = playerHealth / 5;
Transformed Code:
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 ScoreTracker {
public static void main(String[] args) {
int score = 100;
System.out.println("Starting score: " + score);
// Wrong answer penalty
score -= 25;
System.out.println("After wrong answer: " + score);
// Power-up doubles the score
score *= 2;
System.out.println("After power-up: " + score);
// Find remainder after dividing by 7
score %= 7;
System.out.println("After modulo 7: " + score);
}
}
Output:
Starting score: 100 After wrong answer: 75 After power-up: 150 After modulo 7: 3