Back

Activity 1.5 Updating Variables

divider

Introduction

Activity 1.5

Updating Variables

Topics

  • Working with Dynamic Data
  • Keeping Track of Changes with Variables

Working with Dynamic Data

Variables don't have to stay the same throughout a program. We can change the value of a variable after it's been initialized.

int age = 15;

Working with Dynamic Data

Variables don't have to stay the same throughout a program. We can change the value of a variable after it's been initialized.

int age = 15;

System.out.println("🎉🎂🥳 Happy birthday!");

Working with Dynamic Data

Variables don't have to stay the same throughout a program. We can change the value of a variable after it's been initialized.

int age = 15;

System.out.println("🎉🎂🥳 Happy birthday!");

age = 16;

Keeping Track of Changes with Variables

A program often needs to track and update a value as it runs. This is done by modifying the variable's value using mathematical operations like addition, subtraction, multiplication, and division.

int health = 100;
System.out.println("Health: " + health);
System.out.println("Enemy attacks.");
health = health - 5;
System.out.println("Health: " + health);

Keeping Track of Changes with Variables

A program often needs to track and update a value as it runs. This is done by modifying the variable's value using mathematical operations like addition, subtraction, multiplication, and division.

int health = 100;
System.out.println("Health: " + health);
System.out.println("Enemy attacks.");
health = health - 5;
System.out.println("Health: " + health);
Health: 100
Enemy attacks.
Health: 95

Keeping Track of Changes with Variables

int health = 100;
System.out.println("Health: " + health);
System.out.println("Enemy attacks.");
health = health - 5;
System.out.println("Health: " + health);
Health: 100
Enemy attacks.
Health: 95
Variable Update

Key Terms

Variable Initialization
The initial assignment of a value to a newly declared variable. This process gives the variable its first stored data and establishes its type.
Variable Reassignment
The process of updating or changing the value of a variable after it has been initialized.
Dynamic Data
Information in a program that can be changed or modified while the program is running.

'F' → Fullscreen

Objectives

  • icon Updating variable values using the assignment operator.
  • icon Keeping track of data changes during a program's execution.
divider

Activity Tasks

  • icon Create a new project named 1-5-Updating Variables.
  • icon Complete each task individually.

Task 1: Battle Demo

Program.java
public class Program {
public static void main(String[] args) {
System.out.println("--- Demo 1 - Battle Demo ---");
int playerHealth = 100;
int playerAttack = 25;
int enemyHealth = 100;
int enemyAttack = 20;
int potions = 3;
System.out.println("Health: " + playerHealth);
System.out.println("Enemy health: " + enemyHealth);
System.out.println();
System.out.println("Player attacks.");
enemyHealth = enemyHealth - playerAttack;
System.out.println("You did " + playerAttack + " damage! Enemy health: " + enemyHealth);
System.out.println();
System.out.println("Enemy attacks.");
playerHealth = playerHealth - enemyAttack;
System.out.println("Enemy did " + enemyAttack + " damage! Player health: " + playerHealth);
System.out.println();
System.out.println("Using potion...");
playerHealth = playerHealth + 10;
potions = potions - 1;
System.out.println("Player health: " + playerHealth + ". Potions left: " + potions);
System.out.println("\n\n"); // What on earth is \n? Consider Googling '\n Java'
//
}
}

Task 2: Banking Simulator

Program.java
public class Program {
public static void main(String[] args) {
System.out.println("--- Demo 1 - Battle Demo ---");
int playerHealth = 100;
int playerAttack = 25;
int enemyHealth = 100;
int enemyAttack = 20;
int potions = 3;
System.out.println("Health: " + playerHealth);
System.out.println("Enemy health: " + enemyHealth);
System.out.println();
System.out.println("Player attacks.");
enemyHealth = enemyHealth - playerAttack;
System.out.println("You did " + playerAttack + " damage! Enemy health: " + enemyHealth);
System.out.println();
System.out.println("Enemy attacks.");
playerHealth = playerHealth - enemyAttack;
System.out.println("Enemy did " + enemyAttack + " damage! Player health: " + playerHealth);
System.out.println();
System.out.println("Using potion...");
playerHealth = playerHealth + 10;
potions = potions - 1;
System.out.println("Player health: " + playerHealth + ". Potions left: " + potions);
System.out.println("\n\n");
// -----------------------------------------------------------------------------
System.out.println("--- Demo 2 - Banking Simulator ---");
String accountName = "Checking Account";
double balance = 100.50;
System.out.println(accountName + " balance: " + balance);
System.out.println("Depositing $99.01 to " + accountName);
balance = balance + 99.01;
System.out.println(accountName + " balance: " + balance);
System.out.println("\n\n");
//
}
}

Task 3: High Score Tracker

Program.java
public class Program {
public static void main(String[] args) {
System.out.println("--- Demo 1 - Battle Demo ---");
int playerHealth = 100;
int playerAttack = 25;
int enemyHealth = 100;
int enemyAttack = 20;
int potions = 3;
System.out.println("Health: " + playerHealth);
System.out.println("Enemy health: " + enemyHealth);
System.out.println();
System.out.println("Player attacks.");
enemyHealth = enemyHealth - playerAttack;
System.out.println("You did " + playerAttack + " damage! Enemy health: " + enemyHealth);
System.out.println();
System.out.println("Enemy attacks.");
playerHealth = playerHealth - enemyAttack;
System.out.println("Enemy did " + enemyAttack + " damage! Player health: " + playerHealth);
System.out.println();
System.out.println("Using potion...");
playerHealth = playerHealth + 10;
potions = potions - 1;
System.out.println("Player health: " + playerHealth + ". Potions left: " + potions);
System.out.println("\n\n");
// -----------------------------------------------------------------------------
System.out.println("--- Demo 2 - Banking Simulator ---");
String accountName = "Checking Account";
double balance = 100.50;
System.out.println(accountName + " balance: " + balance);
System.out.println("Depositing $99.01 to " + accountName);
balance = balance + 99.01;
System.out.println(accountName + " balance: " + balance);
System.out.println("\n\n");
// -----------------------------------------------------------------------------
System.out.println("--- Demo 3 - High Score Tracker ---");
int playerScore = 0;
System.out.println("Current score: " + playerScore);
System.out.println("Scored 100 points!");
playerScore = playerScore + 100;
System.out.println("New score: " + playerScore);
System.out.println("Scored 500 more points for a headshot!");
playerScore = playerScore + 500;
System.out.println("New score: " + playerScore);
System.out.println("\n\n");
//
}
}

Task 4: Social Media Analytics

Program.java
public class Program {
public static void main(String[] args) {
System.out.println("--- Demo 1 - Battle Demo ---");
int playerHealth = 100;
int playerAttack = 25;
int enemyHealth = 100;
int enemyAttack = 20;
int potions = 3;
System.out.println("Health: " + playerHealth);
System.out.println("Enemy health: " + enemyHealth);
System.out.println();
System.out.println("Player attacks.");
enemyHealth = enemyHealth - playerAttack;
System.out.println("You did " + playerAttack + " damage! Enemy health: " + enemyHealth);
System.out.println();
System.out.println("Enemy attacks.");
playerHealth = playerHealth - enemyAttack;
System.out.println("Enemy did " + enemyAttack + " damage! Player health: " + playerHealth);
System.out.println();
System.out.println("Using potion...");
playerHealth = playerHealth + 10;
potions = potions - 1;
System.out.println("Player health: " + playerHealth + ". Potions left: " + potions);
System.out.println("\n\n");
// -----------------------------------------------------------------------------
System.out.println("--- Demo 2 - Banking Simulator ---");
String accountName = "Checking Account";
double balance = 100.50;
System.out.println(accountName + " balance: " + balance);
System.out.println("Depositing $99.01 to " + accountName);
balance = balance + 99.01;
System.out.println(accountName + " balance: " + balance);
System.out.println("\n\n");
// -----------------------------------------------------------------------------
System.out.println("--- Demo 3 - High Score Tracker ---");
int playerScore = 0;
System.out.println("Current score: " + playerScore);
System.out.println("Scored 100 points!");
playerScore = playerScore + 100;
System.out.println("New score: " + playerScore);
System.out.println("Scored 500 more points for a headshot!");
playerScore = playerScore + 500;
System.out.println("New score: " + playerScore);
System.out.println("\n\n");
// -----------------------------------------------------------------------------
System.out.println("--- Demo 4 - Social Media Analytics ---");
int likes = 23;
System.out.println("Post likes: " + likes);
System.out.println("A friend liked the post!");
likes = likes + 1;
System.out.println("Post likes: " + likes);
System.out.println("Another 10 people liked the post!");
likes = likes + 10;
System.out.println("Post likes: " + likes);
//
}
}
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
--- Demo 1 - Battle Demo ---
Health: 100
Enemy health: 100
Player attacks.
You did 25 damage! Enemy health: 75
Enemy attacks.
Enemy did 20 damage! Player health: 80
Using potion...
Player health: 90. Potions left: 2
--- Demo 2 - Banking Simulator ---
Checking Account balance: 100.5
Depositing $99.01 to Checking Account
Checking Account balance: 199.51
--- Demo 3 - High Score Tracker ---
Current score: 0
Scored 100 points!
New score: 100
Scored 500 more points for a headshot!
New score: 600
--- Demo 4 - Social Media Analytics ---
Post likes: 23
A friend liked the post!
Post likes: 24
Another 10 people liked the post!
Post likes: 34
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. What is the key difference between initializing a variable and updating its value? Why is it necessary to declare the variable type (like int or double) only when you initialize it?
  2. Consider the "Battle Demo" and "Banking Simulator" tasks. How did updating the variable's value allow the program to simulate a sequence of events, and what would happen if you didn't update the variables?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete