You are on page 1of 4

Laboratory Work 1.3. “First Java programs.

Solving search
problems in arrays” (Repl.it)

The aim of the work: to learn basics of Java language and


work out skills of solving of typical search problems in
arrays.

The progress of the work


1. Learn brief theoretical material.
3. Solve the tasks below.
4. Prepare the report on the work

Brief theoretical material


For – loop for the arrays processing:
for (<initialization>; <loop_condition>; <step_update>) {

<statement1>;

<statement2>;

. . .

Simple example of the For-loop. What final value will j get after the for-loop?

int i, j = 1;

for(i = 0; i < 10; i ++)

j = j + 1;
Practical tasks

Task1. Use the code below to create an array of integers and


print it out:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner input=new Scanner (System.in);

int [] arr = {64, 25, 12, 22, 11};


int n = arr.length;

for (int i=0; i < n; i++){


System.out.println(arr[i]);

Task 2. Realize simple (linear) search of a Key number


inputted by a user. The result of the program is the index of
the Key number, if there is in the array, or the message
“There is no your number”.

Task 3. Find out minimum and maximum elements of the array


and their indexes.

Task 4. Find out the amount of two-digit elements and three-


digit elements of the array.

Task 5. Find out the amount of elements between the first


even element and last odd element of the array

Task 6. Realize binary search of a Key number inputted by a


user. The result of the program is the index of the Key
number, if there is in the array, or the message “There is no
your number”. Calculate and output the number of steps of the
search.

You might also like