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

JetBrains Academy: Conditional statement

Boxes:

There are two boxes on the table. The first box has a size of X1 x Y1 x Z1, and the second box has a size of X2 x Y2 x Z2. You need to determine which box can be put inside another box. You can rotate both boxes as you want.

Input contains two lines. The first line contains numbers X1, Y1, Z1, the second line contains numbers X2, Y2, Z2. All numbers are integers and greater than 0.

If the sizes of the boxes are equal, output "Box 1 = Box 2". If the first box can be put inside the second box, output "Box 1 < Box 2". If the second box can be put inside the first box, output "Box 1 > Box 2". If none of the boxes can be put inside the other box, output "Incomparable".

import java.util.*;

public class Boxes {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Box 1 dimensions
        int x1 = scanner.nextInt();
        int y1 = scanner.nextInt();
        int z1 = scanner.nextInt();
        int[] box1 = {x1, y1, z1};
        Arrays.sort(box1);
        x1 = box1[0];
        y1 = box1[1];
        z1 = box1[2];

        // Box 2 dimensions
        int x2 = scanner.nextInt();
        int y2 = scanner.nextInt();
        int z2 = scanner.nextInt();
	int[] box2 = {x2, y2, z2};
	Arrays.sort(box2);
	x2 = box2[0];
	y2 = box2[1];
	z2 = box2[2];

	if (x1 < x2) {
	    if (y1 <= y2) {
	        if (z1 <= z2) {
		    System.out.println("Box 1 < Box 2");
	        } else {
		    System.out.println("Incomparable");
	        }
	    } else {
	        System.out.println("Incomparable");
	    }
	} else if (x1 > x2) {
	    if (y1 >= y2) {
	        if (z1 >= z2) {
	            System.out.println("Box 1 > Box 2");
	        } else {
	    	    System.out.println("Incomparable");
	        }
	    } else {
	    	System.out.println("Incomparable");
	    }
	} else if (x1 == x2) {
	    if (y1 < y2){
	    	if (z1 <= z2){
	    	    System.out.println("Box 1 < Box 2");
	    	} else {
	            System.out.println("Incomparable");
	    	}
	    } else if (y1 > y2){
	    	if (z1 >= z2){
	            System.out.println("Box 1 > Box 2");
	        } else {
	            System.out.println("Incomparable");
	        }
	    } else {
	        if (z1 < z2) {
                    System.out.println("Box 1 < Box 2");
    	        } else if (z1 > z2) {
	            System.out.println("Box 1 > Box 2");
	        } else {
	    	    System.out.println("Box 1 = Box 2");
	    	}
	    }
	}
    }
}

Chocolate:

A chocolate bar has the shape of a rectangle, divided into NxM segments. You can break down this chocolate bar into two parts by a single straight line (only once). Find whether you can break off exactly K segments from the chocolate. Each segment is 1x1.

The program gets an input of three integers: N, M, K

The program must output one of the two words: YES or NO.

import java.util.Scanner;

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

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();

        if (n*m >= k){
            if (k % m == 0 || k % n == 0){
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        } else {
            System.out.println("NO");
        }
    }
}

Healthy sleep:

Ann watched a TV program about health and learned that it is recommended to sleep at least A hours per day, but oversleeping is also not healthy, and you should not sleep more than B hours. Now Ann sleeps H hours per day. If Ann's sleep schedule complies with the requirements of that TV program – print "Normal". If Ann sleeps less than A hours, output “Deficiency”, and if she sleeps more than B hours, output “Excess”.

Input to this program are the three strings with variables in the following order: A, B, H. A is always less than or equal to B.

import java.util.*;

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

        String a = scanner.nextLine();
        String b = scanner.nextLine();
        String h = scanner.nextLine();

        int minimumSleepTime = Integer.valueOf(a);
        int maximumSleepTime = Integer.valueOf(b);
        int sleepTimeAnn = Integer.valueOf(h);

        if (sleepTimeAnn < minimumSleepTime){
            System.out.println("Deficiency");
        } else if (sleepTimeAnn <= maximumSleepTime){
            System.out.println("Normal");
        } else {
            System.out.println("Excess");
        }
    }
}

Interval:

Given an integer as input. Output True if its value is within the interval (−15,12]∪(14,17)∪[19,+∞), and False otherwise.

import java.util.Scanner;

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

        int x = scanner.nextInt();

        if (x > -15 && x <= 12 || x > 14 && x < 17 || x >= 19){
            System.out.println("True");
        } else {
            System.out.println("False");
        }
    }
}

Leap year:

Find whether the given year is a leap year.

Just a reminder: leap years are those years in which the year’s number is either divisible by 4, but not divisible by 100, or divisible by 400 (for example, the year 2000 is a leap year, but the year 2100 will not be a leap year).

The program should work correctly for the years 1900 ≤ n ≤ 3000.

Output "Leap" if the given year is a leap year, and "Regular" otherwise.

import java.util.Scanner;

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

        int year = scanner.nextInt();

        if (year >= 1900 && year <= 3000){
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
                System.out.println("Leap");
            } else {
                System.out.println("Regular");
            }
        } else{
            System.out.println("Year provided must be within range from year 1900 till 3000. Program will now terminate.");
        }
    }
}

Queens:

You are given coordinates of two queens on a chess board. Find out whether or not they hit each other.

Input data format - four integer numbers: x1, y1, x2, y2

Type "YES" if they hit each other or "NO" if they don't.

import java.util.Scanner;

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

        int x1 = scanner.nextInt();
        int y1 = scanner.nextInt();
        int x2 = scanner.nextInt();
        int y2 = scanner.nextInt();

        if (x1 == x2 || y1 == y2 || Math.abs(x1-x2) == Math.abs(y1-y2)){
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

Symmetrical number:

Given a four-digit number. Determine whether its decimal notation is symmetric. If the number is symmetric, output 1; otherwise output any other integer. The number may have less than four digits; in this case you should assume that its decimal notation is complemented by insignificant zeros on the left.

import java.util.*;

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

        String number = scanner.nextLine();
        int[] tablica = {0, 0, 0, 0};
        int output;

        if (number.length() == 1){
            tablica[3] = Character.getNumericValue(number.charAt(0));
        } else if (number.length() == 2){
            tablica[2] = Character.getNumericValue(number.charAt(0));
            tablica[3] = Character.getNumericValue(number.charAt(1));
        } else if (number.length() == 3){
            tablica[1] = Character.getNumericValue(number.charAt(0));
            tablica[2] = Character.getNumericValue(number.charAt(1));
            tablica[3] = Character.getNumericValue(number.charAt(2));
        } else if (number.length() == 4){
            tablica[0] = Character.getNumericValue(number.charAt(0));
            tablica[1] = Character.getNumericValue(number.charAt(1));
            tablica[2] = Character.getNumericValue(number.charAt(2));
            tablica[3] = Character.getNumericValue(number.charAt(3));
        } else {
            System.out.println("Number cannot be analyzed. Program will now terminate.");
        }

        if (tablica[0] == tablica[3] && tablica[1] == tablica[2]){
            output = 1;
        } else {
            output = (int) (Math.random() * 50);
        }
        System.out.println(output);
    }
}

The army of units:

In a computer game, each gamer has an army of units. Write a program that will classify the army of your enemies corresponding to the following rules:

Units Categories:

  • less than 1: no army
  • from 1 to 4: few
  • from 5 to 9: several
  • from 10 to 19: pack
  • from 20 to 49: lots
  • from 50 to 99: horde
  • from 100 to 249: throng
  • from 250 to 499: swarm
  • from 500 to 999: zounds
  • 1000 and more: legion

The program should read the number of units and output the corresponding category.

import java.util.Scanner;

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

        int numberOfUnits = scanner.nextInt();

        if (numberOfUnits < 1){
            System.out.println("no army");
        } else if (numberOfUnits < 5){
            System.out.println("few");
        } else if (numberOfUnits < 10){
            System.out.println("several");
        } else if (numberOfUnits < 20){
            System.out.println("pack");
        } else if (numberOfUnits < 50){
            System.out.println("lots");
        } else if (numberOfUnits < 100){
            System.out.println("horde");
        } else if (numberOfUnits < 250){
            System.out.println("throng");
        } else if (numberOfUnits < 500){
            System.out.println("swarm");
        } else if (numberOfUnits < 1000){
            System.out.println("zounds");
        } else {
            System.out.println("legion");
        }
    }
}

Triangle:

_Given three natural numbers A, B, C. Determine if a triangle with these sides can exist. _

If the triangle exists - output the YES string, otherwise - output NO.

import java.util.*;

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

        int[] dimensions = new int[3];
        dimensions[0] = scanner.nextInt();
        dimensions[1] = scanner.nextInt();
        dimensions[2] = scanner.nextInt();

        Arrays.sort(dimensions);

        if (dimensions[0] + dimensions[1] > dimensions[2]){
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

Solve a simple equation:

Given a simple equation, you should output the correct value for variable "x". The equation has two numbers greater than 0 and variable "x", and between these can be "+", "-" or "=". Numbers, variable "x", symbols "+", "-", "=" all separated by a space.

It is guaranteed that the equation is correct. The result should be an integer.

import java.util.*;

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

        String a = scanner.next();
        String sign1 = scanner.next();
        String b = scanner.next();
        String sign2 = scanner.next();
        String c = scanner.next();
        int x;

        if("x".equals(a)){
            int bNum = Integer.valueOf(b);
            int cNum = Integer.valueOf(c);

            if ("+".equals(sign1)){
                x = cNum - bNum;
            } else if ("-".equals(sign1)){
                x = cNum + bNum;
            } else {                        // sign 1 equals =
                if ("+".equals(sign2)){
                    x = bNum + cNum;
                } else {                    // sign 2 equals -
                    x = bNum - cNum;
                }
            }
            
        } else if ("x".equals(b)){
            int aNum = Integer.valueOf(a);
            int cNum = Integer.valueOf(c);

            if ("+".equals(sign1)){
                x = cNum - aNum;
            }else if ("-".equals(sign1)){
                x = -(cNum - aNum);
            }else {                         // sign1 equals =
                if ("+".equals(sign2)){
                    x = aNum - cNum;
                } else {                    // sign2 eauals -
                    x = aNum + cNum;
                }
            }
            

        } else {                            // c equals x
            int aNum = Integer.valueOf(a);
            int bNum = Integer.valueOf(b);

            if ("+".equals(sign1)){
                x = aNum + bNum;
            } else if ("-".equals(sign1)){
                x = aNum - bNum;
            } else {                        // sign 1 equals =
                if ("+".equals(sign2)){
                    x = aNum - bNum;
                } else {                    // sign 2 equals -
                    x = -(aNum - bNum);
                }
            }
        }
        System.out.println(x);
    }
}

⚠️ **GitHub.com Fallback** ⚠️