Variables and Data Types


Popcorn Hack #1: Your Favorite Things

public class MyFavoriteThings {
    public static void main(String[] args) {
        String favoriteFood = "Sushi";
        int myAge = 16;
        double heightInFeet = 5.8;
        boolean likesPizza = true;
        char colorFirstLetter = 'G';   // Green
        final int BIRTH_YEAR = 2009;   // Never changes

        System.out.println("Favorite food: " + favoriteFood);
        System.out.println("Age: " + myAge);
        System.out.println("Height: " + heightInFeet + " feet");
        System.out.println("Likes pizza: " + likesPizza);
        System.out.println("Favorite color starts with: " + colorFirstLetter);
        System.out.println("Born in: " + BIRTH_YEAR);
    }
}

MyFavoriteThings.main(new String[]{});
Favorite food: Sushi
Age: 16
Height: 5.8 feet
Likes pizza: true
Favorite color starts with: G
Born in: 2009

Popcorn Hack #2: Pick the Best Type

public class PickTheType {
    public static void main(String[] args) {
        int siblings = 3;                 // Whole number → int
        String firstName = "Ahaan";       // Text → String
        boolean isHungry = false;         // Yes/No → boolean
        char favoriteLetter = 'V';        // One letter → char
        double heightInches = 69.5;       // Decimal → double
        final int DAYS_IN_YEAR = 365;     // Never changes → final int

        System.out.println("Siblings: " + siblings);
        System.out.println("First name: " + firstName);
        System.out.println("Hungry: " + isHungry);
        System.out.println("Favorite letter: " + favoriteLetter);
        System.out.println("Height: " + heightInches + " inches");
        System.out.println("Days in a year: " + DAYS_IN_YEAR);
    }
}

PickTheType.main(new String[]{});
Siblings: 3
First name: Ahaan
Hungry: false
Favorite letter: V
Height: 69.5 inches
Days in a year: 365

Homework Hack: Simple Grade Calculator

public class GradeCalculator {
    public static void main(String[] args) {
        // Final variables (never change)
        final String STUDENT_NAME = "Ahaan Vaidyanathan";
        final String CLASS_NAME = "AP Computer Science A";

        // Test scores (whole numbers)
        int score1 = 92;
        int score2 = 85;
        int score3 = 88;

        // Calculate average
        double average = (score1 + score2 + score3) / 3.0;

        // Determine letter grade
        char grade;
        if (average >= 90) {
            grade = 'A';
        } else if (average >= 80) {
            grade = 'B';
        } else if (average >= 70) {
            grade = 'C';
        } else if (average >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }

        // Print summary
        System.out.println("Student: " + STUDENT_NAME);
        System.out.println("Class: " + CLASS_NAME);
        System.out.println("Scores: " + score1 + ", " + score2 + 
 + score3);
        System.out.println("Average: " + average);
        System.out.println("Final Grade: " + grade);
    }
}

GradeCalculator.main(new String[]{});
Student: Ahaan Vaidyanathan
Class: AP Computer Science A
Scores: 92, 8588
Average: 88.33333333333333
Final Grade: B