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

akm50, csv5, jx35 #4

Open
wants to merge 2 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
50 changes: 50 additions & 0 deletions Answers
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
1. READABILITY
- What pieces of code help versus obscure your understanding of the algorithm?

The comments are vague and don't explain the code's functionality.
The operation that computes the worst fit is confusing.
The variable names are too simple to help readability.

- What comments might be helpful within the code?

A note of the fundamental difference between the two sorting algorithms would help clarify their implementation.
Also, a brief step by step of the most important parts of the code would help.

- Are there places where the code could be more concise and also more clear?

The part that reads the data seems verbose and stores it seems verbose, and a bit confusing to what's going on.
Also, the actual implementation of the worst-fit method is a bit criptic because the algorithm used isn't trivially simple, and might confuse someone who has just picked up the code.


2. TESTABILITY
- How would you test this code for bugs?

Test "extreme" conditions, like seeing what happens when a bin is filled 100%, without leaving any free bytes.
Or, interleaving a lot of small files of different sizes.

- Give a specific example of a "test case" as the code is currently written.

Have a 200.000 size file, and a 800.000 file being added.

- What additional functions may be helpful?

A printing function, that just displays the current state of the bins. Not only it's required for the output, it can be used to navigate the steps of a simple test and check if the code is functioning correctly.

- Give a specific example of a "test case" for your new function.




3. EXTENSIBILITY

- What Code Smells can you find?

- 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?


113 changes: 59 additions & 54 deletions src/Bins.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,72 @@ public List<Integer> readData (Scanner input) {
return results;
}

//print the values a the priority queue.
public void printQ (PriorityQueue pq)
{
System.out.println("number of pq used: " + pq.size());
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
System.out.println();
}

/**
* We observed that the methods of worstFit in order and worstFit decreasing order
* ed the same exact code. The only difference was that the Worst fit decreasing order
* code
*/

public PriorityQueue<Disk> worstFitOverall (List<Integer> allData)
{
PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
pq.add(new Disk(0));
int diskId = 1;
int total = 0;
for (Integer size : allData) {
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();
return pq;
}

public void worstFitInOrder (List<Integer> data)
{
System.out.println("worst-fit method");
printQ(worstFitOverall(data));
}

public void worstFitDecreasing (List<Integer> data)
{
Collections.sort(data, Collections.reverseOrder());
System.out.println("worst-fit decreasing method");
printQ(worstFitOverall(data));
}

/**
* The main program.
*/

// Run worst fit methods both in order and decreasing
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;
}

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();
b.worstFitInOrder(data);
b.worstFitDecreasing(data);
}
}
}