From 2d70297ba57825142abe4608eb8f43c1171bd943 Mon Sep 17 00:00:00 2001 From: Kushal Byatnal Date: Thu, 14 Jan 2016 17:02:42 -0500 Subject: [PATCH 1/3] Refactor object instantiation --- src/Bins.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Bins.java b/src/Bins.java index b313a3d..729f105 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -11,7 +11,17 @@ */ public class Bins { public static final String DATA_FILE = "example.txt"; - + private PriorityQueue pq; + private int total; + private List data; + /** + * Create an empty Bin. + */ + public Bins () { + total = 0; + pq = new PriorityQueue(); + } + /** * Reads list of integer data from the given input. * From f3b93c68f63759d33fd5ae11688d80eec0b904bc Mon Sep 17 00:00:00 2001 From: Kushal Byatnal Date: Thu, 14 Jan 2016 17:53:16 -0500 Subject: [PATCH 2/3] Refactoring --- README.md | 10 ++-- src/Bins.java | 146 +++++++++++++++++++++++++++++++++----------------- src/Disk.java | 6 +-- 3 files changed, 105 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 0103fbf..123548b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # lab_bins -First example code for refactoring +-Refactored the main method into multiple smaller methods +-Changed the structure of the Bins class to better represent data belonging to the class (ie. 'private int') +-Clear method and variable names -Making a change!!! - -Even more changes :) +Net Ids: +krb29 +snp20 diff --git a/src/Bins.java b/src/Bins.java index 729f105..7cb8306 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -13,15 +13,66 @@ public class Bins { public static final String DATA_FILE = "example.txt"; private PriorityQueue pq; private int total; + private int numberOfDisks; private List data; + /** * Create an empty Bin. */ public Bins () { total = 0; + numberOfDisks = 0; pq = new PriorityQueue(); } + /** + * Given a file, returns a list of the data. + */ + public List getDataFromFile(String filePath){ + Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(filePath)); + return readData(input); + } + + /** + * Returns the previously written Disk. + */ + public Disk lastUsedDisk(){ + return pq.peek(); + } + + /** + * Given a disk, adds it to the Bin. + */ + public void addDisk(Disk d){ + pq.add(d); + } + + /** + * Updates an existing disk that has space remaining with the given size. + */ + public void updateExistingDiskInBin(Disk d, int size){ + pq.poll(); + d.add(size); + addDisk(d); + } + + /** + * Create a new disk to hold additional data. + */ + public void addNewDiskToBin(int size){ + Disk d2 = new Disk(numberOfDisks); + d2.add(size); + addDisk(d2); + numberOfDisks++; + } + + /** + * Adds an empty Disk to the Bin. + */ + public void addEmptyDiskToBin(){ + addNewDiskToBin(0); + } + /** * Reads list of integer data from the given input. * @@ -35,68 +86,67 @@ public List readData (Scanner input) { } return results; } - + /** - * The main program. + * Adds the data from the file into the Bin as Disks. */ - 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; + public void addDataToBin(List data){ + //Add disks with the info from the file for (Integer size : data) { - Disk d = pq.peek(); + Disk d = lastUsedDisk(); if (d.freeSpace() > size) { - pq.poll(); - d.add(size); - pq.add(d); + updateExistingDiskInBin(d, size); } else { - Disk d2 = new Disk(diskId); - diskId++; - d2.add(size); - pq.add(d2); + addNewDiskToBin(size); } - total += size; + updateTotal(size); } - - System.out.println("total size = " + total / 1000000.0 + "GB"); + } + + /** + * Updates the total size of the Bin. + */ + public void updateTotal(int size){ + total += size; + } + + /** + * Prints the statistics of the Bin. + */ + public void printStats(){ + 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()) { + } + + /** + * Prints the disks within the Bin. + */ + public void emptyAndPrintDisks(){ + while (!pq.isEmpty()) { System.out.println(pq.poll()); } System.out.println(); + } + + /** + * The main program. + */ + public static void main (String args[]) { + Bins b = new Bins(); + List data = b.getDataFromFile(DATA_FILE); - 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); - } - } + b.addEmptyDiskToBin(); //Adds an empty disk to the bin + b.addDataToBin(data); + b.printStats(); + b.emptyAndPrintDisks(); - 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(); + Collections.sort(data, Collections.reverseOrder()); + + b.addEmptyDiskToBin(); //Adds an empty disk to the bin + b.addDataToBin(data); + b.printStats(); + b.emptyAndPrintDisks(); } } diff --git a/src/Disk.java b/src/Disk.java index e3a3f31..b4e5f09 100644 --- a/src/Disk.java +++ b/src/Disk.java @@ -71,11 +71,7 @@ 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; - } + return (myId == ((Disk) other).myId); } else { return false; } From f1c25f9251613b00ff3822c19d177c51cf11b163 Mon Sep 17 00:00:00 2001 From: Kushal Byatnal Date: Thu, 14 Jan 2016 17:53:55 -0500 Subject: [PATCH 3/3] Update README --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 123548b..0b22370 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # lab_bins --Refactored the main method into multiple smaller methods --Changed the structure of the Bins class to better represent data belonging to the class (ie. 'private int') --Clear method and variable names +-Refactored the main method into multiple smaller methods +-Changed the structure of the Bins class to better represent data belonging to the class (ie. 'private int') +-Clear method and variable names -Net Ids: +Net Ids: -krb29 +krb29 snp20