Unit 1.4: Assignment and Input¶
Scope: CS Awesome 2, Section 1.4
Learning Goals¶
By the end of this lesson, you should be able to:
- Trace assignment statements.
- Explain why
=means assignment rather than mathematical equality. - Check type compatibility in assignments.
- Use a variable's previous value to compute its next value.
- Read keyboard input with
Scanner.
Assignment Statements¶
An assignment statement evaluates the expression on the right and stores the resulting value in the single variable on the left.
score = points * 10 + 5;
Read = as “is assigned” or “gets.” Assignment is directional.
int x = 3;
int y = x; // copies the current value 3 into y
x = 8; // changes x, but y remains 3
For primitive values, assigning one variable to another copies the value. It does not permanently connect the two variables.
Tracing Changes¶
int a = 2;
int b = 5;
a = b;
b = b + 1;
| Statement completed | a |
b |
|---|---|---|
| initialization | 2 | 5 |
a = b; |
5 | 5 |
b = b + 1; |
5 | 6 |
The right side uses the old value of b; the result is then stored back into b.
Type Compatibility¶
Every local variable must be assigned a value before use, and the assigned value must be compatible with the variable's type.
double total = 4.5; // valid
double count = 4; // int can widen to double
int result = 4.5; // compile-time type error
Reference variables may hold an object reference or null.
String message = null;
message = "System ready";
null means that the reference is not associated with an object. Calling a method through null causes a run-time error.
Updating a Variable¶
int visitors = 10;
visitors = visitors + 1;
This is meaningful in programming: evaluate visitors + 1 using the old value, then store the new value.
To swap two values, preserve one value in a temporary variable:
int left = 4;
int right = 9;
int temp = left;
left = right;
right = temp;
Input with Scanner¶
Input can be text, audio, visual, or tactile. Scanner is one way to read text typed at the keyboard.
import java.util.Scanner;
public class CheckIn
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Name: ");
String name = input.nextLine();
System.out.print("Group size: ");
int size = input.nextInt();
System.out.println("Welcome, " + name);
System.out.println("Group size: " + size);
}
}
Common methods include:
| Method | Reads |
|---|---|
nextInt() |
an int token |
nextDouble() |
a double token |
next() |
one whitespace-delimited string token |
nextLine() |
the rest of the current line |
Keyboard input is useful for programs, although it is not a focus of the AP exam.
Practice Missions¶
Mission 1: Trace the State¶
Create a trace table for x, y, and z after each statement.
int x = 4;
int y = 7;
int z = x;
x = y;
y = z + 2;
z = x - y;
Mission 2: Rotate Three Values¶
Given three initialized int variables a, b, and c, move the original value of a into b, b into c, and c into a. Use temporary storage and do not use numeric literals in the rotation statements.
Mission 3: Delivery Estimate¶
Write a program that reads a destination, whole number of packages, and estimated hours as a decimal. Print a labeled summary. Choose a compatible Scanner method and variable type for each input.
Mission 4: Diagnose the Type Error¶
Explain why this assignment fails and provide two valid fixes with different result types:
int items = 7;
double price = 2.5;
int total = items * price;
Key Summary¶
| Concept | Core idea |
|---|---|
| Assignment | Evaluates the right side and stores it on the left. |
| Initialization | First assignment of a variable. |
| Type compatibility | The stored value must fit the declared type. |
| Copying a primitive | Copies its current value. |
null |
A reference associated with no object. |
Scanner |
Reads text input and converts selected tokens. |
Source scope: CS Awesome 2, Unit 1.4