JetBrains Academy: Switch statement - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Switch statement

Shape:

Write a program, which reads the number of the shape (1 – square, 2 – circle, 3 – triangle, 4 – rhombus) and prints the text "You have chosen a square" (or circle, or triangle, or rhombus, depending on the number). If it is a number that doesn't correspond to any of the listed shapes, the program should output: "There is no such shape!".

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        switch(scanner.nextInt()){
            case 1:
                System.out.println("You have chosen a square");
                break;
            case 2:
                System.out.println("You have chosen a circle");
                break;
            case 3:
                System.out.println("You have chosen a triangle");
                break;
            case 4:
                System.out.println("You have chosen a rhombus");
                break;
            default:
                System.out.println("There is no such shape!");
                break;
        }
    }
}

Direction:

Write a program, which reads the number of direction (1 – up, 2 – down, 3 – left, 4 – right, 0 – stay) and outputs the text move up (or move down, or move left, or move right, or do not move depending on the entered number). If it is a number that does not belong to any of the listed directions, the program should output: error!

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int direction = scanner.nextInt();

        switch (direction){
            case 1:
                System.out.println("move up");
                break;
            case 2:
                System.out.println("move down");
                break;
            case 3:
                System.out.println("move left");
                break;
            case 4:
                System.out.println("move right");
                break;
            case 0:
                System.out.println("do not move");
                break;
            default:
                System.out.println("error!");
                break;
        }
    }
}

A simple calculator:

Write a simple calculator that reads the three values from the line: the first number, the operation, and the second number.

The program should apply the operation to the numbers entered ("first number" "operation" "second number") and output the result to the screen. Note that the numbers are long.

The calculator should support:

  • addition: "+"
  • subtraction: "-"
  • integer division: "/"
  • multiplication: "*"

If a user performs division and the second number is 0, it is necessary to output the line "Division by 0!".

If the input operator is not one from the list, the program should output "Unknown operator".

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long firstNumber = scanner.nextLong();
        String operator = scanner.next();
        long secondNumber = scanner.nextLong();

        switch (operator){
            case "+":
                System.out.println(firstNumber + secondNumber);
                break;
            case "-":
                System.out.println(firstNumber - secondNumber);
                break;
            case "/":
                switch ((int)secondNumber){
                    case 0:
                        System.out.println("Division by 0!");
                        break;
                    default:
                        System.out.println(firstNumber / secondNumber);
                        break;
                }
                break;
            case "*":
                System.out.println(firstNumber * secondNumber);
                break;
            default:
                System.out.println("Unknown operator");
                break;
        }
    }
}

Floor-space of the room:

Residents of the country of Malevia often experiment with the plan of their rooms. Rooms can be triangular, rectangular, and round.

To quickly calculate the floorage it is required to write a program that gets the type of the room shape and the relevant parameters as input. The program should output the area of the resulting room.

The value of 3.14 is used instead of the number Ο€ in Malevia.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String roomShape = scanner.nextLine();
        double floorage = 0.0;

        switch (roomShape){
            case "triangle":
                double a = scanner.nextDouble();
                double b = scanner.nextDouble();
                double c = scanner.nextDouble();
                double heron = (a+b+c)/2;
                floorage = Math.sqrt(heron*(heron-a)*(heron-b)*(heron-c));
                break;
            case "rectangle":
                double sideA = scanner.nextDouble();
                double sideB = scanner.nextDouble();
                floorage = sideA * sideB;
                break;
            case "circle":
                double r = scanner.nextDouble();
                floorage = 3.14 * Math.pow(r, 2);
                break;
            default:
                System.out.println("Unrecognizable shape");
                return;
        }
        System.out.println(floorage);
    }
}

Test learners:

We need your help to improve an adaptive system. Write a program that asks learners what programming language they are learning. I hope you know which answer is correct.

Question: what programming language are you learning here?

  1. Java
  2. Kotlin
  3. Scala
  4. Python

The program should read the answer’s number from the standard input and output the result of the test: "Yes!", "No!" or "Unknown number".

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        switch (scanner.nextInt()){
            case 1:
                System.out.println("Yes!");
                break;
            case 2:
            case 3:
            case 4:
                System.out.println("No!");
                break;
            default:
                System.out.println("Unknown number");
                break;
        }
    }
}

From string to a number:

Write a program that read a string from the standard input and outputs the number. A number can be from 1 to 9 (inclusive).

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        switch (scanner.next()){
            case "one":
                System.out.println(1);
                break;
            case "two":
                System.out.println(2);
                break;
            case "three":
                System.out.println(3);
                break;
            case "four":
                System.out.println(4);
                break;
            case "five":
                System.out.println(5);
                break;
            case "six":
                System.out.println(6);
                break;
            case "seven":
                System.out.println(7);
                break;
            case "eight":
                System.out.println(8);
                break;
            case "nine":
                System.out.println(9);
                break;
            default:
                System.out.println("Unknown number");
                break;
        }
    }
}

Harry Potter:

Harry Potter needs help identifying what each house means.

Read a string representing a house and output the following:

  • if it is "gryffindor", output "bravery";
  • if it is "hufflepuff", output "loyalty";
  • if it is "slytherin", output "cunning";
  • if it is "ravenclaw", output "intellect";
  • otherwise, output "not a valid house".

The problem was taken from the course Introduction to JavaScript and React by Ken McGrady and adapted for Hyperskill.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Locale localePL = new Locale.Builder().setLanguage("pl").setRegion("PL").build();
        Scanner scanner = new Scanner(System.in);
        String house = scanner.nextLine().toLowerCase(localePL);
        String values;
        
        switch (house){
            case "gryffindor":
                values = "bravery";
                break;
            case "hufflepuff":
                values = "loyalty";
                break;
            case "slytherin":
                values = "cunning";
                break;
            case "ravenclaw":
                values = "intellect";
                break;
            default:
                values= "not a valid house";
                break;
        }
        System.out.println(values);
    }
}