Back to all lessons


Lesson 8

Methods


public class Main {
    public static void main(String[] args) {
        System.out.println((char)65); // 'A'
        System.out.println((char)(65+1)); // 'B'
        System.out.println((char)(65+2)); // 'C'
        System.out.println((char)(65+3)); // 'D'
        System.out.println((int)'z'); // 122
    }
}
        

public class HelloWorld {

    public static void main(String []args){
        sayHello(5);
        System.out.println(add(4, 5));
    }
    
    public static void sayHello(int nTimes) {
        for (int i = 0; i < nTimes; i++) {
            System.out.println("Hello");
        }
    }
    
    public static int add(int x, int y) {
        return x+y;
    }
} 
Task 0

Declare a method that prints array given as a parameter.

Task 1

Create a new method, which returns a randomly generated integer array.

Task 2

Write a method, which creates a clone of a given char array (same size and same content).

Task 3

Create a method: mergeTwoArrays. It takes 2 arrays and returns a new one, what is a join result of them.

i.e.: [1,2]+[2,3,4]=[1,2,2,3,4]

Task 4

Create a new method, which returns sum of all values in a given array. Use the method in main.

Task 5

Create a new method, which prints two values from an array, which sum is the nearest to 0. Use the method in main.

Task 6

Create a new method, which returns array with elements with reversed order. Use the method in main.

Task 7

There are two arrays (arr1 and arr2) filled in with random integer numbers. Write a program which:

  1. creates a new array containing all elements from arr1 and arr2
  2. creates a new array containing only elements that belong to both arrays
  3. creates a new array containing all elements from arr1 and arr2 without repeating
Task 8

Write a method that generates random array of integers (20 elements) and prints its content without repetition of elements.

Task 9

Write a method that generates random array of integers (200 elements) and prints its the most common element.

Task 10

Pass null as a value for array to call any method.

Task 11

Create a method that multiplies every element of copied array by a given number.

Task 12

Read about Fibonacci sequence. Display n-th number of the sequence without using any loop. Tip: call a method in the same method.

Task 13

Display an array without using any loop, nor ready to use methods of Java Api.

Task 14

Using task 12. code, calculate 1000th Fibonacci number. Then create an implementation with any loop and compare it with recursion.

Task 15 (pointed task 2019)

Create methods (to make main working):

  • generateRandomArray
  • copyArray
  • areArraysEqual
  • mergeTwoArrays
  • generateRandomMessage
  • encrypt
  • decrypt

For encrypting you can create your own formula or use existing one (for example: Caesar cipher).

It is forbidden to modify main method.


public class PointedTask2019Edition {
    public static void main(String[] args) {

    int arraySize = 100;

    int[] model = generateRandomArray(arraySize);

    int[] clone = copyArray(model);
    if (areArraysEqual(model, clone))
        System.out.println("I score 1 point");
    else
        System.out.println("Something is wrong");

    int[] merged = mergeTwoArrays(model, clone);
    if (merged.length == arraySize*2)
        System.out.println("I score 1 point");
    else
        System.out.println("Something is wrong");


    char from = 'a';
    char to = 'z';
    char[] message = generateRandomMessage(arraySize, from, to);

    char[] encrypted = encrypt(message);
    System.out.print("Encrypted: ");
    printArray(encrypted);

    char[] decrypted = decrypt(encrypted);
    System.out.print("Decrypted: ");
    printArray(decrypted);

    if (areArraysEqual(message, decrypted))
        System.out.println("Maybe I score 3 points");
    else
        System.out.println("Maybe I score 0, 1, 2 points...");

    }

    // tip: start here

}