public class Knapsack
extends java.lang.Object
| Modifier and Type | Field | Description |
|---|---|---|
private static java.lang.String |
COMMA |
A comma.
|
private static java.lang.String |
EMPTY_STRING |
Empty string.
|
private static int |
ERROR_CODE |
The number used as a parameter for System.exit() if the program has
to quit early due to difficulties during runtime.
|
private static java.lang.String |
EXIT_PHRASE |
A typed phrase used if the user wants to exit the program rather than
inputting information.
|
private static java.lang.String |
FILE_DOES_NOT_EXIST_MESSAGE |
The message shown when a test file does not exist.
|
private static java.lang.String |
IOEXCEPTION_MESSAGE |
The message shown if the program encounters an IOException when reading String arrays from files
|
private static java.util.Scanner |
keyboard |
A Scanner object which will be used to read user input.
|
private static java.lang.String |
NEW_LINE |
A new-line character.
|
private static java.lang.String |
NO_INTEGERS_MESSAGE |
The message shown when the file does not contain integers in the proper format.
|
private static java.lang.String |
NO_POSSIBILITIES_MESSAGE |
The message shown when there is no possible combination that is under the limit.
|
private static java.io.PrintWriter |
output |
A PrintWriter field to write to the output file, which will be used in the main and other methods.
|
private static java.lang.String |
OUTPUT_NAME |
A field representing the String literal that is the name of the output file.
|
private static java.lang.String |
PRINTWRITER_ERROR_MESSAGE |
The error message used if a PrintWriter throws a FileNotFoundException
|
private static java.lang.String |
SPACE |
A space.
|
private static java.lang.String |
TAB |
A tab.
|
private static java.lang.String |
TESTS_FILE_NOT_FOUND_MESSAGE |
The message used if the file with the names of test files is not found.
|
private static java.lang.String |
UNIT |
Printed after every watermelon weight that gets added to the knapsack
in knapsack.txt.
|
private static java.util.List<java.lang.Integer> |
watermelons |
The list where the chosen objects for a problem are stored.
|
| Constructor | Description |
|---|---|
Knapsack() |
| Modifier and Type | Method | Description |
|---|---|---|
static int |
knapsackSum(int[] w,
int n,
int limit) |
A method that finds the optimal sum of weights that can still fit in
the knapsack recursively.
|
static int |
knapsackSum(int[] w,
int n,
int limit,
java.util.List<java.lang.Integer> list) |
A method that finds the solution to the knapsack problem and remembers
the items used to achieve that maximum weight without going over the limit.
|
static void |
main(java.lang.String[] args) |
|
private static java.util.ArrayList<java.lang.String> |
readFileWithStringArray(java.lang.String inputName) |
An internal method that reads a file and returns its lines as a
String array.
|
private static java.lang.Integer[] |
readItemsFromFile(java.lang.String inputName) |
An internal method that reads the integers within a file.
|
private static void |
writeTestToFile(java.lang.String fileName,
int limit,
int[] w) |
Writes a test class for knapsack to the file "knapsack.txt." It prints
the file name, limit, and weights onto the file then prints the best
combination of watermelons (the one that will attain the most weight
without going over the limit by calling the second knapsack method.
|
private static final java.lang.String OUTPUT_NAME
private static final java.lang.String EXIT_PHRASE
private static final int ERROR_CODE
private static final java.lang.String TESTS_FILE_NOT_FOUND_MESSAGE
private static final java.lang.String PRINTWRITER_ERROR_MESSAGE
private static final java.lang.String IOEXCEPTION_MESSAGE
private static final java.lang.String FILE_DOES_NOT_EXIST_MESSAGE
private static final java.lang.String NO_INTEGERS_MESSAGE
private static final java.lang.String NO_POSSIBILITIES_MESSAGE
private static final java.lang.String UNIT
private static final java.lang.String SPACE
private static final java.lang.String TAB
private static final java.lang.String NEW_LINE
private static final java.lang.String COMMA
private static final java.lang.String EMPTY_STRING
private static java.io.PrintWriter output
private static java.util.List<java.lang.Integer> watermelons
private static java.util.Scanner keyboard
public static void main(java.lang.String[] args)
public static int knapsackSum(int[] w,
int n,
int limit)
w - the list of weightsn - the number of weights in w (this is a precondition)limit - the limit on weight of the knapsackpublic static int knapsackSum(int[] w,
int n,
int limit,
java.util.List<java.lang.Integer> list)
w - the list of weightsn - the number of weights (this is a precondition)limit - the limit on weight of the knapsacklist - the list to add items that are chosen toprivate static java.util.ArrayList<java.lang.String> readFileWithStringArray(java.lang.String inputName)
throws java.io.IOException
inputName - the file to read the String array fromjava.io.IOException - if an error occurs during the Files.readAllLines(path) methodprivate static java.lang.Integer[] readItemsFromFile(java.lang.String inputName)
throws java.io.FileNotFoundException
inputName - the name of the file to read the items fromjava.io.FileNotFoundException - if the file with name inputName is not foundprivate static void writeTestToFile(java.lang.String fileName,
int limit,
int[] w)
fileName - the name of the test filelimit - the limit contained within the designated test filesw - the weights contained within the test files