Programming Java

Tasks studies - laboratory


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

Lab03 - Operators, Recursion, String Type in Java

[1.] Operators

Task 1.1

What will the following program return? Justify your answer.

public class IncDec {
  public static void main(String[] arg) {
    double a = 12.12;
    System.out.println(a--);
    System.out.println(a++);
    System.out.println(--a);
    System.out.println(++a);
  }
}

Task 1.2

Write a program demonstrating the use of logical operators && (AND) and || (OR). Use an if conditional structure.
Task 2.2 What are the differences between the following operators:

Task 1.3

Study the following article: LINK
Then write programs demonstrating the behavior of the following operators:

Task 1.4

Consider the following code fragment:

int a = 17;
double b = 4.0;
a += b; // ?
a -= b; // ?
a *= b; // ?
a /= b; // ?
a %= b; // ?

What arithmetic operations do these expressions represent, and what results will they return?

Task 1.5

Consider the following code fragment:

int a = 5;
int b = 3;
int c = a + b++;
int d = (a++) + b;

In what order will the operations be performed during the computation of variables c and d? What values will be calculated?

Task 1.6

What operators in Java define logical operations NOT and XOR? Create appropriate programs demonstrating their behavior as logic gates.

Task 2.7

Create a program using an operator opposite to the comparison operator ==.

[2.] String Type

Task 2.1

Familiarize yourself with the documentation of the String class:
LINK
Test the following program:

public class StringExample {
  public static void main(String args[]) {
    String s1 = new String("Happy ");
    String s2 = new String("Birthday");
    System.out.printf("s1 = %s\ns2 = %s\n\n", s1, s2);
    System.out.printf("Result of s1.concat(s2) = %s\n", s1.concat(s2));
    System.out.printf("s1 after concatenation = %s\n", s1);
  }
}

Task 2.2

Describe the functionality of the following String class methods in words and create simple Java applications to demonstrate their operation—use the above program as a template. Display the results on the console.

[3.] Recursion

Task 3.1

Test the following program:

import java.util.*;
public class Factorial {
  // The factorial method returns the factorial of a number passed as a parameter
  // Factorial computation is performed using recursion
  public static int factorial(int value) {
    // If the passed parameter equals zero, return 1
    // Otherwise, return the parameter value * factorial of (parameter - 1)
    if (value == 0) return 1;
    else return value * factorial(value - 1);
  }
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a number: ");
    // User inputs a number
    int number = sc.nextInt();
    // Display the calculated factorial
    System.out.println(number + "! = " + factorial(number));
  }
}

When the application is executed:


cmd_gcc

Analysis: Entering the number 5 as input will call the factorial method with the value 5. The method will return the parameter value multiplied by another call to the factorial method with the parameter decremented by 1, and so on.
The process will look as follows:


cmd_gcc

Task 3.2

Create a program to calculate factorial without using recursion. Use any type of loop to perform the operation.

Task 3.3

Implement the exponentiation algorithm in two versions: recursive and iterative (as two separate methods). To complete the task, refer to the following material:
LINK

Task 3.4

Implement an algorithm to calculate the Fibonacci sequence. To complete the task, refer to the following material:
LINK

Task 3.5 (optional)

Write an algorithm to solve the Towers of Hanoi problem.
To complete the task, refer to the following material:
LINK