Use java: You are given a set of weights to package up for shipment, for example
ID: 3811348 • Letter: U
Question
Use java: You are given a set of weights to package up for shipment, for example: 15, 11, 8, 7, 6, 5. the first weight in the sequence always corresponds to the capacity of the box. Create a method that when passed any sequence in this way, checks all the numbers to find if any add up to the capacity of the box. If a solution is found, print the numbers that add up to the target weight and then continue checking for other possible solutions.
(IT MUST BE A USER ******NOT PROGRAMMER**** DEFINED SEQUENCE OF WEIGHTS) Example: The user enters 15 8 7 11 4 and it would display the sets
Similar program:https://www.chegg.com/homework-help/questions-and-answers/java-programming-given-set-product-weights-box-shipment-example-15-11-8-7-6-5-first-weight-q20062465
Explanation / Answer
package shipment;
/*
* Hope the code fulfills your requirement,feel free to comment for any doubts or
* help,to understand the program well you must be strong in understanding self recursive loops
* All the Best :)
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Shipments {
static void sum_up_recursive(ArrayList<Integer> numbers, int target, ArrayList<Integer> partial) {
int s = 0;
for (int x: partial) s += x;
if (s == target&&s!=0)
System.out.println("sequence("+Arrays.toString(partial.toArray())+")");
if (s >= target)
return;
for(int i=0;i<numbers.size();i++) {
ArrayList<Integer> remaining = new ArrayList<Integer>();
int n = numbers.get(i);
for (int j=i+1; j<numbers.size();j++) remaining.add(numbers.get(j));
ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
partial_rec.add(n);
sum_up_recursive(remaining,target,partial_rec);
}
}
static void sum_up(ArrayList<Integer> numbers, int target) {
sum_up_recursive(numbers,target,new ArrayList<Integer>());
}
public static void main(String args[]) {
// Integer[] numbers = {16,11,8,7,6};
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of weights:");
int noOfWeights = sc.nextInt();
Integer numbers[]=new Integer[noOfWeights];
for(int i=0;i<noOfWeights;i++) {
System.out.println("Enter the weight "+(i+1)+" :");
numbers[i]=sc.nextInt();
}
int target = numbers[0];
Integer newNumbers[]=new Integer[noOfWeights-1];
for(int i=0;i<newNumbers.length;i++)
newNumbers[i]=
numbers[i+1];
System.out.println("ALl the possible combinations:");
sum_up(new ArrayList<Integer>(Arrays.asList(newNumbers)),target);
}
}
//SAMPLE INPUT/OUTPUT
/*
Enter the number of weights:
6
Enter the weight 1 :
15
Enter the weight 2 :
11
Enter the weight 3 :
8
Enter the weight 4 :
7
Enter the weight 5 :
6
Enter the weight 6 :
5
ALl the possible combinations:
sequence([11])
sequence([8])
sequence([8, 7])
sequence([8, 6])
sequence([8, 5])
sequence([7])
sequence([7, 6])
sequence([7, 5])
sequence([6])
sequence([6, 5])
sequence([5])
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.