diff --git a/answers.txt b/answers.txt new file mode 100644 index 0000000..4c047e5 --- /dev/null +++ b/answers.txt @@ -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. \ No newline at end of file diff --git a/src/Bins.java b/src/Bins.java index b313a3d..b8d6289 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -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. @@ -18,7 +21,7 @@ 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 readData (Scanner input) { + public List readData(Scanner input) { List results = new ArrayList(); while (input.hasNext()) { results.add(input.nextInt()); @@ -26,47 +29,29 @@ public List readData (Scanner input) { return results; } + public List getData() { + InputStream bStream = Bins.class.getClassLoader().getResourceAsStream(DATA_FILE); + Scanner input = new Scanner(bStream); + List 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 data = b.readData(input); - - PriorityQueue pq = new PriorityQueue(); - pq.add(new Disk(0)); - - int diskId = 1; + public int getTotal(List 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 pq, List data) { + int diskId = 1; for (Integer size : data) { Disk d = pq.peek(); if (d.freeSpace() >= size) { @@ -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 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 data, Function, List> func) { + List transformed = func.apply(data); + int total = getTotal(transformed); + PriorityQueue 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 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()))); + + + } } diff --git a/src/Disk.java b/src/Disk.java index e3a3f31..c0f5fec 100644 --- a/src/Disk.java +++ b/src/Disk.java @@ -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(); } /** @@ -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; } /** @@ -99,8 +92,7 @@ public int compareTo (Disk other) { } else { return result; } - } else { - return -1; } + return -1; } } diff --git a/data/example.txt b/src/example.txt similarity index 100% rename from data/example.txt rename to src/example.txt