/ Published in: Java
When you execute bar code to zip, it doesn't work yet. maybe str value and bar# are different.
Expand |
Embed | Plain Text
/** for faster sorting of letters, the united states postal service encourages companies that send large volumes of mail to use a bar code denoting the ZIP code. The encoding scheme for a five-digit ZIP code is shoen in Figure 8. There are full-height frame bars on each side. The fice encoded digits are followed by a check digit, which is computed as follows : Add up all digits, and choose the check digit to make the sum a multip;e of 10. for example, the sum of the digits in the zip code 95014 is is 19, so the check digit is 1 to make the sum equal to 20. Each degit of the ZiP code,and the check digit , is encoded according to the following table: \ 7 4 2 1 0 1 0 0 0 1 1 2 0 0 1 0 1 3 0 0 1 1 0 4 0 1 0 0 1 5 0 1 0 1 0 6 0 1 1 0 0 7 1 0 0 0 1 8 1 0 0 1 0 9 1 0 1 0 0 0 1 1 0 0 0 When 0 denotes a harf bar and 1 a full bar. note that they represent all combinations of two full and three harf bars. The digit can be computed easily from the bar code using the column weights 7,4,2,1,0.for example,01100 is 0*7 + 1*4 + 1*2 + 0*1 + 0*0 = 6 digits The only exception is 0, which would yield 11 according to the weight formula. Write a program that asks the user for a zip code and prints the bar code. your program should also be abble to carry out the opposite conversion: translate bar into their zip code, reporting any errors in the input format or a mismatch of the digit. */ import java.util.Scanner; public class postal { /** Here is a constructor */ public int num2; // 10000 digit public int num3; // 1000 digit public int num4; // 100 digit public int num5; // 10 digit public int num6; // 1 digit public int checkDig; // check digit public static int num; public static int menu; public static int zip; public static int numb; public postal() { zip = 0; /** bar code */ bar0 = "::..."; bar1 = "...::"; bar2 = "..:.:"; bar3 = "..::."; bar4 = ".:..:"; bar5 = ".:.:."; bar6 = ".::.."; bar7 = ":...:"; bar8 = ":..:."; bar9 = ":.:.."; } public static int getZIP() { zip = sc.nextInt(); while((zip<01001)||(zip>99950)) { zip = sc.nextInt(); } return zip; } public static int menu() { return sc.nextInt(); } public void getDigit(int num) { num2 = num/10000; num3 = ((num/1000)-num2*10); num4 = (num/100 - (num2*100 + num3*10)); num5 = (num/10 -(num2*1000 + num3*100 + num4*10)); num6 = (num - (num2*10000 + num3*1000 + num4*100 + num5*10)); // error here; need to use correct formula checkDig = 100- (num2+num3+num4+num5+num6); while(checkDig>10) { checkDig -= 10; } getBar(num2); getBar(num3); getBar(num4); getBar(num5); getBar(num6); getBar(checkDig); } public void getBar(int x) { switch(x) { } } { return bar; } { if(temp.equals(bar0)) { numb = 0; } if(temp.equals(bar1)) { numb = 1; } if(temp.equals(bar2)) { numb = 2; } if(temp.equals(bar3)) { numb = 3; } if(temp.equals(bar4)) { numb = 4; } if(temp.equals(bar5)) { numb = 5; } if(temp.equals(bar6)) { numb = 6; } if(temp.equals(bar7)) { numb = 7; } if(temp.equals(bar8)) { numb = 8; } if(temp.equals(bar9)) { numb = 9; } } { postal po = new postal(); str = temp.substring(0,4); po.getNumber(str); str = temp.substring(5,9); po.getNumber(str); str = temp.substring(10,14); po.getNumber(str); str = temp.substring(15,19); po.getNumber(str); str = temp.substring(20,24); po.getNumber(str); } { postal pos = new postal(); while(menu != 3) { menu = pos.menu(); switch (menu) { case 1: num = pos.getZIP(); pos.getDigit(num); break; case 2: temp = pos.number(); pos.devide(temp); break; case 3: break; } } } }
You need to login to post a comment.
