Programming Java

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

OBJECT-ORIENTED JAVA PROGRAMMING - LABORATORY

Iterative Statements

While Loop:

while (condition) {
    // Code block to execute
}

Do…While Loop:

do {
    // Code block to execute
} while (condition);

Example:

package com.company;
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello Java");

        int itemCounter = 0;
        while (itemCounter < 5) {
            System.out.println(itemCounter);
            itemCounter++;
        }

        itemCounter = 0;
        do {
            System.out.println(itemCounter);
            itemCounter++;
        } while (itemCounter < 5);
    }
}

For Loop:

for (<variable declaration>; <loop condition>; <variable update>) {
    // Code block to execute
}

Example:

package com.company;
public class Main {
    public static void main(String[] args) {
        System.out.println("Incrementing:");
        for (int itemNumber = 0; itemNumber < 5; itemNumber++) {
            System.out.println(itemNumber);
        }

        System.out.println("Decrementing:");
        for (int itemNumberMax = 8; itemNumberMax > 0; itemNumberMax--) {
            System.out.println(itemNumberMax);
        }
    }
}

Random Number Generation

package com.company;
// Import the Random class
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        // Create an instance of Random
        Random rand = new Random();

        // Generate a random number between 0 and 10 (inclusive)
        int a = rand.nextInt(11);
        System.out.println("Random number in [0,10]: " + a);

        // Generate a random number in the range [-5,15]
        System.out.print("Random number in [-5,15]: ");
        System.out.println(rand.nextInt(21) - 5);

        // Generate a random number in the range [-20,-10]
        a = rand.nextInt(11) - 20;
        System.out.println("Random number in [-20,-10]: " + a);

        // Generate a random number in the range [x,y], where x and y are variables
        int x = 7; // Example value
        int y = 15; // Example value
        a = rand.nextInt(y - x + 1) + x;
        System.out.println("Random number in [" + x + "," + y + "]: " + a);

        // Test the example with negative numbers
        x = -27; // Example value
        y = -15; // Example value
        a = rand.nextInt(y - x + 1) + x;
        System.out.println("Random number in [" + x + "," + y + "]: " + a);

        // Test the example where one number is negative and the other is positive
        x = -7; // Example value
        y = 15; // Example value
        a = rand.nextInt(y - x + 1) + x;
        System.out.println("Random number in [" + x + "," + y + "]: " + a);
    }
}

Tasks:

Task 1

The laboratory group consists of n students (value n is provided by the user). Input the number of points for each student. Write a program that calculates the average number of points in the group using a while loop.

Task 2

Write a program that allows the user to input 10 numbers and determine the count and sum of negative and positive numbers provided by the user.

Task 3

Given a sequence of n numbers (n > 0), write a program to calculate the sum of all even numbers in the sequence.

Task 4

Using the algorithm from Task 5, write a program that allows the user to generate n random numbers in the range (-10,45) and calculates the sum of all even numbers in the sequence.

Task 5

Write a program that checks whether a word provided by the user is a palindrome. A palindrome is a word that reads the same forward and backward, e.g., “kayak.”