Question: Explain each method in detail and why we use it !! public class ShippingService { //Instance variables. private Shipment table[]; private int size; private int
Explain each method in detail and why we use it !!
public class ShippingService {
//Instance variables.
private Shipment table[];
private int size;
private int count;
//Create an array of shipments and save the size of the hash table.
public ShippingService(int size) {
table = new Shipment[size];
this.size = size;
}
/*
Split the string address, based on underscore, to get the area.
Then, find the summation of characters ASCII values of area and return the hash key.
*/
public int hashFun(String address) {
String arr[] = address.split("_");
int adress = 0;
for(int i = 0; i < count; i++) {
adress += Math.pow(3, i) * (int)arr[i].charAt(0);
}
int id = 0;
adress += id;
return adress % size;
}
/*
Split the string address, based on underscore, to get the area. Then, find
the summation of characters ASCII values of area and return the hash key.
*/
public boolean insert(Shipment s) {
int currentCount=0;
int index=hashFun(s.getShippingAddress());
while(true) {
if(table[index]==null) {
table[index]=s;
return true;
}
currentCount++;
if(currentCount==size)
return false;
index=(index+1)%size;
}
}
//Call toString() method and print the contents of the Hash Table.
public void printTable() {
System.out.printf("%s\t%s\t%s\t%s\t ", "Shipping Address", "Customer Name", "Customer Number", "Shipment number");
System.out.println("");
int i = 0;
while (i < size) {
if (table[i] != null) {
System.out.println(table[i]);
System.out.println(" ");
}
i++;
}
}
//Count and return the number of shipments in the given area.
public int countArea(String area) {
int AreaC = 0;
int x = 0;
while (x < size) {
if (table[x] != null) {
String shipmentArea = table[x].getShippingAddress().split("_")[0];
if (shipmentArea.equalsIgnoreCase(area)) {
AreaC++;
}
}
x++;
}
return AreaC;
}
//Print all customers names in the given area.
public void printCustomers(String area) {
int y = 0;
while (y < size) {
if (table[y] != null) {
String shipmentArea = table[y].getShippingAddress().split("_")[0];
if (shipmentArea.equalsIgnoreCase(area)) {
System.out.println(table[y].getCustomerName());
}
}
y++;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
