Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am writing a java program. I don't know a lot of imports. Only joptionapane, io, scanner. The Knapsack Problem: A hiker has a

Hello, I am writing a java program. I don't know a lot of imports. Only joptionapane, io, scanner.

image text in transcribed

image text in transcribed

The Knapsack Problem: A hiker has a knapsack which will hold a total of N pounds. The hiker has a number of objects weighing w1 pounds, w2 pounds, ., wr pounds which he wishes to take with him. He wants to fill the knapsack as full as possible, that is, to pack it with objects having the maximum total weight. You will implement a program to help him figure this out. To make this problenm simpler, we fix the number of objects to be 4. Follow the steps below in your program flow. It is required that vou use the for loop for this exercise, 1. Prompt the user and input the maximum number of pounds that the hiker can carry 2. Prompt the user and input the weights of the 4 objects that the hiker can choose from to take with him 3. Find the best solution, and print which objects should be taken by the hiker. (Hint: you will need to use a series of nested loops). The best solution is the combination with the highest total that does not exceed the maximum that the hiker can carry. The name of your class should be KnapsackProblem To help you test your code, here are some test cases: Max Weight 24: 5,7, 13, 17 Best Combo: 7, 17 Max Weight 25: 5, 10, 13,9Best Combo: 5, 10, 9 Max Weight 18: 4,7, 9, 13Best Combo: 4, 13 The solution to this problem will require 4 nested for loops. HINT: consider the output of the following code. what does the value of i and j signify? Think about how this could be extended to four numbers? Try to extend this to 4 nested loops, with the indices going onlv up to 2. What is the significance of the 0 in the print out

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

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions