Week 1L - Lecture
How do you learn?¶
Weekw 1: Getting Started with Programming¶
- Programs are written as High-Level Language Source code
- Procedural source code consists of:
- Statements:
System.out.println("Hello world");
- Expressions:
int a = 2;
- Executed from Top-to-Bottom, Left-to-Right
- Statements:
- HLL were created to make coding faster and more human-readable.
- Hundres of languages, but Java is very popular and makes learning other languages easier.
- Languages like Java have many APIs, Libraries, and GUI tools to make development easier.
- Procedural source code consists of:
- Modern programming is always done in an IDE
- Useful tools for programmer
- Real-time Syntax checking
- Auto-Documentation tools
- Code Completion
- Console
- Debugger
- AI Generative code assistance
- Useful tools for programmer
Exercise 1 - Get familiar with IDE¶
- Open your IDE, start a NEW Project
- Add or find the Main.java file created for the project, what do you think the following do?
- Note the line public class Main
- Note the main method inside that Main.java file.
- Note any // lines.
- Note the ; terminating each code line.
- Now locate the following.
- Build/Run Buttons
- Project/Settings
- SDK/System settings
- Console
- Keep a lookout for Syntax errors/warnings in IDE
Exercise 2 - What does the following program print¶
Exercise2.java | |
---|---|
1 2 3 4 5 6 |
|
Simple Printing¶
- Stdin, Stdout, Stderr
- Java’s
System.out.println("");
will output stdout to console!
Anatomy of a Java Program¶
Week1.java | |
---|---|
1 2 3 4 5 6 |
|
- Every java file must be named the same as the class with a .java extension.
- Execution always starts in main method.
- Code blocks are contained within { and } brackets.
- Single line comments are denoted with //
- This is one way to document your code and leave yourself notes.
Exercise 3 - My First Print¶
YourNameHere.java | |
---|---|
1 2 3 4 5 |
|
- Run the program via Build/Run
- Run the program command line.
- sout
Multiple Line Printing¶
- You could simply put multiple print statements.
MultipleLinePrint.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 |
|
A Glance Into Your Future¶
What is System.out.println() Really?¶
- System is a static class.
- out is a class/static object (e.g. only one instance for the whole system Stdout!).
- println is a method in the out object that can be called via “.” because out is an object.
- “Text to be printed…” is a parameter of data-type String
- A parameter is data passed to a method so the method can use it and is always in the ( ) parenthesis.
- ; Separates the statements and expressions in your source code.
Comments¶
- So far we’ve seen single line comments //
- There is also a multi-line comment:
Multi-Line Comment 1 2 3 4 5 6
/* Comment text goes here. */
Reading Input¶
What is input?¶
- Remember Stdin?
- It is text typed by a user that is always read as a String
- There are many ways to read input, but we’ll mostly use the Scanner class.
Excercise 4 - Guess What?¶
ScannerIntro.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 |
|
What it does, in human… Statement exec line-by-line¶
- Introduce the scanner tool used for reading user input
- Create a tool for reading user input and name it scanner
- Print “Write Something: “
- Read the string written by the user, and assign it
- to program memory “String message = (string that was given as input)”
- Print the message written by the user
What it does, in programmer…¶
- Imports the Scanner class from the Java SDK libraries
- Creates a Java Object of type Scanner so we can access its method for reading input.
- Prints to StdOut a message requesting input
- Uses “dot” notation to call the scanner object’s nextLine method to read from StdIn until a newline (i.e. \n) is encountered.
- Assigns the returned typed line captured in method nextLine and stores it in variable message, which is of data-type String
- Prints to StdOut the typed message out of the variable message.
Exercise 5 - User Input¶
Exercise5.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
What is a String Anyway?¶
- Why do we say String where most people would say Text?
- String is just shorthand for saying charachters strung together like beads on a necklace.
- And that’s how a computer views text
- We’ve used strings in printing to StdOut and reading from StdIn to store the string in a variable
- So what’s a variable (Don’t worry, we’ll revisit this)!
- A storage location for some type of data (So far we’ve only seen String)
- They are declared like so
String message = "Hello";
- String is the type
- message is the variable name
- = is an assignment operator just like MATH!
- It assigns the String “Hello” to the storage/memory location pointed to by message**.
- Variables are used to reference the data at their storage location throughout a program.
- Varaibles are used for Re-Use and repetition.
String Operations¶
- Can we join strings together?
- YES, through concatenation…
ConcatStr.java 1 2 3 4 5 6
public class ConcatStr { public static void main(String[] args) { System.out.println("Hello " + "world!"); } }
- YES, through concatenation…
- You can also use variables, and you can do it any number of times!
ConcatStr.java 1 2 3 4 5 6 7 8 9
public class ConcatStr { public static void main(String[] args) { String message1 = "Hello world!"; String message2 = "and the universe!"; System.out.println(message1 + " ... and the galaxy ... " + message2); } }
Exercise 6 - String Concatenation¶
Bond.java | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Reading Strings¶
ReadingStrings.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Exercise 7¶
TimesThree.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Exercise 8¶
LiteralConcatenation.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Waiting for Input¶
- What does the nextLine method really due?
- Puts process thread into a wait state that will get notified asynchronously, via an event, when the enter key on the keyboard is pressed.
- So what does this mean? Take the following program for example:
Program.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Write the first string:"); String first = scanner.nextLine(); System.out.println("Write the second string:"); String second = scanner.nextLine(); System.out.println("Write the third string:"); String third = scanner.nextLine(); System.out.println("You wrote:"); System.out.println(first); System.out.println(second); System.out.println(third); } }
- Thie program will pause and go idle at each call of nextLine, its context will be switched out (Think 0% CPU usage).
- Upon a Keyboard Enter key being pressed, an event will be generated that will context switch it back.
Excercise 9¶
- Write a program that asks the user for a name and a job to do. Your program then prints a short story as follows:
1 2 3 4 5 6 7
I will tell you a story, but first, I need some info! What is your name? Trevor What is their job? developer Here is the story: Once upon a time there was Trevor, who was a developer. On the way to work, Trevor reflected on AI. Perhaps Trevor will not be a developer forever.