|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "raw", |
| 5 | + "id": "354709c5", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "---\n", |
| 9 | + "layout: blogs\n", |
| 10 | + "title: Object Creation and Storage (Instantiation)\n", |
| 11 | + "permalink: /lesson/objcreation\n", |
| 12 | + "authors: Nora, Soni, Avika, \n", |
| 13 | + "---" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "id": "3f76f2b8", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "## Homework Hack #1 — Object Creation Practice\n", |
| 22 | + "\n", |
| 23 | + "**Goal** - Learn how to create and use objects in Java.\n", |
| 24 | + "Instructions:\n", |
| 25 | + "- Create a class called Car.\n", |
| 26 | + "- Give it attributes like: String brand, int year, In main(), create two Car objects using the new keyword.\n", |
| 27 | + "\n", |
| 28 | + "Print their details using a method or toString() to understand how object instances work." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "raw", |
| 33 | + "id": "4fa4bd60", |
| 34 | + "metadata": { |
| 35 | + "vscode": { |
| 36 | + "languageId": "raw" |
| 37 | + } |
| 38 | + }, |
| 39 | + "source": [ |
| 40 | + "// Homework Hack #1: Object Creation Practice\n", |
| 41 | + "\n", |
| 42 | + "public class ObjectCreation {\n", |
| 43 | + " public static void main(String[] args) {\n", |
| 44 | + " // 1. Create two Car objects using 'new'\n", |
| 45 | + " // Example: Car car1 = new Car(\"Tesla\", 2024);\n", |
| 46 | + "\n", |
| 47 | + " // 2. Print each car's info\n", |
| 48 | + " // Example: System.out.println(car1);\n", |
| 49 | + " }\n", |
| 50 | + "}\n", |
| 51 | + "\n", |
| 52 | + "class Car {\n", |
| 53 | + " // 1. Declare variables: brand, year\n", |
| 54 | + "\n", |
| 55 | + " // 2. Create a constructor to set those variables\n", |
| 56 | + "\n", |
| 57 | + " // 3. Add a method or toString() to display car info\n", |
| 58 | + "}\n" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "id": "5dfeb6ec", |
| 64 | + "metadata": {}, |
| 65 | + "source": [ |
| 66 | + "## Homework Hack #2 — Heap vs Stack Storage Demo\n", |
| 67 | + "\n", |
| 68 | + "**Goal** - Understand where data is stored in memory — stack vs heap.\n", |
| 69 | + "<br>\n", |
| 70 | + "Instructions:\n", |
| 71 | + "- Create a Book class with one variable: String title.\n", |
| 72 | + "- In main(), create:\n", |
| 73 | + "- A primitive variable (e.g. int pages = 300;)\n", |
| 74 | + "- A Book object (e.g. Book b1 = new Book(\"Java Basics\");)\n", |
| 75 | + "- Copy both into new variables (int pagesCopy = pages;, Book b2 = b1;)\n", |
| 76 | + "<br>\n", |
| 77 | + "Change the original values and print everything — watch what changes." |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "raw", |
| 82 | + "id": "87b5929f", |
| 83 | + "metadata": { |
| 84 | + "vscode": { |
| 85 | + "languageId": "raw" |
| 86 | + } |
| 87 | + }, |
| 88 | + "source": [ |
| 89 | + "// Homework Hack #2: Heap vs Stack Storage Demo\n", |
| 90 | + "\n", |
| 91 | + "public class HeapVsStack {\n", |
| 92 | + " public static void main(String[] args) {\n", |
| 93 | + " // 1. Create a primitive variable (int pages)\n", |
| 94 | + " \n", |
| 95 | + " // 2. Create another primitive variable that copies it\n", |
| 96 | + " \n", |
| 97 | + " // 3. Create a Book object (Book b1 = new Book(\"Java Basics\");)\n", |
| 98 | + " \n", |
| 99 | + " // 4. Create another Book reference (Book b2 = b1;)\n", |
| 100 | + " \n", |
| 101 | + " // 5. Change the original primitive and the Book title\n", |
| 102 | + " \n", |
| 103 | + " // 6. Print both sets of values to compare behavior\n", |
| 104 | + " }\n", |
| 105 | + "}\n", |
| 106 | + "\n", |
| 107 | + "class Book {\n", |
| 108 | + " // 1. Declare variable: String title\n", |
| 109 | + " \n", |
| 110 | + " // 2. Create a constructor to set the title\n", |
| 111 | + " \n", |
| 112 | + " // 3. Create a toString() to show the title\n", |
| 113 | + "}\n" |
| 114 | + ] |
| 115 | + } |
| 116 | + ], |
| 117 | + "metadata": { |
| 118 | + "language_info": { |
| 119 | + "name": "python" |
| 120 | + } |
| 121 | + }, |
| 122 | + "nbformat": 4, |
| 123 | + "nbformat_minor": 5 |
| 124 | +} |
0 commit comments