CSA Unit 1.4 — Assignment Statements and Input


Popcorn Hack 1.1: Tracing Variable Values

Question:
After executing this code, what is the value of y? Walk through each line.

int x = 10;
x = 20;
int y = x;
 System.out.println(y); // Outputs 20
20
  • Based on this result 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 helpful?

Answer:
This fails because “hello” is a String, not an int.
Java is a strongly typed language, which means that every variable must store data of the correct declared type.
This type safety prevents bugs and ensures that errors are caught at compile time instead of during program execution.

Error Message:
incompatible types: String cannot be converted to int

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”, the program throws an InputMismatchException because nextInt() can only read integers.
To make the program more robust, you can validate the input before reading or handle the error using a conditional check or a try-catch block.

java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter an integer: ");
if (sc.hasNextInt()) {
    int num = sc.nextInt();
    System.out.println("You entered: " + num);
} else {
    System.out.println("Invalid input. Please enter a number.");
}
sc.close();
Enter an integer: 

You entered: 5

Popcorn Hack 2.2: Token vs Line

Question:
If a user enters “San Diego” when next() is called, what gets stored?
What about with nextLine()?

Answer:

  • next() reads only up to the first space → stores “San”
  • nextLine() reads the entire line → stores “San Diego”

Use nextLine() when you need to capture multiple words, such as full names or sentences.

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:

=== User Information Program ===

  • Enter your full name: Alice Wonderland
  • Enter your age: 20
  • Enter your GPA: 3.85

— Summary —

  • Name: Alice Wonderland
  • Age: 20 (next year: 21)
  • GPA: 3.85

Homework Hack 1: Three-Number Average Calculator

Question:
Modify the example program to:

  1. Prompt for three integers
  2. Calculate their average (avoid integer division!)
  3. Display the result with two decimal places

Answer:
To calculate the average of three numbers, you must use floating-point division (3.0 instead of 3) or cast the sum to double.
This ensures the result includes decimals and doesn’t get truncated as an integer.

// Hack 1: Three-Number Average Calculator
import java.util.Scanner;

public class ThreeNumberAverage {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first integer: ");
        int num1 = sc.nextInt();

        System.out.print("Enter second integer: ");
        int num2 = sc.nextInt();

        System.out.print("Enter third integer: ");
        int num3 = sc.nextInt();

        // Use double casting to avoid integer division
        double average = (double)(num1 + num2 + num3) / 3;

        // Print with 2 decimal places
        System.out.printf("The average is: %.2f\n", average);

        sc.close();
    }
}