Home

AAP-1.B Determine the value of a variable as a result of an assignment

What is Variable Assignment?

Variable assignment in Java means giving a value to a variable. A variable is like a container that holds data that can be used and manipulated throughout your program. When you assign a value to a variable, you are telling the program to store a specific piece of data in that container.

How Does Variable Assignment Work?

In Java, you declare a variable by specifying its type and name, then assign a value to it using the = operator. The variable name is on the left side of the =, and the value you want to assign is on the right side.

Here's a simple example:

int age = 16; // declaring an integer variable named age and assigning it the value 16

In this example:

Examples of Variable Assignment

Let's look at some more examples:

double price = 19.99; // declaring a double variable named price and assigning it the value 19.99
char grade = 'A';     // declaring a char variable named grade and assigning it the value 'A'
boolean isJavaFun = true; // declaring a boolean variable named isJavaFun and assigning it the value true

Real-World Example: Tracking Inventory

Imagine you are running a small store, and you need to keep track of your inventory. You can use variables to store the quantity of each item.

int apples = 50; // you have 50 apples in stock
int oranges = 30; // you have 30 oranges in stock

If you sell 10 apples, you can update the value of the apples variable:

apples = apples - 10; // now you have 40 apples in stock

Full Program Example

Let's create a simple program that demonstrates variable assignment and updates the values of variables.

public class InventoryTracker {
    public static void main(String[] args) {
        // Declaring and assigning initial values to variables
        int apples = 50;
        int oranges = 30;

        // Printing initial stock
        System.out.println("Initial stock:");
        System.out.println("Apples: " + apples);
        System.out.println("Oranges: " + oranges);

        // Selling some apples and oranges
        apples = apples - 10; // selling 10 apples
        oranges = oranges - 5; // selling 5 oranges

        // Printing updated stock
        System.out.println("Updated stock:");
        System.out.println("Apples: " + apples);
        System.out.println("Oranges: " + oranges);

        // Receiving new stock
        apples = apples + 20; // receiving 20 more apples
        oranges = oranges + 15; // receiving 15 more oranges

        // Printing final stock
        System.out.println("Final stock:");
        System.out.println("Apples: " + apples);
        System.out.println("Oranges: " + oranges);
    }
}

Explanation of the Program

  1. We declare two integer variables, apples and oranges, and assign them initial values.

  2. We print the initial stock of apples and oranges.

  3. We update the values of the variables to reflect sales (subtracting sold items).

  4. We print the updated stock.

  5. We update the values again to reflect receiving new stock (adding received items).

  6. We print the final stock.

Programming Problem

Now that you understand how variable assignment works, try solving this problem:

Problem: Create a Java program that tracks the number of books in a library. Start with an initial stock of 100 fiction books and 200 non-fiction books. Update the stock after lending 15 fiction books and 25 non-fiction books. Then, update the stock again after receiving 10 new fiction books and 20 new non-fiction books. Print the stock at each stage.

Good luck, and happy coding!