Please write the class which will work with the test class provided. And for bot
ID: 3592566 • Letter: P
Question
Please write the class which will work with the test class provided. And for both Zipcode to Barcode and Barcode to Zipcode. I really need a correct answer. The questions first asks zipcode to convert to barcode and then a brcode to convert to zipcode. Please provide a correct answer to my question, not just anything which is not useful for me
P8.6 For faster sorting of letters, the U.S. Postal Service encourages companies that send large volumes of mail to use a bar code denoting the ZIP code (see Figure 8) The encoding scheme for a five-digit ZIP code is shown in Figure 9. There are full-height frame bars on each side. The five encoded digits are followed by a check digit, which is computed as follows: Add up all digits, and choose the check digit to ECRLOT CO57 CODE C671RTS2 JOHN DOE 1009 FRANKLIN BLVD SUNNYVALE Frame bars CO57 CA 95014 5143 iDit Digit 1 Digit 2 Digit 3 Digit 4 Digit 5 Check Figure 8 A Postal Bar Code Figure 9 Encoding for Five-Digit Bar Codes ter 8 Designing Classes make the sum a multiple of 10. For example, the sum of the digits in the ZIP code 95014 is 19, so the check digit is 1 to make the sum equal to 20 Each digit of the ZIP code, and the check digit, is encoded according to the table at right, where 0 denotes a half bar and 1 a full bar. Note that thev represent all combinations of two full and three half bars. The digit can be computed easily from the bar code using the column weights 7,4,2,1,0. For example, 01100 is Weight 7 4 2 10 0 0 0 1 1 Digit 0x7+1x4+1x2+0x1+0x0-6 4 The only exception is 0, which would yield 11 according to the weight formula. Write a program that asks the user for aZIP code and prints the bar code. Use: for half bars, | for full bars. For example, 95014 becones 1 010 0 (Alternatively, write a graphical application that draws real bars.) Your program should also be able to carry out the opposite conversion: Translate bars into their ZIP code, reporting any errors in the input format or a mismatch of the digitsExplanation / Answer
Here is the Zipcode class for you:
class Zipcode
{
String barcode;
int zipcode;
public Zipcode(int zip)
{
zipcode = zip;
zipToBar();
}
public Zipcode(String bar)
{
barcode = bar;
barToZip();
}
public String getBarcode()
{
return barcode;
}
public int getZIPcode()
{
return zipcode;
}
private String barForTheDigit(char digit)
{
switch(digit)
{
case '0': return "||:::";
case '1': return ":::||";
case '2': return "::|:|";
case '3': return "::||:";
case '4': return ":|::|";
case '5': return ":|:|:";
case '6': return ":||::";
case '7': return "|:::|";
case '8': return "|::|:";
case '9': return "|:|::";
default : return "|";
}
}
private void zipToBar()
{
String zip = "" + zipcode;
barcode = "|";
int sum = 0;
for(int i = 0; i < zip.length(); i++)
{
barcode += barForTheDigit(zip.charAt(i));
sum += (zip.charAt(i) - '0');
}
int checkDigit = 10 - sum % 10;
barcode += barForTheDigit((char)(checkDigit + '0'));
barcode += "|";
}
private int barToDigit(String code)
{
if(code.equals("||:::"))
return 0;
else if(code.equals(":::||"))
return 1;
else if(code.equals("::|:|"))
return 2;
else if(code.equals("::||:"))
return 3;
else if(code.equals(":|::|"))
return 4;
else if(code.equals(":|:|:"))
return 5;
else if(code.equals(":||::"))
return 6;
else if(code.equals("|:::|"))
return 7;
else if(code.equals("|::|:"))
return 8;
else if(code.equals("|:|::"))
return 9;
else
return -1;
}
private void barToZip()
{
zipcode = 0;
for(int i = 0; i < 5; i++)
{
String code = barcode.substring(5*i+1, 5*i+5);
zipcode = zipcode * 10 + barToDigit(code);
}
}
}