Question: 6.4 Write a program that solves the knapsack problem for an arbitrary knapsack capacity and series of weights. Assume the weights are stored in an

6.4 Write a program that solves the knapsack problem for an arbitrary knapsack

capacity and series of weights. Assume the weights are stored in an array. Hint: The arguments to the recursive knapsack() function are the target weight and the array index where the remaining items start.

You must USE this Main method, YOU MUST use it right the way it is!!!

public static void main(String[] args) throws IOException { int value; int maxSize = 100; Knapsack theKnap = new Knapsack(maxSize);

while(true)

{

System.out.print("Enter first letter of ");

System.out.print("target, insert, delete, run: ");

int choice = getChar();

switch(choice)

{

case 't':

System.out.print("Enter target weight: ");

value = getInt();

theKnap.setTarget(value);

break;

case 'i':

System.out.print("Enter sequence of item ");

System.out.print("weights, Terminate with 0. ");

while(true)

{

System.out.print("Enter item: ");

value = getInt();

if(value == 0)

break;

theKnap.insertItem( value );

}

break;

case 'd':

theKnap.deleteItem();

break;

case 'r':

theKnap.displayArray();

boolean answer = theKnap.knapsack();

if(answer)

System.out.println(" Success");

else

System.out.println(" Failure");

break;

default:

System.out.print("Invalid entry ");

} // end switch

} // end while

} // end main()

AND you MUST get this output just the way it is in form and content. Please make sure you get this output!!!

Enter first letter of target, insert, delete, run: t

Enter target weight: 20

Enter first letter of target, insert, delete, run: i

Enter sequence of item weights, Terminate with 0.

Enter item: 11

Enter item: 8

Enter item: 7

Enter item: 6

Enter item: 5

Enter item: 0

Enter first letter of target, insert, delete, run: r

Item weights: 11 8 7 6 5

Selected:

Selected: 11

Selected: 11 8

Selected: 11 8 7

Selected: 11 8

Selected: 11 8 6

Selected: 11 8

Selected: 11 8 5

Selected: 11 8

Selected: 11

Selected: 11 7

Selected: 11 7 6

Selected: 11 7

Selected: 11 7 5

Selected: 11 7

Selected: 11

Selected: 11 6

Selected: 11 6 5

Selected: 11 6

Selected: 11

Selected: 11 5

Selected: 11

Selected:

Selected: 8

Selected: 8 7

Selected: 8 7 6

Selected: 8 7

Selected: 8 7 5

**5** **7** **8** Success

Thank you for your help Chegg

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!