TUD Java Course Lesson 1
https://github.com/Trivo25/tud-java-course
October 26, 2021
While you’re waiting for it to start…
- We’re checking your 3G status
- Check your E-Mail if you haven’t done so yet
- setup WiFi
- install VS Code & OpenJDK
- check out this course’s page https://trivo25.github.io/tud-java-course/
First: Which Language?
Anyone in here who needs us to speak English? Else, we’ll hold it entirely in German.
Java
Object Oriented Programming
Florian Kluge, Moritz Schulz 21. Oktober 2021
Florian.Kluge@mailbox.tu-dresden.de
Moritz.Schulz2@mailbox.tu-dresden.de
Overview
- General Info about this course
- Check Set-Up
- Start coding!
- learn about variables & user input
About us
Florian Kluge
Florian.Kluge@mailbox.tu-dresden.de
Moritz Schulz
Moritz.Schulz2@mailbox.tu-dresden.de
What are we doing here?
- Introduction to programming
- Getting to know the basics of Java
- Preparation for upcoming courses (e.g. “Softwaretechnologie”, 2nd Semester)
Structure
- 14 lessons
- every Thursday, 13:00 - 14:30
- APB/006 (right here)
- 3G-Regel (vaccinated / recovered / tested)
- Attendance list
Attendance
- This course is held on a voluntary basis.
- You’re here voluntarily.
- If you want to quit, please let us know so we can invite students from the waiting list.
- If you don’t attend the course for 2 weeks in a row without notice
- we will give your slot to other students.
Our course philosophy
- This course is centered around you.
- Coding is best learned by doing it.
- Illustrative examples help.
- Mistakes are good because they help us learn.
- We’re not flawless experts either.
- Please ask questions
- because in the end, it’s about your understanding.
- We’ll walk through the class room to check that everyone gets along.
Why Java?
- widely used & modern programming language
- helpful ways of structuring code
- can be used for lots of things
- the same program can run on most computers
- good for getting started
Pop Quiz Time
Do you have any programming experience already?
We’re about to get started…
- we need Java OpenJDK 11
- check if it’s installed properly:
- open a terminal
- Windows: Windows+R => cmd => Enter
- MacBook: ⌘ + T
- Linux (depends): Ctrl+Shift+T
- enter:
javac -version
- it should say:
javac 11.0.12
Doesn’t work?
Use an online compiler for now.
https://www.jdoodle.com/online-java-compiler/
Let’s go!
- skip these steps if you’re using the online IDE
- in VS Code:
- on the right side, select a new folder
Programmierkurs_Java_1
- create a new file
HelloWorld.java
in that folder
- open the file
Your first piece of code
Now it’s time to write your first piece of code!
public class HelloWorld {
public static void main (String[] args ) {
System.out.println("Hello World!");
}
}
File: HelloWorld.java
Pay attention to copy the code exactly, keeping spaces, semicolons, parentheses, curly braces, capitalization, … exactly as above.
Run the program
- save the file: File > Save
- For VS Code users:
- open the terminal: View > Terminal
- type:
javac HelloWorld.java
- type:
java HelloWorld
- see:
Hello World!
- For the online users:
- click: [Run!]
- see:
Hello World!
Let’s play around a bit
- change the text
- try to run the program
Let’s explain… (1/4)
- Coding (= Programmieren) is telling the computer what to do.
- Coding = We list instructions for the computer.
- A program called compiler translates code so the computer can understand it.
- The computer runs the program.
Let’s explain… (2/4)
- We write code that humans can read.
HelloWorld.java
- let’s look at the code again (next slide)
- The compiler
javac
translates the code so the computer understands it.
HelloWorld.java
=> HelloWorld.class
- The computer runs the program.
Let’s explain… (3/4)
This is the framework of every Java program:
HelloWorld
is the class name and should be like the file name, but without .java
- start inside
public static void main (String[] args) {
… }
public class HelloWorld {
public static void main (String[] args) {
}
}
Let’s explain… (4/4)
This is the piece of code
that prints Hello World!
System.out.println("Hello World!");
Introducing: Variables
public class HelloWorld {
public static void main (String[] args) {
String phrase = "Hello World!";
System.out.println(phrase);
}
}
About Variables
String phrase = "Hello World!";
- they have a type: this one is a
String
(basically a piece of text)
- they have a name: this one is called
phrase
- they can be created (formally: declared):
=
- they have a value:
"Hello World!"
- note the
""
: they tell Java that this is text, not code
- think of them like a box that can only store things of a specific type
What do we need variables for?
- we can store data in them
- we can re-use them
- avoid typing their values twice
public class HelloWorld {
public static void main (String[] args) {
String phrase = "Hello World!";
System.out.println(phrase);
System.out.println(phrase);
}
}
Strings can be merged (concatenated)
public class HelloWorld {
public static void main (String[] args) {
String greeting = "Hello";
String name = "World"
System.out.println(greeting + " " + name + "!");
}
}
prints: Hello World!
(just as before)
Let’s talk to our program!
Remember to copy the first line as well!
import java.util.Scanner;
public class Talk {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Hi, what's your name?");
String name = scanner.nextLine();
System.out.println("Hello " + name + "!");
}
}
file: Talk.java
- online coding: turn “Interactive” on
- remember to press
Enter
after entering your name
// I am a comment. I can explain things.
- comments are ignored by Java
- we can use them to explain our code (to ourselves)
- next, I’ll use comments to explain the previous code
Let’s explain #2… (1/2)
// use somebody else's code, so we don't need to
// tell the computer how exactly to read input
import java.util.Scanner;
// same framework as before:
public class Talk {
public static void main (String[] args) {
// create a new variable of type Scanner
// that reads from the console (System.in)
Scanner scanner = new Scanner(System.in);
// Ask the user about their name:
System.out.println("Hi, what's your name?");
//...
Let’s explain #2… (2/2)
//...
// Read what the user wrote,
// and save it in the variable called "name"
String name = scanner.nextLine();
// Using the name, greet the user!
System.out.println("Hello " + name + "!");
}
}
Java also knows numbers
answer
is a variable of type int
- type
int
(integer) stores whole numbers
We can also read numbers
import java.util.Scanner;
public class TalkAgain {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Hi, how old are you?");
int age = scanner.nextInt();
int age2 = age + 5;
System.out.println("In 5 years, you'll be " + age2);
}
}
file: TalkAgain.java
Adding numbers works!
int num = 42 + 17;
int num2 = num + 7;
- it doesn’t matter if it’s the number itself or a variable containing a number
- some operators on numbers:
+
, -
, *
, /
- notice that an
int
divided by an int
will still be an int
- we’ll learn about floating point numbers soon
What have we learned?
- how to print text to console
- how to declare variables of type
int
, String
- how to read input from the console
- that operators like
+
, -
, *
and /
exist
Apply your new-learned knowledge
- Let’s build a calculator!
- Suggestion on how to do that:
- read one number
- save it in a variable
- read and save another number
- add them
- print the result
That’s it!
- Be encouraged to keep working on the calculator task :)
- Feel free to reach out to us
- to send us your results
- to tell us about problems you ran into
Next lesson
- a few more types of variables
- control flow: if-statements, while-loops
- more practical examples!
See course materials here: https://trivo25.github.io/tud-java-course/