Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor lab, 3/3/2016 #44

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
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>
90 changes: 45 additions & 45 deletions src/Bins.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* 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 = "princeton.txt";
/**
* Reads list of integer data from the given input.
*
Expand All @@ -25,19 +27,10 @@ public List<Integer> readData (Scanner input) {
}
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;

public static int worstFit(PriorityQueue<Disk> pq, List<Integer> data){
pq.add(new Disk(0));
int diskId = 1;
int total = 0;
for (Integer size : data) {
Disk d = pq.peek();
Expand All @@ -53,40 +46,47 @@ public static void main (String args[]) {
}
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());
return total;
}

public static int worstFitDecreasing(PriorityQueue<Disk> pq, List<Integer> data){
pq.add(new Disk(0));
Collections.sort(data, Collections.reverseOrder());
return worstFit(pq,data);
}

public static void printStats(PriorityQueue<Disk> pq, int total){
System.out.println("total size = " + total / 1000000.0 + "GB");
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);
}
}

}

public static void fitDisksinBins(List<Integer> myList, Function<List<Integer>, List<Integer>> myFunction){
List<Integer> newList = myFunction.apply(myList);
newList.forEach(e -> System.out.println(e));
}

/**
* 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();
fitDisksinBins(data, (f) -> f.stream().sorted().collect(Collectors.toList()));

PriorityQueue<Disk> pq = new PriorityQueue<Disk>();

System.out.println("worst-fit in-order method");
int total = worstFit(pq, data);
printStats(pq, total);

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();
total = worstFitDecreasing(pq, data);
printStats(pq, total);
}
}
6 changes: 2 additions & 4 deletions src/Disk.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public class Disk implements Comparable<Disk> {
* Create an empty Disk.
*/
public Disk () {
mySize = 0;
myCapacity = 1000000;
myFiles = new ArrayList<Integer>();
this(0);
}

/**
Expand Down Expand Up @@ -90,7 +88,7 @@ public boolean equals (Object other) {
* @return positive if this disk is greater than the given disk, zero if they are equal, and
* negative if this disk is less than the given disk
*/
@Override

public int compareTo (Disk other) {
if (other != null) {
int result = other.freeSpace() - freeSpace();
Expand Down
43 changes: 43 additions & 0 deletions src/Lab Answers
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
What pieces of code help versus obscure your understanding of the algorithm?
Helps: Print lines that show what data is being printed
Obscure: the entire code is in the main function so it's hard to tell what the sequence and overall functionality of the program is doing

What comments might be helpful within the code?
Steps, different heuristics (i.e. when the code is doing the in-order versus decreasing-order algorithms), describing conditions for if statements

Are there places where the code could be more concise and also more clear?
Yes, the print statements are repetitive, the two heuristics are carried out in the same function when they should be separated

Testability
How would you test this code for bugs?
Run it on different text files and see if it works. Run it on test files with very simple numbers and manually compare
the answer from the program with something done by hand.

Give a specific example of a "test case" as the code is currently written.
The test case is the example.txt file, which is coded into the Bins.java file already.

What additional functions may be helpful?
A consistent print function, a function that reverse orders the list of numbers

Give a specific example of a "test case" for your new function.
We just use the same data

Extensibility
What Code Smells can you find?
1. "The Bloaters". The entire functinality of "Bins" is contained in the main method of the bins.java file.

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?
They would add to the main method another variation of the algorithm that would potentially do something with Disk, then add
that the contents of that list to the priority queue, then print out information about the end result of the algorithm.
This would follow the pattern established above.


What dependencies are there between different parts of the code?
Bins depends on Disk as it creates instances of Disk. Several functions depending on data

How easy to find are those dependencies?
It's not super easy to find.

Can you clarify or remove those dependencies?
Yes, pass data through functions and reuse them to reduce dependencies.