Answer to Question #159207 in Java | JSP | JSF for ZULFIQER

Question #159207

Mr. Akbar is senior software developer working on an ecommerce website he wants to add the following products on his website with following keys

Mouse 4614

Keyboard 5882

Cable 6713

Webcam 4409

Monitor 1825

To make searching better he asked his junior to find the 2 digit hash address of products by following techniques and share results with him 

a) Division method with m=97

b) Mid square method

c) Folding method without reversing

d) Folding method with reversing


1
Expert's answer
2021-01-28T11:29:37-0500
public class HashCodeGenerator {


	public static void main(String[] args) {
		int mouseOne=divisionMethod(4614);
		System.out.println(mouseOne);
		int mouseTwo=midSquareMethod(4614);
		System.out.println(mouseTwo);
		int mouseThree=midSquareMethod(4614);
		System.out.println(mouseThree);
		int keyboardOne=divisionMethod(5882);
		System.out.println(keyboardOne);
		int keyboardTwo=midSquareMethod(5882);
		System.out.println(keyboardTwo);
		int keyboardThree=divisionMethod(5882);
		System.out.println(keyboardThree);
		int webcameOne=divisionMethod(4409);
		System.out.println(webcameOne);
		int webcameTwo=midSquareMethod(4409);
		System.out.println(webcameTwo);
		int webcameThree=divisionMethod(4409);
		System.out.println(webcameThree);
		int monitorOne=divisionMethod(4409);
		System.out.println(monitorOne);
		int monitorTwo=midSquareMethod(4409);
		System.out.println(monitorTwo);
		int monitorThree=divisionMethod(4409);
		System.out.println(monitorThree);
	}


	static int divisionMethod(int x) {
		int m = 97;
		return Math.abs(x) % m;
	}


	static int midSquareMethod(int x) {
		int k = 10;
		int w = Integer.SIZE;
		return (x * x >> (w - k));
	}
	 static int foldingMethod(int x) {
	        int arraySize = 1021;
	        int keyDigitCount = getDigitCount(x);
	        int groupSize = getDigitCount(arraySize);
	        int groupSum = 0;
	        String keyString = Integer.toString(x);
	        int i;
	        for (i = 0; i < keyString.length(); i += groupSize) {
	            if (i + groupSize <= keyString.length()) {
	                String group = keyString.substring(i, i + groupSize);
	                groupSum += Integer.parseInt(group);
	            }
	        }
	        if (keyDigitCount % groupSize != 0) {
	            String remainingPart = 
	                   keyString.substring(i - groupSize, keyString.length());
	            groupSum += Integer.parseInt(remainingPart);
	        }
	        return groupSum % arraySize;
	    }
	    public static int getDigitCount(int x) {
	        int numDigits = 1;
	        while (x > 9) {
	            x /= 10;
	            numDigits++;
	        }
	        return numDigits;
	    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog