Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

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

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;

    • There is a list of available vehicles, in which a vehicle can be used more than once if needed.

    • Total weight of assigned shipments to a vehicle cannot exceed its weight capacity.

    • Order of assignments must be from heaviest to lightest which will be used as a loading order.

image text in transcribed

Figure 2.4: Method Details

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.

Task

Develop the sortShipmentsByWeight() method in the Sort.java file to sort cargo from heaviest to lightest.

2 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.

Tasks

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)

2 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.

1 Implement the getLoadReport method in the LoadsOfVehicle.java file to generate a report.

2 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.

Our customer wants us to develop a new API for the daytime delivery operations given in Figure 2.3. They want an algorithm that will consider the weight of each shipment but this time they want it to be loaded to the delivery truck in a sequence that driver can drive through the neighborhood. So simply they want the shipment to be sorted firstly by address and then the shipments will be sorted with respect to weight.

To achieve this, you can divide the shipments with respect to their postal code. Since this operation will be done in a branch first 4 out 5 digits of the postal code will be same. The last digit will be different but close to each other for each neighboring district, and the driver will drive in that order. So for each postal code, heavy shipments will be loaded first. And of course, the last delivery must be first loaded to the vehicle (the report should be in increasing or decreasing order with respect to the postal code so that driver can use that order to travel between neighboring districts).

Briefly, an API will sort the shipments with respect to the postal code, then for each postal code, it will sort them with respect to the weight (from heaviest to lightest).

Below is the code given:

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. */ } }

Get Loading Array Sort Shipments by Weight Merge Sort wrt Weight Merge Distribute Shipments To Vehicles - - - Sort Shipments > Merge Sort - - Merge Sort - - > Merge Sorted - - Merge Sorted Merge - - Sorted Shipments Merged - - - Distribute the Cargo to Vehicles Distributed Cargo List - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Get Loading Array Sort Shipments by Weight Merge Sort wrt Weight Merge Distribute Shipments To Vehicles - - - Sort Shipments > Merge Sort - - Merge Sort - - > Merge Sorted - - Merge Sorted Merge - - Sorted Shipments Merged - - - Distribute the Cargo to Vehicles Distributed Cargo List

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions