1 Problem statement

Assume you are working on a gym app where the feature you are trying to implement is: given a weight nd how to represent it using plates available in the gym. For example, 110 lbs could be represented as 2 x (45 + 10) or 2 x (45 +5 + 5) if 10 lbs plates are not available. To be more specic, the problem is, given an array of plate weights (e.g. 5,10,45) and a weight to lift (e.g. 110) nd all possible representations of a weight using plates in the gym. Here, you are also asked to implement it in two dierent ways: 1) where there are innitely many weights of each kind, 2) where there are nite number of weights of each kind ( where there are 3 ve pound plates, 2 ten pounds ones etc.)

2 Implementation

Implement the class BarbellWeights.java.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;

public class BarbellWeights {

private int[] weights;

/**
* * Constructor * @param weights_ are from smallest to the largest
* (example: 1, 3, 10, 25)
*/
public BarbellWeights(int[] weights_) {
}

/**
* * Find all possible representations of ’totalWeight’ using array of
* weights ’weights’. * Each representation (element of the array list),
* denoted as ’r’, * is an array of the same length as ’weights’ such as: *
* weights[0]*r[0] + ... + weights[m]*r[m] = n, where m = weights.length - 1
*
* * @param totalWeight - weight to represent * @return ArrayList of all
* possible representations
*/
public ArrayList findAllRepresentation(int totalWeight) {
}

/**
* * Find all possible representations of ’n’ using array of weights
* ’weights’, * subject to constraint on number of available weights. * Each
* representation, denoted as ’r’, * is an array of the same length as
* ’weights’ such as: * weights[0]*r[0] + ... + weights[m]*r[m] = n, where m
* = weights.length - 1 * and r[0] <= availableWeights[0],..., r[m] <=
* availableWeights[m] * * NOTE: it is possible to get solution by applying
* findAllRepresentation(int totalWeight) * function and then filtering the
* result. It is not allowed - you have to * modify
* findAllRepresentation(int totalWeight) code to return solution * which
* are subject to constraint. * * @param totalWeight - weight to represent *
* @return ArrayList of all possible representations
*/
public ArrayList findAllRepresentationsWithConstraint(int n, int[] availableWeights) {
}

/**
* * Check if every solution’s weight adds up to totalWeight * @param
* solutions to check * @param totalWeight * @return
*/
public boolean checkCorrectnessAll(ArrayList solutions, int totalWeight) {
}

/**
* * Print all solutions * @param allSolutions
*/
public void printAllSolutions(ArrayList allSolutions) {
}
}

3 Sample input-output

Create the le Driver.java whose modied version will be used for grading.

3.1 Input

import java.util.ArrayList;

public class Driver {

public static void main(String[] args) {
int[] weights = {2, 5, 25};
BarbellWeights barbell = new BarbellWeights(weights);
int weightToRepresent = 135;
// Part 1: unconstrained solutions
ArrayList unconstrainedSolutions = barbell.findAllRepresentation(weightToRepresent);
System.out.println(barbell.checkCorrectnessAll(unconstrainedSolutions, weightToRepresent));
barbell.printAllSolutions(unconstrainedSolutions);
// Part 2: constrained solutions
int[] numberOfAvailableWeights = {30, 10, 3};
ArrayList constrainedSolutions = barbell.findAllRepresentationsWithConstraint(weightToRepresent, numberOfAvailableWeights);
System.out.println(barbell.checkCorrectnessAll(constrainedSolutions, weightToRepresent));
barbell.printAllSolutions(constrainedSolutions);
}
}

3.2 Output

true
[65, 1, 0]
[60, 3, 0]
[55, 5, 0]
[50, 7, 0]
[45, 9, 0]
[40, 11, 0]
[35, 13, 0]
[30, 15, 0]
[25, 17, 0]
[20, 19, 0]
[15, 21, 0]
[10, 23, 0]
[5, 25, 0]
[0, 27, 0]
[55, 0, 1]
[50, 2, 1]
[45, 4, 1]
[40, 6, 1]
[35, 8, 1]
[30, 10, 1]
[25, 12, 1]
[20, 14, 1]
[15, 16, 1]
[10, 18, 1]
[5, 20, 1]
[0, 22, 1]
[40, 1, 2]
[35, 3, 2]
[30, 5, 2]
[25, 7, 2]
[20, 9, 2]
[15, 11, 2]
[10, 13, 2]
[5, 15, 2]
[0, 17, 2]
[30, 0, 3]
[25, 2, 3]
[20, 4, 3]
[15, 6, 3]
[10, 8, 3]
[5, 10, 3]
[0, 12, 3]
[15, 1, 4]
[10, 3, 4]
[5, 5, 4]
[0, 7, 4]
[5, 0, 5]
[0, 2, 5]
true
[30, 10, 1]
[30, 5, 2]
[25, 7, 2]
[20, 9, 2]
[30, 0, 3]
[25, 2, 3]
[20, 4, 3]
[15, 6, 3]
[10, 8, 3]
[5, 10, 3]
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.