Last active
October 24, 2016 19:38
-
-
Save FreeFly19/bed78f742471dfae60a7d3676d0dbeba to your computer and use it in GitHub Desktop.
Class examples #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Ex1 { | |
| public static void main(String[] args) { | |
| int[] arr = new int[3]; | |
| arr[0] = 1; | |
| arr[1] = 423; | |
| arr[2] = -32; | |
| for(int i = 0; i < arr.length; i++) { | |
| System.out.println(arr[i]); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Ex2 { | |
| public static void main(String[] args) { | |
| System.out.println(sum(3, 6)); // Виклик функції (function call/invocation) | |
| } | |
| static int sum(int a, int b) { // Оголошення функції (function declaration) | |
| int s = a + b; | |
| return s; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Ex3 { | |
| public static void main(String[] args) { | |
| int[] arr = generateRandomArray(10, 0, 10); | |
| printArray(arr); | |
| } | |
| static void printArray(int[] arr) { | |
| for (int i = 0; i < arr.length; i++) { | |
| System.out.println(arr[i]); | |
| } | |
| } | |
| static int[] generateRandomArray(int length, int start, int end) { | |
| int[] arr = new int[length]; | |
| for (int i = 0; i < length; i++) { | |
| arr[i] = random(start, end); | |
| } | |
| return arr; | |
| } | |
| static int random(int start, int end) { | |
| int range = end - start; | |
| int shift = start; | |
| int random = (int) (Math.random() * range) + shift; | |
| return random; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment