Skip to main content
Logo image

Section 1.4 Assignment and Input

Subsection 1.4.1 Assignment Statements

Assignment statements initialize or change the value stored in a variable using the assignment operator =. An assignment statement always has a single variable on the left hand side. The value of the expression (which can contain math operators and other variables) on the right of the = sign is assigned to and stored in the variable on the left side.
Figure 1.4.1. Assignment Statement (variable = expression;)
Instead of saying equals for the = in an assignment statement, say “gets” or “is assigned” to remember that the variable gets or is assigned the value on the right. In the figure above score is assigned the value of the expression 10 times points (which is another variable) plus 5.
The following video
 1 
https://www.youtube.com/watch?v=MZwIgM__5C8&ab_channel=colleenlewis
by Dr. Colleen Lewis shows how variables can change values in memory using assignment statements.
As we saw in the video, we can set one variable’s value to a copy of the value of another variable like y = x;. This won’t change the value of the variable that you are copying from.
Let’s step through the following code in the Java visualizer to see the values in memory (click here if the code below does not generate
 2 
http://www.pythontutor.com/visualize.html#code=public+class+Test2%0A%7B%0A+++public+static+void+main(String%5B%5D+args%29%0A+++%7B%0A+++++int+x+%3D+3%3B%0A+++++int+y+%3D+2%3B%0A+++++System.out.println(x%29%3B%0A+++++System.out.println(y%29%3B%0A+++++x+%3D+y%3B%0A+++++System.out.println(x%29%3B%0A+++++System.out.println(y%29%3B%0A+++++y+%3D+5%3B%0A+++++System.out.println(x%29%3B%0A+++++System.out.println(y%29%3B%0A+++%7D%0A%7D&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=java&rawInputLstJSON=%5B%5D&curInstr=0
). Click on the Next button at the bottom of the code to see how the values of the variables change. You can run the visualizer on any Active Code in this e-book by just clicking on the Code Lens button at the top of each Active Code.

Activity 1.4.1.

Keep clicking on the Next button at the bottom of the code to see how the values of the variables change as you step through the running program.

Activity 1.4.2.

What are the values of x, y, and z after the following code executes? You can step through this code by clicking on this Java visualizer
 3 
http://www.pythontutor.com/visualize.html#code=public+class+Test2%0A%7B%0A+++public+static+void+main(String%5B%5D+args%29%0A+++%7B%0A+++++int+x+%3D+0%3B%0A+++++int+y+%3D+1%3B%0A+++++int+z+%3D+2%3B%0A+++++x+%3D+y%3B%0A+++++y+%3D+y+*+2%3B%0A+++++z+%3D+3%3B%0A+++++System.out.println(x%29%3B%0A+++++System.out.println(y%29%3B%0A+++++System.out.println(z%29%3B%0A+++%7D%0A%7D&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=java&rawInputLstJSON=%5B%5D&curInstr=0
link.
int x = 0;
int y = 1;
int z = 2;
x = y;
y = y * 2;
z = 3;
  • x = 0, y = 1, z = 2
  • These are the initial values in the variable, but the values are changed.
  • x = 1, y = 2, z = 3
  • x changes to y’s initial value, y’s value is doubled, and z is set to 3
  • x = 2, y = 2, z = 3
  • Remember that the equal sign doesn’t mean that the two sides are equal. It sets the value for the variable on the left to the value from evaluating the right side.
  • x = 0, y = 0, z = 3
  • Remember that the equal sign doesn’t mean that the two sides are equal. It sets the value for the variable on the left to the value from evaluating the right side.

Activity 1.4.3.

The following has the correct code to ‘swap’ the values in x and y (so that x ends up with y’s initial value and y ends up with x’s initial value), but the code is mixed up and contains one extra block which is not needed in a correct solution. Drag the needed blocks from the left into the correct order on the right. Check your solution by clicking on the Check button. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks. After three incorrect attempts you will be able to use the Help Me button to make the problem easier.

Subsection 1.4.2 Data Types in Assignments

Every variable must be assigned a value before it can be used in an expression. That value must be from a compatible data type. A variable is initialized the first time it is assigned a value. During execution, an expression is evaluated to produce a single value. The value of an expression has a type based on the types of the values and operators evaluated in the expression. For example, an arithmetic expression that uses at least one double value will evaluate to a double value and must be saved in a double variable, as seen in the exercise below. In the next lesson, we will see how to change a variable or expression from one type to another.

Activity 1.4.4.

The code below looks okay at first glance, but if you run it, you will see that there is an error of incompatible types. Change the data type of one of the variables to fix the error.
Reference types like String can be assigned a new object or null if there is no object. The literal null is a special value used to indicate that a reference is not associated with any object.
// String variables can be assigned null
// or a new String object
String str = null;
str = "new object";

Subsection 1.4.3 Adding 1 to a Variable

If you use a variable to keep score, you would probably increment it (add one to the current value) whenever score should go up. You can do this by setting the variable to the current value of the variable plus one (score = score + 1) as shown below. The formula would look strange in math class, but it makes sense in coding because it is assigning a new value to the variable on the left that comes from evaluating the arithmetic expression on the right. So, the score variable is set to the previous value of score plus 1.

Activity 1.4.5.

Try the code below to see how score is incremented by 1. Try substituting 2 instead of 1 to see what happens.

Subsection 1.4.4 Input with Variables

Variables are a powerful abstraction in programming because the same algorithm can be used with different input values saved in variables. Input can come in a variety of forms, such as tactile for example by clicking on a button, audio with speech, visual using a webcam, or the most common form, text that the user types in. The Scanner class in Java is one way to obtain text input from the keyboard.
The code below using the Scanner class will say hello to anyone who types in their name and will have different results for different name values. First, type in your name below the code and then click on run. Try again with a friend’s name. The code works for any name: behold, the power of variables!

Activity 1.4.6.

The code below will say hello to anyone who types in their name. Type in your name below and then click on run. Try again with a friend’s name.
Although you will not be tested in the AP CSA exam on using the Java input from the keyboard, learning how to do input in Java is very useful and fun. For more information on using the Scanner class, go to https://www.w3schools.com/java/java_user_input.asp
 4 
https://www.w3schools.com/java/java_user_input.asp
, and for the newer Console class, https://howtodoinjava.com/java-examples/console-input-output/
 5 
https://howtodoinjava.com/java-examples/console-input-output/
. We are limited with the one way communication with the Java server in this Runestone ebook, but in most IDEs, the input/output would be more interactive. You can try this Scanner input example in JuiceMind
 6 
https://play.juicemind.com/dashboard/teams/Mk2wWMTqPkekcxTDWqRn/item/2e6655aa-a1b1-446e-9302-8ae43ab07e54#18fe2d0b-9011-40b1-b384-f956576a86a7
(click on Create Starter Code after login with a Google account) or Scanner input example in Replit
 7 
https://replit.com/@BerylHoffman/JavaIOExample
using the Scanner class and Console input example
 8 
https://replit.com/@BerylHoffman/JavaIOConsole
using the Console class. We will also learn how to use Scanner with input files in a later unit.

Subsection 1.4.5 Coding Challenge : Dog Years

In this coding challenge, you will calculate your age, and your pet’s age from your birthdates, and your pet’s age in dog years. In the code below, type in the current year, the year you were born, the year your dog or cat was born (if you don’t have one, make one up!) in the variables below. Then write formulas in assignment statements to calculate how old you are, how old your dog or cat is, and how old they are in dog years which is 7 times a human year. Finally, print it all out. If you are pair programming, switch drivers (who has control of the keyboard in pair programming) after every line of code.

Project 1.4.7.

Calculate your age and your pet’s age from the birthdates, and then your pet’s age in dog years.
Your teacher may suggest that you use a Java IDE with interactive input using the Scanner class
 9 
https://www.w3schools.com/java/java_user_input.asp
for this challenge, for example this JuiceMind activity
 10 
https://play.juicemind.com/dashboard/teams/Mk2wWMTqPkekcxTDWqRn/item/2e6655aa-a1b1-446e-9302-8ae43ab07e54#de42baff-8802-40d7-a3a5-74701999c5ba
or repl template
 11 
https://replit.com/@BerylHoffman/Challenge1-4-Dog-Years-Template
if you want to try the challenge with input.

Subsection 1.4.6 Summary

  • (AP 1.4.A.2) The assignment operator (=) allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.
  • (AP 1.4.A.1) Every variable must be assigned a value before it can be used in an expression. That value must be from a compatible data type.
  • (AP 1.4.A.1) A variable is initialized the first time it is assigned a value.
  • (AP 1.4.A.1) Reference types can be assigned a new object or null if there is no object. The literal null is a special value used to indicate that a reference is not associated with any object.
  • (AP 1.4.A.3) During execution, an expression is evaluated to produce a single value. The value of an expression has a type based on the types of the values and operators used in the expression.
  • (AP 1.4.B.1) Input can come in a variety of forms, such as tactile, audio, visual, or text. The Scanner class is one way to obtain text input from the keyboard, although input from the keyboard will not be on the AP exam.

Subsection 1.4.7 AP Practice

Activity 1.4.8.

Consider the following code segment.
int a = 5;
int b = a/2;
double c = a/2.0;
double d = 5 + a / b * c - 2;
System.out.println(d);
What is printed when the code segment is executed?
  • 8
  • Don’t forget that the result will be a double since at least 1 double value is involved.
  • 8.0
  • Yes, this is equivalent to (5 + ((a/b)*c) - 1) using int division.
  • 10.5
  • Don’t forget that division and multiplication will be done first due to operator precedence, and that an int/int gives an int truncated result where everything to the right of the decimal point is dropped.
  • An incompatible type error will occur.
  • No error will occur since the double result is saved in a double.

Subsection 1.4.8 AP Classroom Progress Check Unit 1 Part 1

This lesson ends the section for the College Board Unit 1 part 1. You can now do the College Board Progress Check for Unit 1 Part 1 in the AP Classroom. Or you can choose to wait until after lesson 1.6 where you will find links to the end of unit practice and review which will help you to prepare for the progress check.
You have attempted of activities on this page.