Back to all lessons


Lesson 4

Switch+prompt

Task 0

Rewrite code to switch-version (if possible):


public class Hello {
    public static void main(String[] args) {
        int a = 2;
        if (a == 2) {
            System.out.println("First");
        } else if (a == 5) {
            System.out.println("Second");
        } else if (a == 10) {
            System.out.println("Third");
        } else if (a == 20) {
            System.out.println("Forth");
        } else {
            System.out.println("None");
        }
    }
}

Task 1

Rewrite code to switch-version (if possible):


public class Hello {
    public static void main(String[] args) {
        int a = 2;
        if (a > 2) {
            System.out.println("First");
        } else if (a > 5) {
            System.out.println("Second");
        } else if (a > 10) {
            System.out.println("Third");
        } else if (a > 20) {
            System.out.println("Forth");
        } else {
            System.out.println("None");
        }
    }
}

Task 2

Prompt user for input. We need just 1 number. Display absolute value of a number.

Task 3

Prompt user for input. We need to take 2 numbers. You have to calculate value of division and reminder, i.e.: 123=22*5+13

Task 4

Create universal gravity calculator. Ask user for all data you need and display result. Formula: https://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation

Make sure you don't divide by 0.

Task 5

Create a quadratic equation calculator: https://en.wikipedia.org/wiki/Quadratic_equation

Hint: use Math.sqrt(double).

Task 6

Prompt user for: number of a month and number of day.

Display number of the day according to whole year.

i.e.: September 11 = 255th day of year