Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Test1Season05 {
- public static void main(String[] args) {
- Task01();
- Task02();
- Task03();
- Task04();
- }
- static void Task01() {
- System.out.println("------ TASK01 -----");
- Random rn = new Random();
- int heads = 0;
- int tries = 0;
- while (heads < 5) {
- ++tries;
- int number = rn.nextInt(2); // 0 Tails, 1 Heads
- if (number == 1) {
- ++heads;
- } else {
- heads = 0;
- }
- }
- System.out.println("Number of tries: " + tries);
- }
- static void Task02() {
- System.out.println("------ TASK02 -----");
- // Scanner sc = new Scanner(System.in);
- //
- // String str = sc.nextLine();
- String str = "Write a method that takes a String variable that holds some text The text contains words that are separated by a single space The method must return the most frequent word from the text";
- System.out.println(mostFrequentWord(str));
- }
- static String mostFrequentWord(String str) {
- String[] words = str.split(" ");
- int counter = 1;
- int maxCounter = 1;
- String res = "";
- for (int i = 0; i < words.length; i++) {
- counter = 1;
- for (int j = i + 1; j < words.length; j++) {
- if (words[i].equals(words[j])) {
- ++counter;
- }
- }
- if (counter > maxCounter) {
- maxCounter = counter;
- res = words[i];
- }
- }
- return res;
- }
- static void Task03() {
- System.out.println("------ TASK03 -----");
- int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- System.out.println(subtraction(arr, 1, arr[0], arr[0]));
- }
- static int subtraction(int[] arr, int index, int min, int max) {
- if (index == arr.length) {
- return max - min;
- }
- if (arr[index] < min) {
- min = arr[index];
- }
- if (arr[index] > max) {
- max = arr[index];
- }
- return subtraction(arr, index + 1, min, max);
- }
- static void Task04() {
- System.out.println("------ TASK04 -----");
- char[][] matrix = {
- { 'b', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
- { 'b', 'b', 'b', 'b', '-', '-', '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', 'b', 'b', '-', '-', '-', '-', 'b', 'b', 'b' },
- { '-', '-', '-', '-', 'b', 'b', '-', '-', 'b', 'b', '-', '-' },
- { '-', '-', '-', '-', '-', 'b', 'b', 'b', 'b', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
- };
- int x, y;
- x = 1;
- y = 6;
- matrix[x][y] = 'x';
- gas(matrix, x, y);
- printMatrix(matrix);
- }
- static void gas(char[][] matrix, int row, int col) {
- if (matrix[row][col] != 'x') {
- matrix[row][col] = 'r';
- }
- for (int i = -1; i < 2; i++) {
- for (int j = -1; j < 2; j++) {
- if (row + i >= 0 && row + i < matrix.length &&
- col + j >= 0 && col + j < matrix[0].length &&
- matrix[row + i][col + j] == '-') {
- gas(matrix, row + i, col + j);
- }
- }
- }
- }
- static void printMatrix(char[][] matrix) {
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement