i did a quick edit so this would compile. I didn't look over the program though to check for any logical errors:
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class BinPacking {
private int _binCount;
private int _numChecks;
private int _bin[];
private ArrayList _manyBins;
private int MAX_SIZE;
private int MAX_ITEMS;
private int _items[];
/**
* Constructor method.
* @param File
* @throws FileNotFoundException
*/
public BinPacking (int[] _tmpItems) throws FileNotFoundException, IOException{
// StringTokenizer st = new StringTokenizer(File);
MAX_SIZE = _tmpItems[0];
_tmpItems[0] = 0;
MAX_ITEMS = _tmpItems[1];
_tmpItems[1] = 0;
int _bin[] = new int[MAX_SIZE];
int _items[] = new int[MAX_ITEMS];
for(int i =0; i < MAX_ITEMS; i++ ){
int j = 2;
_items[i] = _tmpItems[j];
j++;
}
ArrayList _manyBins = new ArrayList();
sort(_items);
packBin(0);
checkWeight();
}
/**
* Sort items into decending order
* @params _items[]
*/
public void sort(int _items[]){
int l = 0;
int r = l+1;
for(int i = l + 1; i <= r; i++){
for(int j = i; j > l; j--){
if(_items[j]< _items[j-1]){
exch(_items,j,j-1);
}
}
}
}
/**
* The exch is where the values of _items is changed around if called upon.
* @param _items, int i, int j
*/
public void exch(int[] _items, int i, int j) {
int t = _items[i];
_items[i] = _items[j];
_items[j] = t;
}
/**
* hasSpace is to determine if the bin has space
* @param int k, int c
* @return boolean
*/
public boolean hasSpace(int k, int c) {
_numChecks++;
System.out.println("\rnumChecks = " + _numChecks);
if(_items[k]>MAX_SIZE){
System.out.print("FAILURE! Element way to large please change and try again! " +_items[k]);
System.exit(0);
}
if(_manyBins.isEmpty() || _manyBins.get(c) == null){
_manyBins.add(_bin[MAX_SIZE]);
_binCount++;
System.out.println("\rbinCount = " +_binCount);
}
//check bin's for space by retriving their value
if(_items[k] > getBinValue(c)){
hasSpace(k, c+1);
return false;
}
return true;
}
/**
* Gets the Bin Value.
* @param i
* @return
*/
public int getBinValue(int i){
_manyBins.get(i);
int binValue=0;
for(int c = 0; c < _bin.length; c++){
binValue+=_bin[c];
}
return binValue;
}
/**
* packBin: this is where if a bin hasSpace then it will put it into other it recursively checks other bins, if non then new bin is created and binCount incremented by one
* @param int k
*/
public void packBin(int k){
int i = 0;
int x= 0;
int j = 0;
if(j<MAX_ITEMS){
if (hasSpace(k, i)){
j++;
_bin[x]=_items[k];
}
packBin(k++);
}
}
/**
* toString this the toString that will print out the end results
*/
/** public void toString(String result){
}
*/
/**
* Check to for weight distrubution
*/
public void checkWeight(){
int i =0;
//find the last entry in the array list and check to see its size if its size is 1/3 of the maximum weight then add to it.
if(getBinValue(_manyBins.size() - 1) <= (int)(MAX_SIZE / 3)){
_manyBins.get(i);
int tmpBin = _bin[_bin.length -1];
if(hasSpace(tmpBin, i)){
_bin[_bin.length - 1] = 0;
_manyBins.get(_manyBins.size() - 1);
_bin[_bin.length] = tmpBin;
i++;
checkWeight();
}
}
}
/**
* Display all the bins and their contents.
*/
public void display(){
for(int i = 0; i<_manyBins.size(); i++){
System.out.print("** Bin["+ i +"]: ");
_manyBins.get(i);
for(int j = 0; j < _bin.length; j++){
System.out.println(" "+_bin[j] +"**");
}
}
System.out.println("Total number of checks : "+ _numChecks);
System.out.println("Total number of bins : "+_binCount);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ArrayList<Integer> tmpItems = new ArrayList<Integer>();
FileReader fr = new FileReader(args[0]);
int i;
while ((i = fr.read()) != -1) {
char c = (char) i;
int k = c - '0';
if (k >= 0 && k < 10)
tmpItems.add(k);
}
int[] _tmpItems = new int[tmpItems.size()];
for (int j =0; j<tmpItems.size(); j++){
//_tmpItems[j] = Integer.parseInt(tmpItems.get(j));
_tmpItems[j] = tmpItems.get(j);
}
fr.close();
// _tmpItems not defined outside try block
BinPacking bp = new BinPacking(_tmpItems);
System.out.println("****************************** ************************************");
System.out.println("Success!!");
bp.display();
System.out.println("****************************** ************************************");
} catch (Exception e) {
System.out.println("Exception: " + e);
}
//moved block
}
}
Bookmarks