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.)
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 ArrayListfindAllRepresentation(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 ArrayListfindAllRepresentationsWithConstraint(int n, int[] availableWeights) {
}
/**
* * Check if every solution’s weight adds up to totalWeight * @param
* solutions to check * @param totalWeight * @return
*/
public boolean checkCorrectnessAll(ArrayListsolutions, int totalWeight) {
}
/**
* * Print all solutions * @param allSolutions
*/
public void printAllSolutions(ArrayListallSolutions) {
}
}
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
ArrayListunconstrainedSolutions = barbell.findAllRepresentation(weightToRepresent);
System.out.println(barbell.checkCorrectnessAll(unconstrainedSolutions, weightToRepresent));
barbell.printAllSolutions(unconstrainedSolutions);
// Part 2: constrained solutions
int[] numberOfAvailableWeights = {30, 10, 3};
ArrayListconstrainedSolutions = 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]