From cdaf155e5e8162c9ccc6f090422c0efeaef6a84e Mon Sep 17 00:00:00 2001 From: Briguy52 Date: Thu, 14 Jan 2016 16:44:55 -0500 Subject: [PATCH 1/3] Made small changes --- .classpath | 6 ++++++ RECITATION.txt | 0 src/Bins.java | 2 ++ 3 files changed, 8 insertions(+) create mode 100644 .classpath create mode 100644 RECITATION.txt diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/RECITATION.txt b/RECITATION.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/Bins.java b/src/Bins.java index b313a3d..31ae731 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -6,6 +6,8 @@ import java.util.PriorityQueue; import java.util.Scanner; +// small change + /** * Runs a number of algorithms that try to fit files onto disks. */ From b89f51e8f7cd8c78871209ac4f723963157e4fff Mon Sep 17 00:00:00 2001 From: Briguy52 Date: Thu, 14 Jan 2016 17:48:19 -0500 Subject: [PATCH 2/3] Refactored lab_bins --- .classpath | 1 + RECITATION.txt | 40 ++++++++++++++ src/Bins.java | 145 +++++++++++++++++++++++-------------------------- 3 files changed, 110 insertions(+), 76 deletions(-) diff --git a/.classpath b/.classpath index fb50116..d478271 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,7 @@ + diff --git a/RECITATION.txt b/RECITATION.txt index e69de29..89bf202 100644 --- a/RECITATION.txt +++ b/RECITATION.txt @@ -0,0 +1,40 @@ +Bins: Code Critiquen +Brian Lin bl131 +Austin Hua ah335 + +Readability + +What pieces of code help versus obscure your understanding of the algorithm? +-The Disk methods are all relatively short, simple, and aptly titled. These help us understand the algorithm. +-The Bins main method is very long and uncommented. This makes the algorithm hard to understand. +What comments might be helpful within the code? +Are there places where the code could be more concise and also more clear? +-The Bins main method could be improved with comments +-The Bins main method should also be shortened or split into their own methods based on function + +Testability +How would you test this code for bugs? +-Run the program with test cases, including specific edge cases +Give a specific example of a "test case" as the code is currently written. +-Text file containing: 1000001, 99999 +What additional functions may be helpful? +-An 'add to bin/disk' function that loops over the scanned input +Give a specific example of a "test case" for your new function. +-Text file containing: 500001, 500002, 500003 + + +Extensibility + +What Code Smells can you find? +-Redundancy, the print statements in the Bins main method are written twice and can instead be placed in their own method +-Change preventers, the duplicate print statements would force you to modify two lines for a single desired change +-The for loops are also the same and a function with a for loop could be used instead. +What suggestions does this code make about how someone would extend it in the future to compare the performance of a wider variety of fitting algorithms? +-Currently, in order to add more fitting algorithms, you would have to manually type in code within the main method. Instead, you could have the main method take in separate Java files corresponding to different fitting algorithms. +What dependencies are there between different parts of the code? +-You are forced to run both the in-order and decreasing methods one after the other +-The Disk.java class is required for Bins.java to run +How easy to find are those dependencies? +-Difficult, you have to visually parse the code for instantiation of other classes +Can you clarify or remove those dependencies? +-These two different orders should be placed in separate methods to run independently \ No newline at end of file diff --git a/src/Bins.java b/src/Bins.java index 31ae731..bc37f50 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -12,83 +12,76 @@ * Runs a number of algorithms that try to fit files onto disks. */ public class Bins { - public static final String DATA_FILE = "example.txt"; + public static final String DATA_FILE = "example.txt"; - /** - * Reads list of integer data from the given input. - * - * @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) { - List results = new ArrayList(); - while (input.hasNext()) { - results.add(input.nextInt()); - } - return results; - } + /** + * Reads list of integer data from the given input. + * + * @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) { + List results = new ArrayList(); + while (input.hasNext()) { + results.add(input.nextInt()); + } + return results; + } + + // Add to collection of disks + public PriorityQueue addDisk(List myData) { + PriorityQueue pq = new PriorityQueue(); + pq.add(new Disk(0)); - /** - * The main program. - */ - 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); + int diskId = 1; + for (Integer size : myData) { + 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); + } + } + return pq; + } + + // Returns total data size of files + public int getTotalData(List dataIn) { + int total = 0; + for (Integer elem: dataIn) { + total += elem; + } + return total; + } + + // Print statistics + public void printStats(PriorityQueue myPQ) { + System.out.println(); + System.out.println("worst-fit method"); + System.out.println("number of pq used: " + myPQ.size()); + while (!myPQ.isEmpty()) { + System.out.println(myPQ.poll()); + } + System.out.println(); + } - PriorityQueue pq = new PriorityQueue(); - pq.add(new Disk(0)); - - int diskId = 1; - 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; - } - - 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; - 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); - } - } - - System.out.println(); - System.out.println("worst-fit decreasing method"); - System.out.println("number of pq used: " + pq.size()); - while (!pq.isEmpty()) { - System.out.println(pq.poll()); - } - System.out.println(); - } + /** + * The main program. + */ + 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); + System.out.println("total size = " + b.getTotalData(data) / 1000000.0 + "GB\n"); + // In-order + b.printStats(b.addDisk(data)); + // Decreasing order + Collections.sort(data, Collections.reverseOrder()); + b.printStats(b.addDisk(data)); + } } From 8674db8dbdbcf91e2e21f536074e5c813cd7fe6e Mon Sep 17 00:00:00 2001 From: Briguy52 Date: Thu, 3 Mar 2016 19:06:59 -0500 Subject: [PATCH 3/3] Used Stream to sort and collect list --- src/Bins.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Bins.java b/src/Bins.java index bc37f50..718431f 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -1,10 +1,12 @@ import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; +import java.util.function.Function; // small change @@ -28,6 +30,15 @@ public List readData (Scanner input) { return results; } + public void fitDisksAndPrint(Function< List, List > function, List myList) { + List transformedFunction = function.apply(myList); + } + + public List myReverse (List myList ) { + Collections.reverse(myList); + return myList; + } + // Add to collection of disks public PriorityQueue addDisk(List myData) { PriorityQueue pq = new PriorityQueue(); @@ -83,5 +94,9 @@ public static void main (String args[]) { // Decreasing order Collections.sort(data, Collections.reverseOrder()); b.printStats(b.addDisk(data)); + List places = new ArrayList( + Arrays.asList(1, 2, 3)); + b.fitDisksAndPrint((t -> t.stream().sorted().collect(Collections::toList) ), places); + } }