|
| 1 | +# Week 1 – Setup, Java Basics, Primitive Data Types & Variables |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## Why Learn This? |
| 6 | + |
| 7 | +Understanding Java’s syntax and primitive data types is like learning the alphabet before writing words. This foundation enables you to: |
| 8 | + |
| 9 | +* Build reliable, efficient programs. |
| 10 | +* Understand how Java communicates with your machine. |
| 11 | +* Begin thinking like a software engineer. |
| 12 | + These basics also make it easier to learn advanced Java concepts later and transition to other languages. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Week 1 Concepts & Notes |
| 17 | + |
| 18 | +### Java Setup |
| 19 | + |
| 20 | +* Install the **Java Development Kit (JDK)**. |
| 21 | + |
| 22 | +* Use **Command Line (CLI)** to compile and run Java programs: |
| 23 | + |
| 24 | + ```bash |
| 25 | + javac HelloWorld.java |
| 26 | + java HelloWorld |
| 27 | + ``` |
| 28 | + |
| 29 | +* Use an **IDE like IntelliJ** for easier development. |
| 30 | + |
| 31 | +### Your First Java Program |
| 32 | + |
| 33 | +```java |
| 34 | +public class HelloWorld { |
| 35 | + public static void main(String[] args) { |
| 36 | + System.out.println("Hello, world!"); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +* `public static void main(String[] args)` is the program entry point. |
| 42 | +* `System.out.println()` prints to the console. |
| 43 | + |
| 44 | +### Comments |
| 45 | + |
| 46 | +```java |
| 47 | +// This is a single-line comment |
| 48 | + |
| 49 | +/* This is |
| 50 | + a multi-line comment */ |
| 51 | +``` |
| 52 | + |
| 53 | +### Primitive Data Types |
| 54 | + |
| 55 | +| Type | Description | Example | |
| 56 | +| --------- | ----------------- | ------------------------- | |
| 57 | +| `int` | Integer numbers | `int x = 10;` | |
| 58 | +| `double` | Decimal numbers | `double y = 3.14;` | |
| 59 | +| `boolean` | True/False values | `boolean isTrue = false;` | |
| 60 | +| `char` | Single characters | `char letter = 'A';` | |
| 61 | + |
| 62 | +### Variables |
| 63 | + |
| 64 | +```java |
| 65 | +int age = 30; |
| 66 | +double price = 19.99; |
| 67 | +char grade = 'A'; |
| 68 | +boolean isValid = true; |
| 69 | +``` |
| 70 | + |
| 71 | +### Type Casting |
| 72 | + |
| 73 | +```java |
| 74 | +int a = 10; |
| 75 | +double b = a; // implicit casting |
| 76 | +int c = (int) b; // explicit casting |
| 77 | +``` |
| 78 | + |
| 79 | +### Input & Output |
| 80 | + |
| 81 | +```java |
| 82 | +import java.util.Scanner; |
| 83 | + |
| 84 | +public class InputExample { |
| 85 | + public static void main(String[] args) { |
| 86 | + Scanner scanner = new Scanner(System.in); |
| 87 | + System.out.print("Enter your name: "); |
| 88 | + String name = scanner.nextLine(); |
| 89 | + System.out.println("Hello, " + name); |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Operators |
| 95 | + |
| 96 | +#### Arithmetic Operators |
| 97 | + |
| 98 | +```java |
| 99 | +int x = 10 + 5; // 15 |
| 100 | +int y = 10 - 3; // 7 |
| 101 | +int z = 2 * 3; // 6 |
| 102 | +int a = 10 / 2; // 5 |
| 103 | +int b = 10 % 3; // 1 (remainder) |
| 104 | +``` |
| 105 | + |
| 106 | +#### Relational Operators |
| 107 | + |
| 108 | +```java |
| 109 | +int a = 5; |
| 110 | +int b = 10; |
| 111 | +boolean result = a < b; // true |
| 112 | +``` |
| 113 | + |
| 114 | +#### Logical Operators |
| 115 | + |
| 116 | +```java |
| 117 | +true && false // false |
| 118 | +true || false // true |
| 119 | +!true // false |
| 120 | +``` |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +### Conditional Statements |
| 125 | + |
| 126 | +```java |
| 127 | +int score = 85; |
| 128 | + |
| 129 | +if (score >= 90) { |
| 130 | + System.out.println("A"); |
| 131 | +} else if (score >= 80) { |
| 132 | + System.out.println("B"); |
| 133 | +} else { |
| 134 | + System.out.println("C or below"); |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +#### switch Statement |
| 139 | + |
| 140 | +```java |
| 141 | +int day = 3; |
| 142 | + |
| 143 | +switch (day) { |
| 144 | + case 1: |
| 145 | + System.out.println("Monday"); |
| 146 | + break; |
| 147 | + case 2: |
| 148 | + System.out.println("Tuesday"); |
| 149 | + break; |
| 150 | + default: |
| 151 | + System.out.println("Other day"); |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## Quiz Questions |
| 158 | + |
| 159 | +1. **What is the correct entry point for a Java program?** |
| 160 | + |
| 161 | +2. **What command is used to print output in Java?** |
| 162 | + |
| 163 | +3. **What is the size of an `int` in Java?** |
| 164 | + |
| 165 | +4. **What are four common primitive data types in Java?** |
| 166 | + |
| 167 | +5. **What is type casting in Java?** |
| 168 | + |
| 169 | +6. **What are the arithmetic operators in Java?** |
| 170 | + |
| 171 | +7. **What is the result of casting `double d = 9.8; int i = (int) d;`?** |
| 172 | + |
| 173 | +8. **Which class do we use to get user input from the console?** |
| 174 | + |
| 175 | +9. **What is the difference between == and = in Java?** |
| 176 | + |
| 177 | +10. **What does `System.out.println(5 > 3 && 2 < 1);` print?** |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## Coding Challenges |
| 182 | + |
| 183 | +> Each of these includes instructions to guide the student through incremental thinking. |
| 184 | +
|
| 185 | +1. **Number Comparison** |
| 186 | + Ask the user to enter two numbers. Print which number is greater or if they are equal. |
| 187 | + |
| 188 | +2. **Basic Calculator** |
| 189 | + Write a program that adds two integers and prints the result. |
| 190 | + *Stretch:* Use `Scanner` to allow user input. |
| 191 | + |
| 192 | +3. **Data Types Practice** |
| 193 | + Declare variables of each primitive type and print them. |
| 194 | + |
| 195 | +4. **Type Casting Demo** |
| 196 | + Demonstrate implicit and explicit casting between `int` and `double`. |
| 197 | + |
| 198 | +5. **Grade Categorizer** |
| 199 | + Ask the user for a numeric score and print the letter grade using `if-else`. |
| 200 | + |
| 201 | +6. **Character Math** |
| 202 | + Assign a `char` variable, cast it to an `int`, and print both. |
| 203 | + *Stretch:* Try adding 1 to the char and print the result. |
| 204 | + |
| 205 | +--- |
| 206 | + |
| 207 | +## Week 1 Summary |
| 208 | + |
| 209 | +* Installed Java and set up your dev environment. |
| 210 | +* Wrote and ran your first Java program. |
| 211 | +* Practiced with primitive types, variables, and console I/O. |
| 212 | +* Learned type casting and Scanner input. |
| 213 | +* Learned how to use Java’s operators and conditional logic. |
| 214 | +* Built confidence with foundational Java tools. |
| 215 | + |
| 216 | +--- |
| 217 | + |
| 218 | +## For Next Week |
| 219 | + |
| 220 | +### Reading |
| 221 | +* https://codingnomads.com/course/java-programming-101 |
| 222 | + * Section 2) Tools of the Developer Trade |
| 223 | + * Section 3) Java Virtual Machine (JVM) |
| 224 | + * Section 4) Java's Primitive Data Types |
| 225 | + * Section 5) Java Variables |
| 226 | + * Section 6) Java Operators |
| 227 | + |
| 228 | +### Assignments (in IntelliJ) |
| 229 | + |
| 230 | +First: |
| 231 | +* Make sure you have your [dev workspace setup](https://codingnomads.com/java-101-development-workspace) |
| 232 | +* Download or clone (ideally) the [Java Lab Exercises](https://codingnomads.com/java-101-get-your-java-labs-project) |
| 233 | + |
| 234 | +Then, complete the following exercises: |
| 235 | +* src/labs_examples/fundamentals/labs -> exercises 3, 4, and 5 |
| 236 | +* src/labs_examples/datatypes_operators/labs -> all exercises |
0 commit comments