Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAMMING - RECURSION ONLY Imagine that you have a human pyramid where ea

ID: 3729591 • Letter: J

Question

JAVA PROGRAMMING - RECURSION ONLY

Imagine that you have a human pyramid where each person in a row is balanced on the two people below them, as in the following diagram: Each row has one fewer person than the row above them, and each person carries the weight of themselves plus half the weight carried by the two people standing on them. For example if you have a pyramid of people with the following weights: 140.3 125.3 134.2 180.9 175.6 The person on the top carries only their own weight (111.3). The people in the next row carry their weights (140.3 and 125.3, respectively) and half of the weight of the person on the top (111.3/2 5555), so the total weight carried by the people in the second row is 195.95 and 180.95, respectively. Finally the people in the bottom row carry their own weight, plus half the weight carried by the people standing on them. So the total weights at the bottom row are: 134.2 + (195.95/2) = 232.175, 180.9 (195.95/2 180.95/2) = 369.35, and 175.6 + (180.95/2) = 266.075 Implement the method public static double humanPyramidweight ( ArrayList weights, int level, int offset) That takes a nested ArrayList containing the weights of the people in the pyramid, a level where the top person in the pyramid is at level 0 and each level increases by 1, and an offset, which is the number of people in from the left in a given level This function returns the total weight carried (as described above) by the person in the specified level of the pyramid at the given offset. If the level/offset passed is outside the bounds of the ArrayList, return 0

Explanation / Answer

here is your program : ------------->>>>>>>>>>>

import java.util.ArrayList;

public class Example2{
public static double humanPyramidWeights(ArrayList<ArrayList<Double>> weights,int level,int offset){
  if(level < 0 || level >= weights.size()){
   return 0;
  }
  if(offset < 0 || offset >= weights.get(level).size()){
   return 0;
  }
  if(level == 0 && offset == 0){
   return weights.get(0).get(0);
  }
  double w1 = 0.5*(humanPyramidWeights(weights,level-1,offset-1));
  double w2 = 0.5*(humanPyramidWeights(weights,level-1,offset));
  return weights.get(level).get(offset) + w1 + w2;
}
public static void main(String[] args) {
  ArrayList<ArrayList<Double>> weights = new ArrayList<>();
  ArrayList<Double> data = new ArrayList<>();
  data.add(111.3);
  weights.add(data);
  ArrayList<Double> data1 = new ArrayList<>();
  data1.add(140.3);
  data1.add(125.3);
  weights.add(data1);
  ArrayList<Double> data2 = new ArrayList<>();
  data2.add(134.2);
  data2.add(180.9);
  data2.add(175.6);
  weights.add(data2);
  double weight = humanPyramidWeights(weights,2,1);
  System.out.println(weight);
}
}