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
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="data"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
40 changes: 40 additions & 0 deletions RECITATION.txt
Original file line number Diff line number Diff line change
@@ -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
160 changes: 85 additions & 75 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -1,92 +1,102 @@
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

/**
* Runs a number of algorithms that try to fit files onto disks.
*/
public class Bins {
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<Integer> readData (Scanner input) {
List<Integer> results = new ArrayList<Integer>();
while (input.hasNext()) {
results.add(input.nextInt());
}
return results;
}

/**
* The main program.
*/
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;
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;
}
public static final String DATA_FILE = "example.txt";

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();
/**
* 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<Integer> readData (Scanner input) {
List<Integer> results = new ArrayList<Integer>();
while (input.hasNext()) {
results.add(input.nextInt());
}
return results;
}

public void fitDisksAndPrint(Function< List<Integer>, List<Integer> > function, List<Integer> myList) {
List<Integer> transformedFunction = function.apply(myList);
}

public List<Integer> myReverse (List<Integer> myList ) {
Collections.reverse(myList);
return myList;
}

// Add to collection of disks
public PriorityQueue<Disk> addDisk(List<Integer> myData) {
PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
pq.add(new Disk(0));

Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));
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<Integer> dataIn) {
int total = 0;
for (Integer elem: dataIn) {
total += elem;
}
return total;
}

// Print statistics
public void printStats(PriorityQueue<Disk> 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();
}

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);
}
}
/**
* The main program.
*/
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);
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));
List<Integer> places = new ArrayList<Integer>(
Arrays.asList(1, 2, 3));
b.fitDisksAndPrint((t -> t.stream().sorted().collect(Collections::toList) ), places);

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();
}
}
}