Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

6. Vanguard

Answered: 1 week ago

Question

1. PricewaterhouseCoopers

Answered: 1 week ago