Assignment Statements and Input
- Popcorn Hack 1.1: Tracing Variable Values
- Popcorn Hack 1.2: Type Safety
- Popcorn Hack 2.1: Input Validation
- Popcorn Hack 2.2: Token vs
- Popcorn Hack 3: Code Trace
- Hack 1: Three-Number Average Calculator
- Hack 2: Assignment vs Comparison Explanation
Popcorn Hack 1.1: Tracing Variable Values
Question: After executing this code, what is the value of y? Walk through each line.
Answer:
int x = 10;
initializesx
with the value 10.x = 20;
updates the value ofx
to 20.int y = x;
initializesy
with the current value ofx
, which is 20.
Thus, the value of y
is 20.
Popcorn Hack 1.2: Type Safety
Question: What happens if you write int count = “hello”;? Why does this fail, and why is this behavior actually helpful?
Answer:
This fails because "hello"
is a string, and you are trying to assign it to a variable of type int
. In statically-typed languages like Java, variables have a specific type, and you cannot assign a value of a different type without explicit conversion.
This behavior is helpful because it prevents type-related errors. For example, if you accidentally try to perform arithmetic operations on non-numeric data, the compiler will catch the error at compile time, ensuring type safety and reducing runtime bugs.
Popcorn Hack 2.1: Input Validation
Question: What happens if a user types “twenty” when nextInt() expects a number? How would you make your program more robust?
Answer:
If a user types “twenty” when nextInt()
expects a number, the program will throw an InputMismatchException
because “twenty” is not a valid integer.
Popcorn Hack 2.2: Token vs
Question: If a user enters “San Diego” when next() is called, what gets stored? What about with nextLine()?
Answer:
When next()
is called, it reads and returns the next token (a single word) up to the first whitespace. In this case, it will store “San”.
When nextLine()
is called, it reads and returns the entire line of input, including spaces, up to the newline character. In this case, it will store “San Diego”.
Popcorn Hack 3: Code Trace
Question: Trace through the complete example. If the user enters “Alice Wonderland”, 20, and 3.85, what is the exact output?
Answer:
- The program reads the input “Alice Wonderland” using
nextLine()
. This stores the full name “Alice Wonderland”. - The program reads the input
20
usingnextInt()
. This stores the integer value 20. - The program reads the input
3.85
usingnextDouble()
. This stores the double value 3.85.
Hack 1: Three-Number Average Calculator
Task: Modify the example program to:
Prompt for three integers Calculate their average (watch for integer division!) Display the result with two decimal places Hint: Cast at least one number to double before division: (double) num1 / 3
import java.util.Scanner;
public class ThreeNumberAverage {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt for three integers
System.out.print("Enter the first integer: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second integer: ");
int num2 = scanner.nextInt();
System.out.print("Enter the third integer: ");
int num3 = scanner.nextInt();
// Calculate the average
double average = (double) (num1 + num2 + num3) / 3;
// Display the result with two decimal places
System.out.printf("The average is: %.2f%n", average);
scanner.close();
}
}
ThreeNumberAverage.main(null);
Enter the first integer: Enter the second integer: Enter the third integer: The average is: 88.33
Hack 2: Assignment vs Comparison Explanation
Assignment Operator (=
)
The assignment operator (=
) is used to assign a value to a variable. It takes the value on the right-hand side and stores it in the variable on the left-hand side.
Comparison Operator (==
)
The comparison operator (==
) is used to compare two values for equality. It checks whether the values on both sides are equal and returns a boolean result (true
or false
).