Back to all lessons


Lesson 3

Variables and if(s)

Task 0

Declare variable and assign a value. Display the variable in the next line.

Swap the lines. Try to compile and describe compiler behavior.

Task 1

Define three numerical variables: a, b, c.

Write a program that sorts it and display result i.e. "cab".

Task 2

Declare integer. Try to assign value 10.0.

Repair the code.

Task 3

We want to display "0.5", so we have written:


public class A {
    public static void main(String[] args) {
        System.out.println(1/2);
    }
}
            
Is the result correct? If no, how we can fix it?

Task 4

Use "? :" operator. Show "even" or "odd" depending of user's input.

Task 5

Create an if-statement with "&&" in a condition. Then achieve the same results without ampersands.

Task 6

Unix-like operating systems use a number to tell about permissions for file or directory.

Example: 762 means "User can everything, group can write and read, anyone can write"


Number meaning:

  • first digit - user permission
  • second digit - group permission
  • third digit - anyone permission

Every digit meaning:

  • 4 - can read
  • 2 - can write
  • 1 - can execute

Write a program, that explains what a permission number means in human readable text.