Question: Lab 3 - Instructions At Attached Files Lab3 Parti Java (573) Lab3 Partit java (1.32 KB) (THE LAB SAMPLE CODE IS ATATCHED.) Intro: This Lab
Lab 3 - Instructions At Attached Files Lab3 Parti Java (573) Lab3 Partit java (1.32 KB) (THE LAB SAMPLE CODE IS ATATCHED.) Intro: This Lab contains two parts. Each part is it's own class file The first part (Part I) is simply to create a Java program which calculates the answers to the function practice we did yesterday in class (laba Part java) The second part is about "generalizing the code in Lab 2 a bit further to eliminate redundant for loops - where identical inner loops appear in multiple places - by adding a few additional methods which encapsulate the loop behavior (Lab3 Part2.java) Examples are attached to this Lab Part 1: (Class name: Lab3_Parti) 1. Create valid Java functions definitions for each of the following (see attached example) fun1(x) = 2 * x fun2(x, y, z) = x + y + z fun3(x, y) = funi (2 * x) + y fund (x, y) = fun3(x, funl(y)) 2. Create a "main" method which prompts the user for the values to pass to each of the functions (as per the worksheet", see the attached example). Assuming the USER has entered the values for x, y, and z are calculated as follows. a = fun1 (2) b = fun2(x,y,z) c = fun3 (x,y) d = fun 4 (x,y) e = fun3 (x, fun1 (x)) 3. Display the results. Assuming the user entered 1,2, and 3 for the value of x, y, and z, the output for would look like this (see the attached example): funi (1) = 2 7 3. Display the results. Assuming the user entered 1.2 and 3 for the value of x, y, and z, the output for would look like this (see the attached example): fun1(1) = 2 fun2(1,2,3) = 7 fun3 (1,2) = 6 fun4 (1,2) = 8 fun 3 (1, funi (1)) = 6 Part II: (Class name: Lab3_PartII) 1. This part is intentionally a bit less specific as the objective is for you to minimize the unnecessary redundancy in Labz by "generalizing the loop redundancy - where possible - by encapsulating the inner loops" (within the shape drawing functions) into their own functions. Just like back in Lab, some functionally will be unique enough to keep everything together in a single function, other places may have the same reoccurring structures which could be broken out into their own slapper functions. In short: Remove the inner-loops from your Lab Solution and, WHERE POSSIBLE, replace them with calls to functions/methods, passing any necessary parameters. (It's best to see the attached example for an example). And yes... as is true with MOST REAL-WORLD PROGRAMMING, this part is a bit more "subjective" and there is not "one correct way of breaking things apart. // SAMPLE OF LAB3_Parti components. import java.util.Scanner; public class Lab3_PartI { public static void main(String[] args) { Scanner console = new Scanner(System.in); // get user input int x, y, z; System.out.print("Enter x: "); x = console.nextInt(); // get results int a = fun1(x); 1/int b = fun2 (x,y,z); // display answer key System.out.printf("fun1( 8d) = %d", x, a); } static int fun1(int x) { int result = 2 * x; return result; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
