Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

The Structure Of The Relational Database Model

Authors: Jan Paredaens ,Paul De Bra ,Marc Gyssens ,Dirk Van Gucht

1st Edition

3642699588, 978-3642699580

More Books

Students also viewed these Databases questions

Question

=+ What are the information and consultation requirements?

Answered: 1 week ago

Question

=+ Should the MNE belong (why, why not)?

Answered: 1 week ago