Answer in Java and only utilise Streams to solve the question. Imperative programming (For loops and while loops) are no

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Answer in Java and only utilise Streams to solve the question. Imperative programming (For loops and while loops) are no

Post by answerhappygod »

Answer in Java and only utilise
Streams to solve the question. Imperative
programming (For loops and
while loops) are not allowed in
this question. Clearly follow the question given methods in the
image below.
Answer In Java And Only Utilise Streams To Solve The Question Imperative Programming For Loops And While Loops Are No 1
Answer In Java And Only Utilise Streams To Solve The Question Imperative Programming For Loops And While Loops Are No 1 (75.59 KiB) Viewed 32 times
Task 4 - One-Dimensional Game of Life A one-dimensional Conway's Game of Life is a population of cells represented in a list where each element represents a cell organism. Each cell may be alive or dead. Let's assume a population of nine cells with only one living organism (marked 1; 0 being dead) in the initial state (or generation 1): [0, 0, 0, 0, 1, 0, 0, 0, 0] For each generation, a cell is alive or dead depending on its own previous state and the previous states of the two neighbour cells. We adopt the following rule: • For each cell in the population, olf a cell is alive in one generation, it will be dead in the next. If a cell is dead in one generation, but has one and only one live neighbour cell, it will be alive in the next generation. Applying the above rules to the initial generation above, we obtain the next generation 2: [0, 0, 0, 1, 0, 1, 0, 0, 0] Applying the rules again to obtain generation 3 [0, 0, 1, 0, 0, 0, 1, 0, 0] Define a generateRule () method that returns the above rule in the form UnaryOperator<List<Integer>> static UnaryOperator<List<Integer>> generateRule () Define the gameoflife method that takes in a List<Integer> list as the starting population, the rule in the form UnaryOperator<List<Integer>> and the integer number of generations n of the game. The method returns a stream<String> of the game of life for n (> 0) generations where a list elemento is represented as a blank space, and 1 is represented as an asterisk * static Stream<String> gameOfLife (List<Integer> list, UnaryOperator<List<Integer>> rule, int n) jshell> Main.gameoflife (List.of(0,0,0,0,1,0,0,0,0), Main.generateRule(), 4). forEach(System.out::println) jshell> int[] arr = new int[63] arr ==> int[63] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 } jshell> arr[31] = 1 s.. ==> 1 jshell> List<Integer> list = Arrays.stream(arr).boxed().collect (Collectors.toList()) list ==> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... 0, 0, 0, 0, 0, 0, 0, 0, 0] jshell> Main.gameOfLife(list, Main.generateRule(), 32).forEach(System.out::println)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply