01 - Introduction to Intermediate Java Course

Michele

Goals

  • Getting to know each other
  • Get familiar with schedule, attendance, tools
  • Course introduction
  • Install required software
  • Do some basic Java exercises (hopefully 🀩)

Slides

Java Basics Recap

package com.redi.j2;

public class Main {

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

        String name = "Memet";
        String profession = "skydiver";
        int age = 40;
        boolean smoking = false;

        someMethod(profession);

        for (int i = 0; i < 10; i++) {
            System.out.println("hey " + i);
        }
    }

    private static void someMethod(String profession) {
        if (profession.equals("skydiver")) {
            System.out.println("Cool");
        } else {
            System.out.println("Not cool");
        }
    }
}

Exercises

Same exercises as on the slides, copied here for convenience.

Exercise 1

Write the method squares that for X and Y given by arguments prints the square of each number between X and Y.

square(1,3)
> 1 - 1
> 2 - 4
> 3 - 9

square(5,6)
> 5 - 25
> 6 - 36

😎 Toggle Solution
private static void squares(int x, int y) {
    for (int i = x; i < y; i++) {
        System.out.println(i + " - " + (i * i));
    }
}

Exercise 2

In german, nouns ending with e are almost always feminine. Write a method isFeminineNoun that checks if the provided word ends with e or not.

isFeminineNoun("Katze")
> true

isFeminineNoun("Hund")
> false

Bonus: can you also make sure the method also catches words ending in -ung? Those are also feminine.


😎 Toggle Solution
private static void isFeminineNoun(String noun) {
    boolean isFeminine = noun.endsWith("e");
    System.out.println(isFeminine);
}

private static void isFeminineNounExpanded(String noun) {
    boolean isFeminine = noun.endsWith("e") || noun.endsWith("ung");
    System.out.println(isFeminine);
}

Exercise 3

Let’s assume a freelancer has to pay insurance and income tax. Let’s assume insurance is fixed at 300€, while income tax is 9% for incomes of less than 1000€ (after insurance payment), and 21% otherwise.

inPocket(5000)
> 3713

inPocket(1000)
> 637

😎 Toggle Solution
private static void inPocket(int wage) {
    int afterInsurance = wage - 300;
    double taxation;

    if (afterInsurance > 1000) {
        taxation = afterInsurance * 0.21;
    } else {
        taxation = afterInsurance * 0.09;
    }

    double afterTaxes = afterInsurance - taxation;
    System.out.println(afterTaxes);
}

Homework

Write a program that for given value of variable β€˜height’ will print out the right-half of a pine tree to the console.

  • The tree starts with β€œI” on the top and ends with β€œM” on the bottom.
  • The tree is built from β€œX” and β€œY” characters one after another

For example, for an height of 6 it will print:

I
XY
XYX
XYXY
XYXYX
M

😎 Toggle Solution
private static void tree (int height) {
    if (height < 3) {
        System.out.println("A tree must have a height of at least 3");
    }

    System.out.println("I");
    String treeLine = "X";
    int effectiveHeight = height - 2;
    for (int i=0; i< effectiveHeight; i++) {
        treeLine += (i%2 == 0) ? "Y" : "X";
        System.out.println(treeLine);
    }
    System.out.println("M");
}

Additional Resources

Last Updated: 8/28/2019, 2:28:02 PM