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

import java.util.*; import javax.swing.*; import java.io.*; class knapsta { stat

ID: 3550773 • Letter: I

Question

import java.util.*;


import javax.swing.*;


import java.io.*;




class knapsta


{

static int n;


public static List<Integer> snap(int[] arry, int first, int target){


if(first == n){


List<Integer> LIS = new ArrayList<Integer>();


return LIS;


}


if(arry[first] <= target){


List<Integer> Maesti = snap(arry, first+1, target-arry[first]);


Maesti.add(arry[first]);


List<Integer> Maesti2 = snap(arry, first+1, target);


int add1=0, add2=0;


for(int k:Maesti){


add1+=k;


}


for(int k2:Maesti2){


add2+=k2;


}


if(add1 >= add2){


Maesti2.clear();


return Maesti;


}


else{


Maesti.clear();


return Maesti2;


}


}


else{


return snap(arry, first+1, target);


}


}



public static void main(String[] args)


{


String entry = JOptionPane.showInputDialog(" Enter your target followed by the weights ");


    String[] numb = entry.split(" ");


int target = Integer.parseInt(numb[0]);


n = numb.length - 1;


int[] weights_array = new int[26];


for(int i=1; i<=n; i++){


weights_array[i-1] = Integer.parseInt(numb[i]);




}


List<Integer> Maesti = snap(weights_array, 0, target);


for(int k:Maesti){


System.out.print(k+" ");


}


System.out.println();


}


}

======================================================================================

when  you enter the data i want the output to be sorted from largest to smallest. how can i do it?


right now when you enter :  48 45 23 11 8 6 2


i get the answer 6 8 11 23  instead of  23 11 8 6



Explanation / Answer

import java.util.*;


import javax.swing.*;


import java.io.*;




class knapsta


{

static int n;


public static List<Integer> snap(int[] arry, int first, int target){


if(first == n){


List<Integer> LIS = new ArrayList<Integer>();


return LIS;


}


if(arry[first] <= target){


List<Integer> Maesti = snap(arry, first+1, target-arry[first]);


Maesti.add(arry[first]);


List<Integer> Maesti2 = snap(arry, first+1, target);


int add1=0, add2=0;


for(int k:Maesti){


add1+=k;


}


for(int k2:Maesti2){


add2+=k2;


}


if(add1 >= add2){


Maesti2.clear();


return Maesti;


}


else{


Maesti.clear();


return Maesti2;


}


}


else{


return snap(arry, first+1, target);


}


}



public static void main(String[] args)


{

int arr[]=new int[10],t=0,i;

String entry = JOptionPane.showInputDialog(" Enter your target followed by the weights ");


String[] numb = entry.split(" ");


int target = Integer.parseInt(numb[0]);


n = numb.length - 1;


int[] weights_array = new int[26];


for(i=1; i<=n; i++){


weights_array[i-1] = Integer.parseInt(numb[i]);




}


List<Integer> Maesti = snap(weights_array, 0, target);


for(int k:Maesti)

arr[t++]=k;

for(i=t-1;i>=0;i--)

System.out.print(arr[i]+" ");



System.out.println();


}


}