j1-1_strings
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| j1-1_strings [2024/11/28 00:47] – appledog | j1-1_strings [2024/12/05 06:40] (current) – appledog | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| Scroll down to the second option, the " | Scroll down to the second option, the " | ||
| - | == Entering The Program | + | == Variables |
| + | First explain the basic structure of a Java program and demonstrate basic integer variables. Then show how to print them out. | ||
| + | |||
| + | < | ||
| + | public class Main { | ||
| + | public static void main(String[] args) { | ||
| + | int a = 5; | ||
| + | int b = 3; | ||
| + | int c = a + b; | ||
| + | |||
| + | System.out.println(c); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Don't discuss anything else yet, just play around with integer math a bit, and show what a function is: | ||
| + | |||
| + | < | ||
| + | public class Main { | ||
| + | public static void main(String[] args) { | ||
| + | int a = 5; | ||
| + | int b = 3; | ||
| + | int c = a + b; | ||
| + | |||
| + | System.out.println(c); | ||
| + | |||
| + | int d = mul(a, b); | ||
| + | |||
| + | System.out.println(d); | ||
| + | } | ||
| + | |||
| + | public static int mul(int a, int b) { | ||
| + | int c = a * b; | ||
| + | |||
| + | return c; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Strings | ||
| Start a new project called "J1-1 Strings" | Start a new project called "J1-1 Strings" | ||
| === Main.java | === Main.java | ||
| < | < | ||
| + | import java.util.Scanner; | ||
| + | |||
| public class Main { | public class Main { | ||
| public static void main(String[] args) { | public static void main(String[] args) { | ||
| Line 54: | Line 95: | ||
| Teacher may discuss the answers here. | Teacher may discuss the answers here. | ||
| - | == The Code | + | == Don't Read This Section |
| Here's an explanation of what each line of the program does. We will look at it like a computer looks at it. First, here's the basic starter program: | Here's an explanation of what each line of the program does. We will look at it like a computer looks at it. First, here's the basic starter program: | ||
| Line 92: | Line 133: | ||
| This means the function doesn' | This means the function doesn' | ||
| - | == The Code | + | == The Code (Read This Part!) |
| === System.out.println(" | === System.out.println(" | ||
| This kind of code just prints a message out. | This kind of code just prints a message out. | ||
| Line 128: | Line 169: | ||
| < | < | ||
| - | String | + | String |
| - | char secondChar | + | char c = s.charAt(4); // Access the 5th character (index |
| - | System.out.println(" | + | System.out.println(" |
| </ | </ | ||
| + | |||
| + | === Strings are immutable | ||
| + | You cannot deal with a string like you can in Python, by directly indexing it. You need to first convert it into an array, and then back again. | ||
| + | |||
| + | < | ||
| + | String s = " | ||
| + | // Convert String to char[] | ||
| + | char[] a = s.toCharArray(); | ||
| + | | ||
| + | // Modify the array | ||
| + | a[5] = ' | ||
| + | |||
| + | // Convert char[] back to String | ||
| + | s = new String(a); | ||
| + | System.out.println(s); | ||
| + | </ | ||
| + | |||
| + | This prints " | ||
| + | |||
| + | Here concludes the lesson. Next: [[J1-2 if-else]] | ||
| + | |||
j1-1_strings.1732754835.txt.gz · Last modified: 2024/11/28 00:47 by appledog
