Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions answers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
can18, bwz2, rss44


Readability:
The heuristic code for both heuristics obscures our understanding as it isn’t commented at all. The code to read the input file also obscures understanding as it has a ton of method calls on one line.


Comments in the heuristic code would be helpful to understand what parts of the heuristic are being performed at each point.


The line to read the input file and convert it into a data list (Lines 34 and 35) should be its own method, and not chain of method calls on a single line.


The code for both heuristics is the same and is repeated in the main method of bins. Since we don’t want duplicate code, the heuristic code should be separated out into its own method, and for the second heuristic we pass the method sorted data, instead of the unsorted data used in the first heuristic.


The code to calculate the total size of the files should not be included in this method as it has nothing to do with actually running the heuristic so we would separate that out into its own method as well.


The code for printing the statistics at the end of each heuristic is also duplicated code and can be placed into one method, with the name of the heuristic passed as a parameter.


Also in disk the equals function has unneeded if an return statements that could be removed to create a more concise and clear method.


Testability:




Extensibility:
The bins file as quite a bit of duplicated code. It also has an extremely long main method that encompasses the entire program. It also has message chains to create the scanner object to read the test text files. The disk class also has two constructors that repeat code, which could be refactored to remove duplicate code.


To extend this code as written, one would have to add the heuristics to the main method and could not reuse any of the previously written code. Thus it would take more work to add new heuristics to this code than necessary.
93 changes: 54 additions & 39 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Runs a number of algorithms that try to fit files onto disks.
Expand All @@ -18,55 +21,37 @@ public class Bins {
* @param input tied to an input source that contains space separated numbers
* @return list of the numbers in the order they were read
*/
public List<Integer> readData (Scanner input) {
public List<Integer> readData(Scanner input) {
List<Integer> results = new ArrayList<Integer>();
while (input.hasNext()) {
results.add(input.nextInt());
}
return results;
}

public List<Integer> getData() {
InputStream bStream = Bins.class.getClassLoader().getResourceAsStream(DATA_FILE);
Scanner input = new Scanner(bStream);
List<Integer> data = readData(input);
return data;
}

/**
* The main program.
* Get the total size of all the files.
*
* @param data a list containing the sizes of each file
* @return the total size of all the files.
*/
public static void main (String args[]) {
Bins b = new Bins();
Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE));
List<Integer> data = b.readData(input);

PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
pq.add(new Disk(0));

int diskId = 1;
public int getTotal(List<Integer> data) {
int total = 0;
for (Integer size : data) {
Disk d = pq.peek();
if (d.freeSpace() > size) {
pq.poll();
d.add(size);
pq.add(d);
} else {
Disk d2 = new Disk(diskId);
diskId++;
d2.add(size);
pq.add(d2);
}
total += size;
}
return total;
}

System.out.println("total size = " + total / 1000000.0 + "GB");
System.out.println();
System.out.println("worst-fit method");
System.out.println("number of pq used: " + pq.size());
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
System.out.println();

Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));

diskId = 1;
public void worstFit(PriorityQueue<Disk> pq, List<Integer> data) {
int diskId = 1;
for (Integer size : data) {
Disk d = pq.peek();
if (d.freeSpace() >= size) {
Expand All @@ -80,13 +65,43 @@ public static void main (String args[]) {
pq.add(d2);
}
}
}

System.out.println();
System.out.println("worst-fit decreasing method");
/**
* Do the prints.
*
* @param type is the type of bin packing to use (worst-fit or worst-fit decreasing)
* @param pq is the Priority Queue containing the Disks.
*/
public void prints(String type, PriorityQueue<Disk> pq) {
System.out.println(type);
System.out.println("number of pq used: " + pq.size());
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
System.out.println();
}

public void fitDisksAndPrint(List<Integer> data, Function<List<Integer>, List<Integer>> func) {
List<Integer> transformed = func.apply(data);
int total = getTotal(transformed);
PriorityQueue<Disk> pq = new PriorityQueue<>();
pq.add(new Disk(0));

worstFit(pq, transformed);
System.out.println("total size = " + total / 1000000.0 + "GB");
prints("worst-fit method", pq);
}

/**
* The main program.
*/
public static void main(String args[]) {
Bins b = new Bins();
List<Integer> data = b.getData();
b.fitDisksAndPrint(data, (t -> t.stream().sorted().collect(Collectors.toList())));
b.fitDisksAndPrint(data, (t -> t.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList())));


}
}
16 changes: 4 additions & 12 deletions src/Disk.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ public Disk () {
* Create an empty Disk with the given ID.
*/
public Disk (int id) {
this();
myId = id;
mySize = 0;
myCapacity = 1000000;
myFiles = new ArrayList<Integer>();
}

/**
Expand Down Expand Up @@ -71,14 +69,9 @@ public String toString () {
@Override
public boolean equals (Object other) {
if (other != null && other instanceof Disk) {
if (myId == ((Disk) other).myId) {
return true;
} else {
return false;
}
} else {
return false;
return (myId == ((Disk) other).myId);
}
return false;
}

/**
Expand All @@ -99,8 +92,7 @@ public int compareTo (Disk other) {
} else {
return result;
}
} else {
return -1;
}
return -1;
}
}
File renamed without changes.