program code to copy
DPOptimization.java
import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
public class DPOptimization
{
public static void main(String[] args)
{
// Terminal Input Setup
if (args.length > 0)
{
File fileName = new File(args[0]);
int numHrs = 0; int numProbs = 0;
ArrayList<Integer> hrs = new ArrayList<Integer>();
ArrayList<Integer> pts = new ArrayList<Integer>();
// Process input
readFile(fileName, numHrs, numProbs, hrs, pts);
}
else
{
System.err.println("Input file not specified!, try again");
}
}
// READS INPUT FILE AND EXECUTES COMMANDS //
public static void readFile(File fileName, int numHrs, int numProbs, ArrayList<Integer> hrs, ArrayList<Integer> pts)
{
try
{
int k; int counter = 1;
String line;
Scanner scan = new Scanner(fileName);
// Get # of problems and # of hours from input file
line = scan.nextLine();
numProbs = Integer.parseInt(line);
line = scan.nextLine();
numHrs = Integer.parseInt(line);
System.out.println(fileName + " has " + numProbs + " problems over " + numHrs + " hours");
System.out.println("Pr#\tTime\tPoints");
// Read the rest of the lines in file
while (scan.hasNextLine())
{
line = scan.nextLine();
String[] command = line.split(" ");
if (command.length > 0)
{
System.out.println(counter + "\t" + command[0] + "\t" + command[2]);
hrs.add(Integer.parseInt(command[0]));
pts.add(Integer.parseInt(command[2]));
counter++;
}
}
// Execute DP process
k = genTable(numProbs, numHrs, hrs, pts);
}
catch (Exception e)
{
e.printStackTrace();
}
}
// HELPER MAX FUNCTION //
public static int findMax(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
// SCHEDULE LOGIC //
//---------------//
// DP SOLUTION // 0-1 Knapsack Problem
public static int genTable(int numPrbs, int numHrs, ArrayList<Integer> hrs, ArrayList<Integer> pts)
{
// Serves as the DP table
int[][] k = new int[numPrbs + 1][numHrs + 1];
// Knapsask
int sack[] = new int[numPrbs];
// Building DP table from the bottom up
for (int i = 0; i <= numPrbs; i++)
{
for (int w = 0; w <= numHrs; w++)
{
// Base Case - Initialization
if (i == 0 || w == 0)
{
k[i][w] = 0;
// Current problem does not exceed max hours
}
else if (hrs.get(i-1) <= w)
{
k[i][w] = findMax(pts.get(i-1) + k[i-1][w - hrs.get(i-1)], k[i-1][w]);
}
else
{
k[i][w] = k[i-1][w];
}
}
}
// Traverse back and collect items in knapsack
int temp1 = numPrbs; int temp2 = numHrs; int sackIndex = 0;
while (numPrbs > 0)
{
// Detects when it should account for a problem in the sack
if (k[numPrbs][numHrs] != k[numPrbs-1][numHrs]) {
sack[sackIndex] = numPrbs;
sackIndex++; numPrbs--;
numHrs = numHrs - hrs.get(numPrbs);
} else {
numPrbs--;
}
}
// Output optimal items
System.out.println("\nThe selected problems for the highest contest score are: ");
System.out.println("\tProblem\tPoints");
for (int x = sackIndex - 1; x >= 0; x--)
{
System.out.println("\t" + sack[x] + "\t" + pts.get((int)sack[x]-1));
}
System.out.println("\nTarget problem list yields " + k[temp1][temp2] + " points");
return k[temp1][temp2];
}
}
===================================================================
contestA.in
4
10
2 4
3 5
4 6
6 7
sample output
1 Objective The rules for a new type of programming contest provides a list of problems,...
1 Objective Build a hashing algorithm that is suitable for use in a Bloom Filter. Please note that while a cryptographic hash is quite common in many Bloom Filters, the hashing algorithm to be implemented is a mix of the the following algorithmic models, specifically, a multiply & rotate hash colloquially known as a murmur hash, and an AND, rolale, & XOR hash colloquially known as an ARX hash. 2 Requirements • Inputs. Read the input file which contains strings...
Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...
4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...
Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1. The input and variable value file are both text files that will be specified in...
Main objective: Solve the n queens problems. You have to place n queens on an n × n chessboard such that no two attack each other. Important: the chessboard should be indexed starting from 1, in standard (x, y) coordinates. Thus, (4, 3) refers to the square in the 4th column and 3rd row. We have a slight twist in this assignment. We will take as input the position of one queen, and have to generate a solution with a...
Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...
You are to simulate a dispatcher using a priority queue system. New processes are to be entered using a GUI with priority included (numbering should be automatic). Processes are also to be terminated by GUI command. Context switches are to be by command with the cause of the switch being immaterial. Assume only one CPU. Priorities and numbers of processes can be kept small, just big enough to demonstrate the below listed functionality. You may pre-populate the queues initially from...
Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...
//I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...
Project 4 simulation with conway's rules for life - revisited due before 4/30 11:59pm, autograded by project 4 zylab, limited to 10 submissions. We will be using the concepts from project 2 to test the more recent concepts of linked lists, pointers, and file I/O. The initial data points will be stored in a file (specification of the layout of the file below), each grid of values will be dynamically allocated and then referenced via a pointer in a linked...