Compound Assignment Operators
- Popcorn Hack #1: Transform Beginner Code
- Popcorn Hack 2: Score Manipulation Program
- Homework Hack: Social Media Influencer Simulator
- Key Takeaways
Popcorn Hack #1: Transform Beginner Code
public class PopcornHack1 {
public static void main(String[] args) {
int playerScore = 1000;
int playerHealth = 100;
int enemiesDefeated = 0;
System.out.println("=== GAME START ===");
System.out.println("Initial Score: " + playerScore);
System.out.println("Initial Health: " + playerHealth);
System.out.println("Enemies Defeated: " + enemiesDefeated);
// Player defeats an enemy worth 250 points
playerScore += 250;
System.out.println("\nAfter defeating enemy:");
System.out.println("Score: " + playerScore);
// Player takes 15 damage
playerHealth -= 15;
System.out.println("\nAfter taking damage:");
System.out.println("Health: " + playerHealth);
// Enemy count goes up
enemiesDefeated++;
System.out.println("\nEnemies Defeated: " + enemiesDefeated);
// Boss battle: double the current score!
playerScore *= 2;
System.out.println("\nAfter boss battle (score doubled):");
System.out.println("Score: " + playerScore);
// Healing potion restores health to 80% of current
playerHealth *= 4;
playerHealth /= 5;
System.out.println("\nAfter healing potion (80% restoration):");
System.out.println("Health: " + playerHealth);
System.out.println("\n=== GAME STATE ===");
System.out.println("Final Score: " + playerScore);
System.out.println("Final Health: " + playerHealth);
System.out.println("Total Enemies Defeated: " + enemiesDefeated);
}
}
PopcornHack1.main(null);
=== GAME START ===
Initial Score: 1000
Initial Health: 100
Enemies Defeated: 0
After defeating enemy:
Score: 1250
After taking damage:
Health: 85
Enemies Defeated: 1
After boss battle (score doubled):
Score: 2500
After healing potion (80% restoration):
Health: 68
=== GAME STATE ===
Final Score: 2500
Final Health: 68
Total Enemies Defeated: 1
Popcorn Hack 2: Score Manipulation Program
public class PopcornHack2 {
public static void main(String[] args) {
int score = 100;
System.out.println("=== SCORE TRACKER ===");
System.out.println("Starting score: " + score);
// Deduct points for a wrong answer
score -= 15;
System.out.println("\nWrong answer penalty (-15 points)");
System.out.println("Current score: " + score);
// Double the score with a power-up
score *= 2;
System.out.println("\nPower-up activated! (Score doubled)");
System.out.println("Current score: " + score);
// Add bonus points
score += 30;
System.out.println("\nBonus points earned (+30 points)");
System.out.println("Current score: " + score);
// Find remainder after dividing by 7
score %= 7;
System.out.println("\nCalculating position in leaderboard (score % 7)");
System.out.println("Position indicator: " + score);
System.out.println("\n=== TRACKING COMPLETE ===");
}
}
PopcornHack2.main(null);
=== SCORE TRACKER ===
Starting score: 100
Wrong answer penalty (-15 points)
Current score: 85
Power-up activated! (Score doubled)
Current score: 170
Bonus points earned (+30 points)
Current score: 200
Calculating position in leaderboard (score % 7)
Position indicator: 4
=== TRACKING COMPLETE ===
Homework Hack: Social Media Influencer Simulator
public class SocialMediaSimulator {
public static void main(String[] args) {
// Initialize variables
int followers = 500;
int posts = 10;
int engagement = 350;
double sponsorshipEarnings = 0.0;
int position = 1450;
System.out.println("=== SOCIAL MEDIA SIMULATOR ===\n");
System.out.println("Starting followers: " + followers);
System.out.println("Starting posts: " + posts);
System.out.println("Starting engagement: " + engagement + " likes\n");
// Operation 1: Posted a viral video
System.out.println("Posted a new video!");
followers += 250;
posts++;
System.out.println("Followers: " + followers + " (+250 from the viral video)");
System.out.println("Total posts: " + posts + "\n");
// Operation 2: Controversial opinion posted
System.out.println("Controversial opinion posted...");
followers -= 50;
System.out.println("Followers: " + followers + " (-50 from upset followers)\n");
// Operation 3: Trending hashtag boost
System.out.println("Trending hashtag boost!");
followers *= 2;
engagement *= 2;
System.out.println("Followers: " + followers + " (doubled from trending!)");
System.out.println("Engagement: " + engagement + " likes (doubled!)\n");
// Operation 4: Calculate average engagement per post
int avgEngagement = engagement / posts;
System.out.println("Average engagement per post: " + avgEngagement + " likes\n");
// Operation 5: First sponsorship deal
sponsorshipEarnings += 500.00;
System.out.println("First sponsorship deal secured!");
System.out.println("Earnings: $" + sponsorshipEarnings + "\n");
// Operation 6: Brand deal - earnings increase
sponsorshipEarnings *= 3;
System.out.println("Major brand deal! (Earnings tripled)");
System.out.println("Total earnings: $" + sponsorshipEarnings + "\n");
// Operation 7: Calculate ranking position
position %= 100;
System.out.println("Final ranking position: " + position + "\n");
// Operation 8: Increment posts for consistency
posts++;
System.out.println("Posted another video for consistency!");
System.out.println("Total posts: " + posts + "\n");
System.out.println("=== SIMULATION COMPLETE ===");
System.out.println("\nFinal Statistics:");
System.out.println("Total Followers: " + followers);
System.out.println("Total Posts: " + posts);
System.out.println("Total Engagement: " + engagement + " likes");
System.out.println("Sponsorship Earnings: $" + sponsorshipEarnings);
System.out.println("Leaderboard Position: " + position);
}
}
SocialMediaSimulator.main(null);
=== SOCIAL MEDIA SIMULATOR ===
Starting followers: 500
Starting posts: 10
Starting engagement: 350 likes
Posted a new video!
Followers: 750 (+250 from the viral video)
Total posts: 11
Controversial opinion posted...
Followers: 700 (-50 from upset followers)
Trending hashtag boost!
Followers: 1400 (doubled from trending!)
Engagement: 700 likes (doubled!)
Average engagement per post: 63 likes
First sponsorship deal secured!
Earnings: $500.0
Major brand deal! (Earnings tripled)
Total earnings: $1500.0
Final ranking position: 50
Posted another video for consistency!
Total posts: 12
=== SIMULATION COMPLETE ===
Final Statistics:
Total Followers: 1400
Total Posts: 12
Total Engagement: 700 likes
Sponsorship Earnings: $1500.0
Leaderboard Position: 50
Key Takeaways
Compound assignment operators make code cleaner and more efficient
- +=, -=, *=, /=, %= combine operations with assignment
- ++ and – are shorthand for incrementing/decrementing by 1
Important Rules for AP Exam:
- Variable must exist before using compound operators
- Integer division truncates (no decimals)
- Only post-form (x++, x–) is tested on AP exam
Real-world applications:
- Game development (scores, health, counters)
- Financial calculations (balances, interest)
- Social media metrics (followers, engagement)
- Fitness tracking (calories, steps, progress)