In: Computer Science
Next Day Cargo Company needs an integrated application with
their system that sorts the cargo with respect to their weight and
distributes the weight evenly (if possible) between the given
vehicles. Note that each vehicle has a weight limit, and cannot
exceed it. But each vehicle can be used more than once during these
operations. And do not forget, they want a fast solution because
waiting a long time for the algorithm to complete reports will risk
their motto, which is “Next Day Cargo”. They require a report which
will be used as a work order for workers and truck drivers as it
shows the order of load for each vehicle. The report/order will
have the following four columns: Number (which will be the loading
sequence), Barcode Number, Weight, Postal Code. Our team has worked
on API architecture and concluded in the solution given in Figure
2.4. In this API there are two main steps, which are explained as
follows; 1. Sort the shipments: We are planning to use merge sort
algorithm. Sorting must be from heaviest to lightest. 2. Distribute
the sorted shipments to vehicles: To distribute shipments to
available vehicles there are some restriction/request as follows; o
There is a list of available vehicles, in which a vehicle can be
used more than once if needed. o Total weight of assigned shipments
to a vehicle cannot exceed its weight capacity. o Order of
assignments must be from heaviest to lightest which will be used as
a loading order. For the distribution of cargo to vehicles the
following steps are considered; • Take each cargo in the given
order, • Take a vehicle and calculate the total weight of the taken
cargo assigned to the taken vehicle, • If the capacity of the
vehicle is not exceed, assign the cargo to the vehicle, • If not,
select another vehicle until a suitable vehicle is found. Vehicle
and Shipment classes are completed in the API.LoadsOfVehicle and
Sort classes need implementations. A sample report is provided in
the file SampleReport.txt.
In order to sort the shipments by weight, you will need to
implement the sortShipmentsByWeight method in the Sort.java file.
To implement the sortShipmentsByWeight you will need to implement
the following two methods mergeSortShipmentsByWeight and merge
method. mergeSortShipmentsByWeight will take three arguments, an
array of shipments, a start index and an end index. This method
should recursively call itself until the shipment has been sorted.
The merge method should merge two halves of the sorted shipment.
Develop the sortShipmentsByWeight() method in the Sort.java file to
sort cargo from heaviest to lightest. The shipments now need to be
distributed evenly among the vehicles. Each vehicle can do multiple
transfers and can be used more than once if needed, but the weight
limit of each vehicle cannot be exceeded. The first method to
implement is distributeShipmentsToVehicles which will return the
maximum weight per load for each vehicle. Once the maximum capacity
per vehicle has been calculated, the shipments need to be loaded
onto the vehicle using the loadTheVehicles method. Develop the
distributeShipmentsToVehicles method to evenly distribute weight
between transportation vehicles but do not exceed the weight limit
of each vehicle. (Each vehicle can do multiple transfers/can be
used more than once if needed) Implment the loadTheVehicles method
to load the shipments into the vehicles, the total capacity of the
vehicles must be greater than the total weight of the shipment.
Implement the getLoadReport method in the LoadsOfVehicle.java file
to generate a report. Since the load reports will be read by humans
and computers alike, it is important that the report format matches
the sample report, including spaces.
LoadsOfVehicles.java
import java.util.ArrayList;
/**
* @author
*
*/
public class LoadsOfVehicle {
private Vehicle vehicle;
private ArrayList<Shipment> cargoList;
public LoadsOfVehicle(Vehicle vehicle) {
super();
this.vehicle = vehicle;
this.cargoList = new ArrayList<>();
}
public ArrayList<Shipment> getCargoList() {
return cargoList;
}
public void addCargo(Shipment shipment) {
cargoList.add(shipment);
}
public double getLoadedWeight() {
double totalWeight = 0;
for (int i = 0; i < cargoList.size(); i++) {
totalWeight += cargoList.get(i).getWeight();
}
return totalWeight;
}
public double getVehicleCapacity() {
return vehicle.getWeightCapacity();
}
public String getVehiclePlate() {
return vehicle.getPlate();
}
public String getVehicleDriver() {
return vehicle.getDriverName();
}
/**This methods generates report for the vehicle and the
* the shipments in it wrt weight (from heaviest to lightest)
*
* @return generated report
*/
public String getLoadReport() {
return null;
}
/**This method takes vehicles and their loadings as input and
* generates report for each vehicle
*
* @param loadsOfVehicles vehicle and loadings array
* @return a report for each vehicle showing loading sequence
*/
public static String[] getReports(LoadsOfVehicle[] loadsOfVehicles)
{
return null;
}
}
SampleReport.txt
Vehicle Plate : KZ66 ZYT
Weight Limit : 10.00
Driver : John Locke
Loading Sequence Barcode Number Weight Delivery Address Postal
Code
---------------- -------------- ------
----------------------------
1 2147253625 2.000 34547
2 2147043627 0.750 34543
---------------- -------------- ------
----------------------------
2 -------------- 2.750 ----------------------------
Vehicle Plate : SBG 984
Weight Limit : 20.00
Driver : Down Brown
Loading Sequence Barcode Number Weight Delivery Address Postal
Code
---------------- -------------- ------
----------------------------
1 2147013624 20.000 34546
---------------- -------------- ------
----------------------------
1 -------------- 20.000 ----------------------------
Vehicle Plate : EKI6 LLO
Weight Limit : 50.00
Driver : Adam Smith
Loading Sequence Barcode Number Weight Delivery Address Postal
Code
---------------- -------------- ------
----------------------------
1 2147313623 15.000 34543
2 2147013628 1.250 34543
3 2147014626 0.500 34542
---------------- -------------- ------
----------------------------
3 -------------- 16.750 ----------------------------
Shipment.java
public class Shipment {
private double weight;
private long barcodeNumber;
private int fromPostalCode;
private int toPostalCode;
public Shipment(double weight, long barcodeNumber, int
fromPostalCode, int toPostalCode) {
super();
this.weight = weight;
this.barcodeNumber = barcodeNumber;
this.fromPostalCode = fromPostalCode;
this.toPostalCode = toPostalCode;
}
public int getFromPostalCode() {
return fromPostalCode;
}
public double getWeight() {
return weight;
}
public long getBarcodeNumber() {
return barcodeNumber;
}
public int getToPostalCode() {
return toPostalCode;
}
}
Sort.java
public class Sort {
/**
* @param shipments
* @param vehicles
* @return
*/
public static LoadsOfVehicle[] getLoadingArray(Shipment[]
shipments, Vehicle[] vehicles) {
return null;
}
/**
* @param shipmentsToSort
*/
public static void sortShipmentsByWeight(Shipment[]
shipmentsToSort) {
}
/**
* @param shipmentsToSort
* @param start
* @param end
*/
private static void mergeSortShipmentsByWeight(Shipment[]
shipmentsToSort, int start, int end) {
}
/**
* @param array
* @param start
* @param middle
* @param end
*/
private static void merge(Shipment[] array, int start, int middle,
int end) {
}
/**
* @param sortedShipments
* @param vehicles
* @return
*/
public static LoadsOfVehicle[] distributeShipmentsToVehicles(
Shipment[] sortedShipments, Vehicle[] vehicles) {
//calculate total weight of shipments
//calculate total weight capacity of vehicles
//if capacity of vehicles is not enough add more turns for
vehicles
//define return array with extra vehicle turns
//use modulus to add vehicles more than once if needed
//distribute weight to vehicles
return null;
}
/**
* @param vehicleListToLoad
* @param sortedShipments
*/
public static void loadTheVehicles(LoadsOfVehicle[]
vehicleListToLoad, Shipment[] sortedShipments) {
//while there is more cargo to load
//distribute weight over vehicles
//calculate total weight if current weight is loaded
//there is a cargo to load and vehicle has the capacity
//load cargo to vehicle
//If no more cargo to load break while loop
}
public static void main(String[] args) {
/*
* This main method is a stub.
* It does nothing.
* Feel free to write your own code to test your
implementation.
* In this case, we have nothing actionable in here, just this
comment block, so the JVM should rapidly lose interest and move on
to the rest of your code.
*/
}
}
Vehicle.java
public class Vehicle {
private double weightCapacity;
private String plate;
private String driverName;
public Vehicle(double weightCapacity, String plate, String
driverName) {
super();
this.weightCapacity = weightCapacity;
this.plate = plate;
this.driverName = driverName;
}
public double getWeightCapacity() {
return weightCapacity;
}
public String getPlate() {
return plate;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
}
import java.util.ArrayList;
import com.m.*;
public class Sort {
/**
* @param shipments
* @param vehicles
* @return
*/
public static LoadsOfVehicle[] getLoadingArray(final Shipment[] shipments, final Vehicle[] vehicles) {
return null;
}
/**
* @param shipmentsToSort
*/
public static void sortShipmentsByWeight(final Shipment[] shipmentsToSort) {
}
/**
* @param shipmentsToSort
* @param start
* @param end
*/
private static void mergeSortShipmentsByWeight(final Shipment[] shipmentsToSort, final int start, final int end) {
if (start < end) {
// Find the middle point
final int m = (start + end) / 2;
// Sort first and second halves
mergeSortShipmentsByWeight(shipmentsToSort, start, m);
mergeSortShipmentsByWeight(shipmentsToSort, m + 1, end);
// Merge the sorted halves
merge(shipmentsToSort, start, m, end);
}
}
/**
* @param array
* @param start
* @param middle
* @param end
*/
private static void merge(final Shipment[] array, final int start, final int middle, final int end) {
// Find sizes of two subarrays to be merged
final int n1 = middle - start + 1;
final int n2 = end - middle;
/* Create temp arrays */
final Shipment L[] = new Shipment[n1];
final Shipment R[] = new Shipment[n2];
/* Copy data to temp arrays */
for (int i = 0; i < n1; ++i)
L[i] = array[start + i];
for (int j = 0; j < n2; ++j)
R[j] = array[middle + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = start;
while (i < n1 && j < n2) {
if (L[i].getWeight() >= R[j].getWeight()) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
array[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
array[k] = R[j];
j++;
k++;
}
}
/**
* @param sortedShipments
* @param vehicles
* @return
*/
public static LoadsOfVehicle[] distributeShipmentsToVehicles(final Shipment[] sortedShipments,
final Vehicle[] vehicles) {
// calculate total weight of shipments
// calculate total weight capacity of vehicles
// if capacity of vehicles is not enough add more turns for vehicles
// define return array with extra vehicle turns
// use modulus to add vehicles more than once if needed
// distribute weight to vehicles
double totalShipWt = 0, totalVechWt = 0;
for(Shipment ship: sortedShipments){
totalShipWt += ship.getWeight();
}
for(Vehicle vech: vehicles){
totalVechWt += vech.getWeightCapacity();
}
ArrayList<LoadsOfVehicle> arrayList= new ArrayList<>();
return arrayList.toArray(new LoadsOfVehicle[vehicles.length]);
}
/**
* @param vehicleListToLoad
* @param sortedShipments
*/
public static void loadTheVehicles(final LoadsOfVehicle[] vehicleListToLoad, final Shipment[] sortedShipments) {
// while there is more cargo to load
// distribute weight over vehicles
// calculate total weight if current weight is loaded
// there is a cargo to load and vehicle has the capacity
// load cargo to vehicle
// If no more cargo to load break while loop
// Base Case
for(LoadsOfVehicle v: vehicleListToLoad){
double cap = v.getVehicleCapacity();
}
}
public static void main(final String[] args) {
/*
* This main method is a stub. It does nothing. Feel free to write your own code
* to test your implementation. In this case, we have nothing actionable in
* here, just this comment block, so the JVM should rapidly lose interest and
* move on to the rest of your code.
*/
}
}