From bb229e25424bf44cd6d629cbdba02aa3c8a7d393 Mon Sep 17 00:00:00 2001 From: SudoTavo Date: Thu, 14 Jan 2016 17:32:05 -0500 Subject: [PATCH 1/2] Refactored code --- src/Bins.java | 54 +++++++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/src/Bins.java b/src/Bins.java index b313a3d..ee6063f 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -29,47 +29,43 @@ public List readData (Scanner input) { /** * The main program. */ + public static int diskId = 1; + public static int total = 0; + 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; - 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; - } - + worstFit(data, pq); 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()); + pqPrint(pq); + + Collections.sort(data, Collections.reverseOrder()); + pq = worstFit(data, pq); + + System.out.println(); + System.out.println("worst-fit decreasing method"); + pqPrint(pq); + } + + + public static void pqPrint(PriorityQueue pq){ + 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()); + public static PriorityQueue worstFit(List data, PriorityQueue pq ){ pq.add(new Disk(0)); - diskId = 1; for (Integer size : data) { Disk d = pq.peek(); - if (d.freeSpace() >= size) { + if (d.freeSpace() > size) { pq.poll(); d.add(size); pq.add(d); @@ -79,14 +75,8 @@ public static void main (String args[]) { d2.add(size); pq.add(d2); } + total += size; } - - 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(); + return pq; } } From 904c24922981657977f3ddf54935cb5db5900bb2 Mon Sep 17 00:00:00 2001 From: SudoTavo Date: Thu, 14 Jan 2016 17:57:46 -0500 Subject: [PATCH 2/2] Inserted answers to readme --- README.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 README.txt diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..3fa81b2 --- /dev/null +++ b/README.txt @@ -0,0 +1,26 @@ +Readability - +What pieces of code help versus obscure your understanding of the algorithm? + The read data method helped my understanding, while + the the algorithm for sorting the bins obscured my understanding. +What comments might be helpful within the code? + Comments that explained and separated related chucks, such as printing the output, + or manipulating the queue. +Are there places where the code could be more concise and also more clear? + The main method was unnecessary long and unclear +Testability +How would you test this code for bugs? + Running specific text files with known correct outputs, and comparing it to our programs + output. +Give a specific example of a "test case" as the code is currently written. + Using the text file "example.txt" and the program's output +What additional functions may be helpful? + +Give a specific example of a "test case" for your new function. +Extensibility + +What Code Smells can you find? + The main function +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? +What dependencies are there between different parts of the code? +How easy to find are those dependencies? +Can you clarify or remove those dependencies? \ No newline at end of file