Skip to the content.

Lesson 1.4 - Rohan Bojja

// int x = 10;     // x is first assigned the value 10
// x = 20;         // x is then updated to 20, replacing the old value
// int y = x;      // y is assigned the current value of x, which is 20
// Final value of y: 20
// int count = "hello";  // ❌ This causes a compile-time error.
// Reason: "hello" is a String, not an int. Java is strongly typed, so you can’t assign text to a number variable.
// This behavior is helpful because it prevents type mistakes before the program runs, making code safer and more reliable.

// Popcorn Hack 2.1: Input Validation
// If a user types "twenty" when nextInt() expects a number, the program crashes with an InputMismatchException.
// To make the program more robust, you can use try-catch blocks or check input with hasNextInt() before reading it.
// Popcorn Hack 2.2: Token vs Line
// If a user enters "San Diego" when next() is called, only "San" is stored (it stops at the space).
// With nextLine(), the entire line "San Diego" is stored.
// Assuming the example code is something like:
// Scanner input = new Scanner(System.in);
// System.out.print("Enter name: ");
// String name = input.nextLine();
// System.out.print("Enter age: ");
// int age = input.nextInt();
// System.out.print("Enter GPA: ");
// double gpa = input.nextDouble();
// System.out.println("Name: " + name + ", Age: " + age + ", GPA: " + gpa);

// User inputs:
// Name: Alice Wonderland
// Age: 20
// GPA: 3.85

// Exact output:
//Enter name: Enter age: Enter GPA: Name: Alice Wonderland, Age: 20, GPA: 3.85

import java.util.Scanner;

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

        System.out.print("Enter first number: ");
        int num1 = input.nextInt();

        System.out.print("Enter second number: ");
        int num2 = input.nextInt();

        System.out.print("Enter third number: ");
        int num3 = input.nextInt();

        double average = (double)(num1 + num2 + num3) / 3;

        System.out.printf("Average: %.2f\n", average);

        input.close();
    }
}
int x = 5;

if (x == 5) {
    System.out.println("x is equal to 5");
}
x is equal to 5