// Homework Hack #1: Object Creation Practice

public class ObjectCreation {
    public static void main(String[] args) {
        // 1. Create two Car objects using 'new'
        // Example: Car car1 = new Car("Tesla", 2024);
        Car car1 = new Car("Tesla", 2024);
        Car car2 = new Car("Toyota", 2020);

        // 2. Print each car's info
        // Example: System.out.println(car1);
        System.out.println(car1);
        System.out.println(car2);
    }
}

class Car {
    // 1. Declare variables: brand, year
    String brand;
    int year;

    // 2. Create a constructor to set those variables
    Car(String b, int y) {
        brand = b;
        year = y;
    }

    // 3. Add a method or toString() to display car info
    public String toString() {
        return brand + " (" + year + ")";
    }
}

ObjectCreation.main(null);

Tesla (2024)
Toyota (2020)

Homework Hack #2 — Heap vs Stack Storage Demo

Goal - Understand where data is stored in memory — stack vs heap.
Instructions:

  • Create a Book class with one variable: String title.
  • In main(), create:
  • A primitive variable (e.g. int pages = 300;)
  • A Book object (e.g. Book b1 = new Book(“Java Basics”);)
  • Copy both into new variables (int pagesCopy = pages;, Book b2 = b1;)
    Change the original values and print everything — watch what changes.
// Homework Hack #2: Heap vs Stack Storage Demo

public class HeapVsStack {
    public static void main(String[] args) {
        // 1. Create a primitive variable (int pages)
        int pages = 300;

        // 2. Create another primitive variable that copies it
        int pagesCopy = pages;

        // 3. Create a Book object (Book b1 = new Book("Java Basics");)
        Book b1 = new Book("Java Basics");

        // 4. Create another Book reference (Book b2 = b1;)
        Book b2 = b1;

        // 5. Change the original primitive and the Book title
        pages = 500;
        b1.title = "Advanced Java";

        // 6. Print both sets of values to compare behavior
        System.out.println("pages: " + pages);
        System.out.println("pagesCopy: " + pagesCopy);
        System.out.println("b1: " + b1);
        System.out.println("b2: " + b2);
    }
}

class Book {
    // 1. Declare variable: String title
    String title;

    // 2. Create a constructor to set the title
    Book(String t) {
        title = t;
    }

    // 3. Create a toString() to show the title
    public String toString() {
        return title;
    }
}

HeapVsStack.main(null);

pages: 500
pagesCopy: 300
b1: Advanced Java
b2: Advanced Java