Question
How can I got about implementing first fit and next fit bin packing methods in Java? This is the provided code: ************************************************************************ package BinPacking; import
How can I got about implementing first fit and next fit bin packing methods in Java? This is the provided code:
************************************************************************
package BinPacking; import java.util.ArrayList; import java.util.*;
public class BinPacking {
int capacity; int itemsToPack[] = new int[100]; int binNumber; int totalNumBins; public static void main(String args[]) { ArrayList vect = new ArrayList<>(); int pos = 0; int number = 0; int count = 1; int upperBound = 0; Scanner sc= new Scanner(System.in); System.out.println("How many items are to be packed? "); number = sc.nextInt(); System.out.println("What is the capacity? "); upperBound = sc.nextInt(); while (count <= number) {
pos = getRandomValue(upperBound); vect.add(pos); count++; } System.out.println("-----------------------------"); System.out.println(" items to pack: "); print(vect);
} public static int getRandomValue(int ub)
{
Random r = new Random(); int element = 0; int upperBound = ub; int lowerBound = 1; element = Math.abs(r.nextInt()) % upperBound + lowerBound; return element;
}
public static void print(ArrayList a)
{
for (int i = 0; i < a.size(); ++i)
System.out.print(a.get(i) + " "); System.out.println(" -----------------------------");
}
}
********************************************************************************
Currently, the output looks something like
==================================
How many items are to be packed? 8 What is the capacity? 10 -----------------------------
items to pack: 5 10 1 3 3 8 3 7 -----------------------------
===================================
What needs to be added are methods for sorting in a first fit and next fit matter.
First fit example:
===================================
Bin 1: 5
Bin 2: 10
Bin 1: 1
Bin 1: 3
Bin 3: 3
Bin 4: 8
Bin 3: 3
Bin 5: 7
===================================
Next fit example:
===================================
Bin 1: 5
Bin 2: 10
Bin 3: 1
Bin 3: 3
Bin 3: 3
Bin 4: 8
Bin 5: 3
Bin 5: 7
===================================
The numbers that are to be sorted are random every time. How can I go about implementing methods for these sorting formats?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started