Skip to content

Instantly share code, notes, and snippets.

@FreeFly19
Last active October 24, 2016 19:38
Show Gist options
  • Select an option

  • Save FreeFly19/bed78f742471dfae60a7d3676d0dbeba to your computer and use it in GitHub Desktop.

Select an option

Save FreeFly19/bed78f742471dfae60a7d3676d0dbeba to your computer and use it in GitHub Desktop.
Class examples #3
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]);
}
}
}
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;
}
}
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