1.6 Lesson Homework
Lesson 1.6 Homework
Popcorn Hack 1
public class PopcornHack1 {
public static void main(String[] args) {
System.out.println("=== POPCORN HACK #1 ===");
int playerScore = 1000;
int playerHealth = 100;
int enemiesDefeated = 0;
System.out.println("Starting stats:");
System.out.println("Score: " + playerScore + ", Health: " + playerHealth + ", Enemies defeated: " + enemiesDefeated);
// Player defeats an enemy worth 250 points
playerScore += 250;
System.out.println("Player defeats an enemy! Score: " + playerScore);
// Player takes 15 damage
playerHealth -= 15;
System.out.println("Player takes damage. Health: " + playerHealth);
// Enemy count goes up
enemiesDefeated += 1;
System.out.println("Enemies defeated: " + enemiesDefeated);
// Boss battle: double the current score!
playerScore *= 2;
System.out.println("Boss battle! Score doubled: " + playerScore);
// Healing potion restores health to 80% of current
playerHealth *= 4;
playerHealth /= 5; // equivalent to multiplying by 0.8
System.out.println("Healing potion used. Health: " + playerHealth);
System.out.println("=== POPCORN HACK #1 COMPLETE ===");
}
}
// Run it
PopcornHack1.main(null);
=== POPCORN HACK #1 ===
Starting stats:
Score: 1000, Health: 100, Enemies defeated: 0
Player defeats an enemy! Score: 1250
Player takes damage. Health: 85
Enemies defeated: 1
Boss battle! Score doubled: 2500
Healing potion used. Health: 68
=== POPCORN HACK #1 COMPLETE ===
Popcorn Hack 2
public class PopcornHack2 {
public static void main(String[] args) {
System.out.println("=== POPCORN HACK #2 ===");
int score = 100;
System.out.println("Starting score: " + score);
// Deduct points for a wrong answer
score -= 20; // -= operator
System.out.println("After wrong answer (-20): " + score);
// Double the score with a power-up
score *= 2; // *= operator
System.out.println("After power-up (*2): " + score);
// Find remainder after dividing by 7
score %= 7; // %= operator
System.out.println("After modulo 7 (%=7): " + score);
// Increment score by 1 (post-increment)
score++;
System.out.println("After post-increment (++): " + score);
// Decrement score by 1 (post-decrement)
score--;
System.out.println("After post-decrement (--): " + score);
System.out.println("=== POPCORN HACK #2 COMPLETE ===");
}
}
// Run it
PopcornHack2.main(null);
=== POPCORN HACK #2 ===
Starting score: 100
After wrong answer (-20): 80
After power-up (*2): 160
After modulo 7 (%=7): 6
After post-increment (++): 7
After post-decrement (--): 6
=== POPCORN HACK #2 COMPLETE ===
Homework
Option A
public class SocialMediaSimulator {
public static void main(String[] args) {
System.out.println("=== SOCIAL MEDIA SIMULATOR ===");
// Starting stats
int followers = 500;
int posts = 5;
int engagement = 120;
int sponsorshipEarnings = 0;
System.out.println("Starting followers: " + followers);
// Viral post adds followers
followers += 250; // += operator
System.out.println("Posted a new video! Followers: " + followers + " (+250 from viral post)");
// Controversial post loses followers
followers -= 50; // -= operator
System.out.println("Controversial opinion posted... Followers: " + followers + " (-50 from upset followers)");
// Trending hashtag doubles engagement
engagement *= 2; // *= operator
System.out.println("Trending hashtag boost! Engagement: " + engagement + " (doubled)");
// Average engagement per post
int avgEngagement = engagement / posts; // integer division
System.out.println("Average engagement per post: " + avgEngagement);
// Final ranking modulo 1000
int ranking = 1450;
ranking %= 1000; // %= operator
System.out.println("Final ranking position: " + ranking);
System.out.println("=== SIMULATION COMPLETE ===");
}
}
// Running the simulation
SocialMediaSimulator.main(null);
=== SOCIAL MEDIA SIMULATOR ===
Starting followers: 500
Posted a new video! Followers: 750 (+250 from viral post)
Controversial opinion posted... Followers: 700 (-50 from upset followers)
Trending hashtag boost! Engagement: 240 (doubled)
Average engagement per post: 48
Final ranking position: 450
=== SIMULATION COMPLETE ===