

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A java programming assignment where students are required to write a knapsack class to fill a knapsack with bricks of varying weights and values, using integer division and modulus for calculations. Students must also use jgrasp to compile, run, and test their programs.
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


After completing Program 2, students should be able to Be able to write a simple Java application from scratch. Design and write a Java class named KnapSack to solve the Program 2 assignment Use jGRASP to type in, compile, and fix the syntax errors in your program Use jGRASP to run and test your program And know how to Declare and use int variables in a Java program Prompt for and read in an int value entered by the user using keyboard.nextInt() Write arithmetic expressions using integer division and modulus to perform calculations Use output statements in your program to display the results of calculations
You have a knapsack that can contain a specified number of kilograms of weight. You also have an unlimited supply of bricks of a semi-precious metal, of varying weights and value. You want to fill the knapsack so that you are able to carry the maximum dollar value. The bricks have the following weight and dollar values. BRICK: A B C WEIGHT: 5 kg 3 kg 1 kg TOTAL VALUE OF BRICK: $150 $60 $ Prompt the user to enter an integer value for the capacity of the knapsack. Using the / and % integer operators, calculate the number of each size brick to put in the knapsack, so that the knapsack is filled to capacity. Calculate the total value of the bricks in the filled knapsack. Display the results of your calculations, as illustrated below. Hint: The heaviest bricks have the most value in terms of dollars per kg, so begin by putting in as many as possible of the 5 kg brick (brick A), then the 3 kg brick (brick B), and finally the 1 kg brick (brick C).
Hand-Calculated Results Program Output Capacity A (5 kg) B (3 kg) C (1 kg) Total Value A (5 kg) B (3 kg) C (1 kg) Total Value
50 points maximum 20 points - Output Correctness o The program calculates and displays the correct number of bricks and total value for different knapsack capacities. Number of 5 kg bricks is correct (5) Number of 3 kg bricks is correct (5) Number of 1 kg bricks is correct (5) Total value is correct (5) 10 points - Algorithm o Correct use of integer division and modulus in the calculations (10) 10 points - Comments o Complete file header comments with the assignment, your CS account, name, creation date, completion time, and honor code (3) Write " no one " if you received no help on your program solution, otherwise indicate the other