Java Programming Assignment: KnapSack Problem, Assignments of Computer Science

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-1te
koofers-user-1te 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Program 2
Learning Objectives
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
Program 2 Assignment:
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 $10
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).
Sample Output:
pf3

Partial preview of the text

Download Java Programming Assignment: KnapSack Problem and more Assignments Computer Science in PDF only on Docsity!

Program 2

Learning Objectives

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

Program 2 Assignment:

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).

Sample Output:

Test Cases:

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

19 kg 3 1 1 $

What to turn in:

  1. KnapSack.java program printout
  2. Be sure your project is saved in your U:\Program2\ folder on the computer science network.
  3. The assignment is due at the start of Lab 3.

Grading Criteria

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