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
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin1"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ local.properties

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin1/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Making a change!!!

Even more changes :)

Annie, Aamir, Nelson
27 changes: 27 additions & 0 deletions lab_bins.rtf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural

\f0\fs24 \cf0 Annie Tang, Aamir Azhar, Nelson Winrow\
CS 308 - lab_bins\
\
\ul Readability:\ulnone \
- Comments would be helpful in main function of Bins.java. \
- A lot of repeating code in main function when adding Disks to PQ; could be more concise. \
- Comments for methods in Disk.java are very helpful. Comments for reading data in Bins.java are helpful. \
- In Disk.java .compareTo() method, if two disks have an equal amount of free space, it should return 0. However, the method returns the difference of their IDs. \
- Unused code: .equals() method to compare disk IDs, .toString() method in Disks.java\
\
\ul Testability:\
\ulnone - Test larger files that won't fit on a disk / will overfill the disk. \
- Add a test case to the .add() method in Disk.java to first see if the file can fit on the disk before adding it. If it doesn't fit, throw an exception error.\
- A helpful additional function might be to create a function that tests the input data for validity / compatibility with the program (the expected data should have space separated numbers). \
- A test case for said additional function could be any data file that is in a different format than expected. \
\
\
\ul Extensibility:\ulnone \
- Code smells: bloaters (long method), dispensables (duplicate code, dead code) \
- This coder would probably copy and paste a chunk of code in the main method and make some modifications to that (which obviously is not that great) \
- Each method in Disks.java depends on the private attributes myId, mySize, myCapacity, myFiles}
88 changes: 52 additions & 36 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.function.Function;

/**
* Runs a number of algorithms that try to fit files onto disks.
Expand All @@ -26,47 +28,69 @@ public List<Integer> readData (Scanner input) {
return results;
}


public void addtoPQ(){

}
/**
* The main program.
* THE MAIN PROGRAM.
* @throws IOException
*/
public static void main (String args[]) {
public static void main (String args[]) throws IOException {
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>();

//worst-fit decreasing method
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;
}
int total = b.countTotal(data);

allocateDisks(data, pq);

System.out.println("total size = " + total / 1000000.0 + "GB");
System.out.println();
System.out.println("worst-fit method");

b.printTotals(pq, "worst-fit");

//worst-fit decreasing method
Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));

allocateDisks(data, pq);

b.printTotals(pq, "worst-fit decreasing");
}

public void printTotals(PriorityQueue<Disk> pq, String description){
PriorityQueue<Disk> copy = new PriorityQueue<Disk>(pq); //makes shallow copy of pq so is not destroyed
//shallow because both still reference same structure in memory, but pointers for copy are destroyed
//this copy doesn't actually make new copy of the data itself
System.out.println();
System.out.println(description + "method");
System.out.println("number of pq used: " + pq.size());
while (!pq.isEmpty()) {
System.out.println(pq.poll());
while (!copy.isEmpty()) {
System.out.println(copy.poll());
}
System.out.println();
System.out.println();
}

Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));
private int countTotal(List<Integer> data) {
int total = 0;
for (Integer size : data) {
total += size;
}
return total;
}

public List<Integer> fitDisksAndPrint(List<Integer> list, Function<List<Integer>, List<Integer>> func){
List<Integer> transformed = func.apply(list);
return transformed;

}

diskId = 1;
private static void allocateDisks(List<Integer> data, PriorityQueue<Disk> pq) throws IOException {
int diskId= 1;
for (Integer size : data) {
Disk d = pq.peek();
if (d.freeSpace() >= size) {
Expand All @@ -80,13 +104,5 @@ public static void main (String args[]) {
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();
}
}
}
18 changes: 10 additions & 8 deletions src/Disk.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -43,9 +44,14 @@ public int freeSpace () {
*
* @param filesize size of file to add to this disk
*/
public void add (int filesize) {
myFiles.add(filesize);
mySize += filesize;
public void add (int filesize) throws IOException {
if(myCapacity - mySize - filesize >= 0){
myFiles.add(filesize);
mySize += filesize;
}
else{
throw new IOException("no space, sorry");
}
}

/**
Expand Down Expand Up @@ -94,11 +100,7 @@ public boolean equals (Object other) {
public int compareTo (Disk other) {
if (other != null) {
int result = other.freeSpace() - freeSpace();
if (result == 0) {
return myId - other.myId;
} else {
return result;
}
return result; //CHANGE
} else {
return -1;
}
Expand Down