TUD Java Course Outline

https://github.com/Trivo25/tud-java-course

October 28, 2021

While you’re waiting for it to start…

  • We’re checking your 3G status
  • Check your setup
    • make sure Java OpenJDK works
    • check out this course’s page https://trivo25.github.io/tud-java-course/

Let’s ask again:

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 28. Oktober 2021


Florian.Kluge@mailbox.tu-dresden.de

Moritz.Schulz2@mailbox.tu-dresden.de

What’s happening today

  1. Short replay on what we did last time
  2. Variables
  3. Control flow

Contact

Florian Kluge

Florian.Kluge@mailbox.tu-dresden.de

Moritz Schulz

Moritz.Schulz2@mailbox.tu-dresden.de

Quick info about this course

  • please let us know if you know you won’t come
    • missing twice in a row => disenroll
  • practical task-based approach
    • examples, we’ll walk around & check
  • please, always ask questions
    • feel free to contact us
  • we want to leave no one behind

Stay up to date

  • check your TUD e-mails daily
  • check the course website
    • link is on the course page (kurse.ifsr.de)

Revision

  • let’s do a Hello World again - but quickly
  • afterwards, some questions
  • OpenJDK or VS Code not installed?
    • https://www.jdoodle.com/online-java-compiler/

copy it precisely

public class HelloWorld {
    public static void main (String[] args) {
        System.out.println("Hello World!");
    }
}
public class HelloWorld {
    public static void main (String[] args) {
        System.out.println("Hello World!");
    }
}

Can someone explain?

public class HelloWorld {
    public static void main (String[] args) {
        String phrase = "Hello World!";
        System.out.println(phrase);
    }
}
  • What’s the difference here?
  • Tell us about phrase.
  • What are variables useful for?

Cheat sheet

  • let’s take a look:

http://tiny.cc/java-cs

  • or create your own :)
    • you can use the slides (website)

Did you encounter any problems since last time?

  • Are the use of (basic) variables clear?
  • Can you independently compile and run your Java programs?

Lesson 2

Variables and Control Flow

More kinds of variables

  • Java introduces a wide ranges of variable types
    • … but only a handful are essential
Need to know!
Name Example Definition
int int i = 3213; Whole numbers (-2,147,483,648 to 2,147,483,647)
float float f = 0.420f; Floating point numbers up to 7 decimal digits
boolean boolean b = false; Binary state - True or False
char char c = 'a'; Single character or ASCII code
Good to know - but not important!
Name Example Definition
byte byte b = 11; Whole numbers (-128 to 127)
long long l = 31L; Whole numbers (very big)
double double d = 43.23d; Like float - just twice as precise
short short s = 423; Whole numbers (-32,768 to 32,767)
Integer Operators
int a = 9 * 4; // = 36
int b = 9 + 4; // = 13
int c = 9 % 4; // = ??
int d = 9 / 4; // = ??
Float Operators
float a = 9f * 4f; // = 36.0f
float b = 9f + 4f; // = 13.0f
float c = 9f % 4f; // = 1.0f
float d = 9f / 4f; // = ??
Variable types have different sizes
int i = 30000 * 2; // = ??
short s = 30000 * 2; // = ??
Variable types have different sizes
incompatible types: possible lossy conversion from int to short
boolean Algebra

What is boolean algebra?

boolean b = true || false; // ?
Why booleans?

With booleans, we can make logical decisions and control how our code “flows”.

Without booleans, code would be boring and always do the exact same thing.

boolean Algebra
// a boolean can only be true or false
boolean a = false;
boolean b = true;
boolean Algebra
  • What do we need booleans for?
    • to control how our program flows
    • to make decisions
  • conditions are booleans
Conditions
  • What do we use conditions and if-statements for?
    • to execute different code depending on the value of the condition
if(condition) {
    // do something cool!
}
Conditions
  • conditions need to evaluate to true so the code inside {} is executed
if(true) {
    // ...the code...
}
Conditions
  • conditions can be boolean variables
boolean myBoolean = true;
if(myBoolean) {
    // do something cool!
}
Conditions and comparisons
  • We can compare variables to each other using comparison operators
    • the result is a boolean
1 < 3    // ??
3 > 2    // ??
3 <= 3   // ??
1 >= 1   // ??
1 == 1   // ??
Conditions and comparisons
  • We also can use comparisons as conditions
int a = 3;
int b = 11;
if(a < b) {
    System.out.println("a is smaller than b!");
    System.out.println("Condition is true!");
}
Conditions and comparisons
  • We can also define else cases
int age = 12;
int minAge = 18;
if(age >= minAge) {
    System.out.println("Come on in!");
} else {
    System.out.println("You're too young.");
}

What does this program do?

Conditions and booleans - Task 1 [EASY]

Write a program that prints a text out when a condition is true

Conditions and comparisions - Task 2 [MEDIUM]

Write a program that prints the absolute difference of two int a, int b. - a=7, b=9 ↦ 2 - a=9, b=7 ↦ 2

Conditions, comparisions and data types - Task 3 [HARD]

Remember the size of different data types? e.g short and int?

Write a program that prints the product (*) of two short only if the product does not exceed the limit of short (32,767) only using variables of type short!

Any questions so far?

if not, it’s time for loops loops loops lo…
Loops
  • Loops let us execute the same code multiple times
  • Loops continue as long as a condition is true (“satisfied”)
  • Java has two general types of loops: while and for
Loops
  • a while loop is the easiest
    • .. do something while (as long as) a condition is satisfied
    boolean myLoopCondition = true;
    while(myLoopCondition) {
        // this section will get executed multiple times
    }

Question: How long will this loop continue for?

Loops
    boolean myLoopCondition = false;
    while(myLoopCondition) {
        // this section will get executed multiple times
    }

Question: How long will this loop continue for?

Loops
  • How do we avoid infinite loops?
    • We can use variables to dynamically change our loop condition once we want to
    int a = 0;
    while(a < 10) {
        a = a+1; // increment a
        System.out.println(a);
    }

Question: What happens here?

Loops
  • with continue and break we can escape a loop or skip an iteration
    int a = 0;
    while(true) {
        if(a == 10) {
            break;
        }
        a++;
    }

Question: What happens here?

Loops
int a = 0;
while(a <= 20) {
    a++;
    if(a % 2 == 0) {
        continue;
    } else {
        System.out.println(a);
    }
}

Question: What happens here? Can you think of a simpler program that does the same?

Apply your new-learned knowledge

  • Let’s build something with the knowledge we have gained today!
  • create a program for the FizzBuzz problem:
    • for all integers n from 1 to 100
    • print n, but, instead,
    • print “Fizz” if n is divisible by 3
    • print “Buzz” if n is divisible by 5
    • print “FizzBuzz” if n is divisible by both 3 and 5
  • do it for all values that fit in a short?

That’s it!

  • Be encouraged to keep working on the tasks :)
  • Feel free to reach out to us
    • are we too slow or too fast?
    • to send us your results
    • to tell us about problems you ran into

Next lesson

  • for-loops
  • Functions
  • Arrays (non-primitive data types)
  • more practical examples!

See course materials here: https://trivo25.github.io/tud-java-course/