A well-known problem in computer science is called the knapsack problem. A variation is as follows: Given a collection of weights of (possibly) different integral values, is it possible to place some of the weights in a knapsack so as to fill it to some exact total weight? For example, if the weights are 3, 5, 6, and 9, then it is possible for such totals as 3, 8, 11, 14, 17, etc. to be made exactly, but 2, 4, 22, etc. are not possible. Create a Knapsack application that solves this problem. Save & Run Load History Show CodeLens 1 public class Knapsack{ 2 3 /* Returns true if there exists a subset of the items in 4 * weights[start..weights.length] that sum to goal. pre: items in weights[start..weights.length] > 0 post: true has been returned if there exists a subset 7 * of items in weights[start..weights.length] that sum to goal. 8 */ public boolean fillKnapsack (int[] weights, int goal, int start) 10 11 } 12 } 13 LO

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 18SA
icon
Related questions
Question

use Java thank you

A well-known problem in computer science is called the knapsack problem. A variation is as follows:
Given a collection of weights of (possibly) different integral values, is it possible to place some of the
weights in a knapsack so as to fill it to some exact total weight?
For example, if the weights are 3, 5, 6, and 9, then it is possible for such totals as 3, 8, 11, 14, 17,
etc. to be made exactly, but 2, 4, 22, etc. are not possible.
Create a Knapsack application that solves this problem.
Save & Run
Load History
Show CodeLens
1 public class Knapsack{
2
/* Returns true if there exists a subset of the items in
weights [start..weights.length] that sum to goal.
pre: items in weights [start..weights.length] > 0
3
4
5
post: true has been returned if there exists a subset
7
of items in weights [start..weights.length] that sum to goal.
8
*/
9
public boolean fillKnapsack (int [] weights, int goal, int start)
10
11
}
12 }
13
Activity: 1 ActiveCode (Knapsack)
Transcribed Image Text:A well-known problem in computer science is called the knapsack problem. A variation is as follows: Given a collection of weights of (possibly) different integral values, is it possible to place some of the weights in a knapsack so as to fill it to some exact total weight? For example, if the weights are 3, 5, 6, and 9, then it is possible for such totals as 3, 8, 11, 14, 17, etc. to be made exactly, but 2, 4, 22, etc. are not possible. Create a Knapsack application that solves this problem. Save & Run Load History Show CodeLens 1 public class Knapsack{ 2 /* Returns true if there exists a subset of the items in weights [start..weights.length] that sum to goal. pre: items in weights [start..weights.length] > 0 3 4 5 post: true has been returned if there exists a subset 7 of items in weights [start..weights.length] that sum to goal. 8 */ 9 public boolean fillKnapsack (int [] weights, int goal, int start) 10 11 } 12 } 13 Activity: 1 ActiveCode (Knapsack)
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Introduction to computer system
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning