Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Task 1: public class ArraySum { /** * Implement a recursive method that takes an integer array, two indices as arguements * calulate the

image text in transcribed

image text in transcribed

// Task 1:

public class ArraySum { /** * Implement a recursive method that takes an integer array, two indices as arguements * calulate the sum from start to end of the array, and return the result. */ public static int arrSum(int[] arr, int start, int end) { // Filling your code here

// End of your code } }

// Task 2:

public class NodeStacking { // use this weight for calculating public static int weight = 128;

/** * Implement a recursive method that takes row index and column index of a node. * calculate the weight it supports. */ public static double weightSupporting(int row, int col) { // Filling your code here

// End of your code } }

Lab 5 This lab is to be completed individually This lab is for you to understand recursion. Please find two files ArraySum.java, NodeStacking.java and fill the method body. What to do? Task 1 Design and implement a recursive method for calculating the sum of an integer 1D-array with given start and end index. If the input index is invalid, please return O instead. For example, Input: (1,2,3,4,5,6), 2,5 Output: 18 Input: (1,2,3,4,5,6), -1,8 Output: 0 Task 2 Suppose there's a structure as follows. Each row of nodes is stacked together to form a pyramid. Each node weighs 128 pounds, and splits its weight evenly on the two nodes below it in the pyramid. For example, node A splits its weight across node B and node C. Node F splits its weight, plus the accumulated weight of the nodes it is supporting, onto node I and node J. Suppose this structure has infinite rows. The goal is to design and implement a recursive method that calculates how much weight a given node is supporting by specifying its row index and column index. Both indices start from 0, and each row has exact row index + 1 nodes. i.e. A is in row 0, column 0. F is in row 2, column 2. Your method should return -1 if the input indices are invalid. For example, Input 0, 0 which represents A, the output should be 0 Input 0, 1, which is an invalid index. The output should be -1 Input 1, 1, which is C. The supported weight should be the half of A's weight and the weight it's carrying. 1/2* (128 + 0) = 64. Thus, output should be 64. Please put your code between /* #. Filling your code here */ and /* #. End of code */. Follow the instructions in comments carefully. Do not change the code or structure outside the blocks. Make your own test cases to test the program. Lab 5 This lab is to be completed individually This lab is for you to understand recursion. Please find two files ArraySum.java, NodeStacking.java and fill the method body. What to do? Task 1 Design and implement a recursive method for calculating the sum of an integer 1D-array with given start and end index. If the input index is invalid, please return O instead. For example, Input: (1,2,3,4,5,6), 2,5 Output: 18 Input: (1,2,3,4,5,6), -1,8 Output: 0 Task 2 Suppose there's a structure as follows. Each row of nodes is stacked together to form a pyramid. Each node weighs 128 pounds, and splits its weight evenly on the two nodes below it in the pyramid. For example, node A splits its weight across node B and node C. Node F splits its weight, plus the accumulated weight of the nodes it is supporting, onto node I and node J. Suppose this structure has infinite rows. The goal is to design and implement a recursive method that calculates how much weight a given node is supporting by specifying its row index and column index. Both indices start from 0, and each row has exact row index + 1 nodes. i.e. A is in row 0, column 0. F is in row 2, column 2. Your method should return -1 if the input indices are invalid. For example, Input 0, 0 which represents A, the output should be 0 Input 0, 1, which is an invalid index. The output should be -1 Input 1, 1, which is C. The supported weight should be the half of A's weight and the weight it's carrying. 1/2* (128 + 0) = 64. Thus, output should be 64. Please put your code between /* #. Filling your code here */ and /* #. End of code */. Follow the instructions in comments carefully. Do not change the code or structure outside the blocks. Make your own test cases to test the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Larry Ellison Database Genius Of Oracle

Authors: Craig Peters

1st Edition

0766019748, 978-0766019744

More Books

Students also viewed these Databases questions

Question

=+ If your school were to go ahead with such a role model scheme,

Answered: 1 week ago

Question

Distinguish between poor and good positive and neutral messages.

Answered: 1 week ago

Question

Describe the four specific guidelines for using the direct plan.

Answered: 1 week ago