j1-1_strings
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
j1-1_strings [2024/11/28 00:05] – created 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 " | ||
- | == The Code | + | == Variables |
+ | First explain the basic structure of a Java program and demonstrate basic integer variables. Then show how to print them out. | ||
+ | |||
+ | <Code:Java> | ||
+ | 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 40: | Line 81: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Now try to run the program. On Mac it's Control-R. On windows, I think, it's Shift-F10. Get used to using a keyboard; it's faster. | ||
+ | |||
+ | == Fixing any Bugs | ||
+ | Oh no! Your first error. Find the line with the red underlined word " | ||
+ | |||
+ | < | ||
+ | |||
+ | Now, run the program again. What happens? | ||
+ | |||
+ | A: (Your answer). | ||
+ | |||
+ | Teacher may discuss the answers here. | ||
+ | |||
+ | == 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: | ||
+ | |||
+ | < | ||
+ | public class Main { | ||
+ | public static void main(String[] args) { | ||
+ | // Code starts here: | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === These aren't the droids you're looking for | ||
+ | First, a warning. It might be better to say " | ||
+ | |||
+ | ==== import | ||
+ | The first thing we do is import the java.util.Scanner class. This Scanner class is inside the util library, which is inside the java library. That's it's location. What is it? It is a group of utility functions for doing things like, for example, reading strings. | ||
+ | |||
+ | ==== public | ||
+ | * public class Main | ||
+ | |||
+ | This is a declaration of a class. A class is a group of code and data; a specification, | ||
+ | |||
+ | So, why is " | ||
+ | |||
+ | ==== static | ||
+ | * public static void main(String[] args) | ||
+ | We see public again; this is just so the main() function can be found by the system. | ||
+ | |||
+ | It's static, which means it runs without first making a named copy of itself. If you don't know what that means, it means that you can make as many copies of a class and run it at the same time as you want. But here, if you are trying to run a program, it just "runs the program" | ||
+ | |||
+ | If you remove the static, it doesn' | ||
+ | |||
+ | ==== void | ||
+ | * void means nothing. | ||
+ | |||
+ | This means the function doesn' | ||
+ | |||
+ | == The Code (Read This Part!) | ||
+ | === System.out.println(" | ||
+ | This kind of code just prints a message out. | ||
+ | |||
+ | === Scanner | ||
+ | I'm going to deal with scanner as a block of code. Here's what it looks like if you remove the comments: | ||
+ | |||
+ | < | ||
+ | // Create a Scanner object to read input | ||
+ | Scanner scanner = new Scanner(System.in); | ||
+ | |||
+ | // Prompt the user for their name | ||
+ | System.out.print(" | ||
+ | |||
+ | // Read the input from the user | ||
+ | String name = scanner.nextLine(); | ||
+ | |||
+ | // Output a response | ||
+ | System.out.println(" | ||
+ | |||
+ | // Close the scanner | ||
+ | scanner.close(); | ||
+ | </ | ||
+ | |||
+ | First you need to understand System.out and System.in aren't the same thing. Meaning, maybe System.out isn't a character terminal but a file, or a printer, or nothing (anything sent there is destroyed). System.in also might not exist. For example on a phone, there is no keyboard. Also, it could be a file, or information sent to the program in some other way. However, in most instances on a computer the default will be set to STDIN and STDOUT -- in terms of IDEA, this will be some kind of mini-terminal in the program out window. | ||
+ | |||
+ | So what the Scanner class does is find STDIN and find a way to take information from it. Since it could be anything, we have to open the Scanner on System.in. That's what < | ||
+ | |||
+ | Now input is simple; just call nextLine() on scanner. This will accept input until a CR or LF is detected -- i.e. the ENTER key, or the end of a line in a file. | ||
+ | |||
+ | We place this information into a String object, and then we close the scanner object. Simple. | ||
+ | |||
+ | == What's a String? | ||
+ | A string is a bunch of characters -- like ' | ||
+ | |||
+ | < | ||
+ | String s = " | ||
+ | char c = s.charAt(4); | ||
+ | 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.1732752353.txt.gz · Last modified: 2024/11/28 00:05 by appledog