Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write in java. Thank you! PROBLEM 1 For faster sorting of letters, the United States Postal Service encourages companies that send large volumes of

Please write in java. Thank you!

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

PROBLEM 1 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 see Figure 1: Example Zip Encoding. Until 2013, the encoding scheme for a five-digit ZIP code was known as POSTNET. The encoding of each numerical digit is shown in Table 1: Zip Code Digit Encoding Values. In addition to the Value Encoding bars for each digit, there are full-height frame bars on each side of the bar code see ull Figure 2: Encoded Digit Grouping. 2 The five encoded digits are followed by a check digit, which is computed as follows: ulil Take the sum of the digits and choose the check digit to make the sum a multiple of 10. For an example checksum calculation, see Equation 1: Example Checksum Calculation. Each digit of the ZIP code, and the check digit, is encoded according to the table at right. For this problem, we will write a program that allows the user to instantiate a zip code object using the bar code or numeric value and allow the user to convert the value to either a numerical or bar code format. To represent the encoding bars, we will use . for half bars and for full bars. For example, 95014 represented as a bar code would be ....1.1. || ......||.|..|... || Equation 1: Example Checksum Calculation Checksum Calculation: Lu Consider the ZIP code 95014. The sum of each digit is 9 + 5 + 0 + 1 + 4 is 19 Table 1: Zip Code Digit Encodina Values The check digit would be 1 to make the sum equal to a multiple of 10 (i.e., 19 + 1 = 20). JFebruary 18-1/13 llu *************** ECRLOT ** C057 - Frame bars CODE C671 RTS2 JOHN DOE 1009 FRANKLIN BLVD SUNNYVALE CA 95014 - 5143 C057 ielu lilo ll... ull olul li Digit 1 Digit 2 Digit 3 Digit 4 Digit 5 Check Digit Figure 2: Encoded Digit Grouping IIIIIIIIIIIIIIIIIIIIII Figure 1: Example Zip Encoding 1. Create a Java file called clszipcode.java. Do not add a main() method to clszipcode.java. 2. At the top of your .java file add the following documentation comments // Your Name // CS110 Section XX // Assignment XX // Todays Date 3. Create a constant instance variable of type String[] named DIGITCODES and assign the elements from Table 1: Zip Code Digit Encoding Values using the digit value as the position in the string array. HINT: ARRAYS ARE ZERO INDEXED, START WITH THE ENCODED REPRESENTATION OF O FOR POSITION O IN THE ARRAY. 4. Create an instance variables of type String named strzipBarCode. 5. Create an instance variable of type int named intzip. 6. Create an overloaded constructor of clsZipCode with one parameter of type int named zip. This constructor will store the zip code in the instance variable intzip and will convert the zip to its bar code representation and store it in strzipBarCode. a. In the body of the constructor, do the following: i. Assign the value of the parameter zip to the instance variable intzip. ii. Assign the empty string to the instance variable strzipBarCode. iii. Create two local variables of type int named intdigit and intchecksum and assign O to intdigit and intchecksum. 2/13 iv. Use a loop to iterate over each single digit number in intzip and convert it to its string representation of the encoded value using the index of DIGITCODES for the digit encoding. HINT: USE A LOOP TO DIVIDE INTZIP BY POWERS OF TEN FROM 10 TO 104. FOR EACH ITERATION OBTAIN THE REMAINDER OF THE CURRENT QUOTIENT DIVIDED BY TEN AND ADD THE VALUE TO INTCHECKSUM. 1. In the body of the loop, concatenate the string representation of the encoded value to strzipBarCode. v. Calculate the value of the check digit by summing each single digit number value in the zip code and calculating the difference between the value and the closest multiple of 10. vi. Convert the check digit to an encoded value and append the value to the end of strzipBarCode. b. Add a JavaDoc comment before the constructor signature with an explanation of your choice. The JavaDoc must be a complete sentence and explain what the method does. The parameter must be explained. 7. Create an overloaded constructor of clsZipCode with one parameter of type String named zipBarCode. This constructor will store the bar code representation of a zip code in strzipBarCode and will convert the bar code to its numerical representation and store the numerical value in the variable intzip. a. In the body of the constructor, do the following: i. Assign the value of the parameter zipBarCode to the instance variable strzipBarCode. ii. Create two local instance variables of type String named strdigits and strcurrentDigitBarCode and assign the empty string to strdigits and strcurrentDigitBarCode. iii. Use a loop to iterate 5 times. We will use each iteration to calculate a substring and convert the encoding to a numerical digit. 1. In the body of the loop, do the following: a. Create a substring of strzipBarCode for the 5 bars representing the current digit using the substring() method. HINT: SUBSTRING(*5, *5 + 5); b. Search for the matching encoding value in the DIGITCODES array and return the position when a match is found. The position represents the numerical value of the bar code. C. Convert the position to a String using Integer.toString() and append to strdigits. iv. Set the instance variable intzip equal to the integer representation of strdigits using the parseInt() method of the Integer class. HINT: AT THIS TIME STRDIGITS SHOULD BE A 5 DIGIT NUMBER STORED AS A STRING LIKE: 26505", USE THE PARSEINT METHOD TO CONVERT IT TO THE NUMERICAL VALUE 26505. b. Add a JavaDoc comment before the constructor signature with an explanation of your choice. The JavaDoc must be a complete sentence and explain what the method does. The parameter must be explained. B. Create a method with the signature public String toBar(). This method will not take any parameters and will return a String representing the encoded zip code as a bar code. This method will be used to retrieve the bar code value stored in the instance variable strzipBarCode. a. In the body of the method return the instance variable used to hold the bar code representation of the zip code. b. Add a JavaDoc comment before the method signature with an explanation of your choice. The JavaDoc must be a complete sentence and explain what the method does. The return value must be explained. 9. Create a method with the signature public int toZip(). This method will not take any parameters and will return an int representing the numeric zip code value. This method will be used to retrieve the zip code stored in the instance variable intzip. a. In the body of the method return the instance variable used to hold the zip code numerical value. b. Add a JavaDoc comment before the method signature with an explanation of your choice. The JavaDoc must be a complete sentence and explain what the method does. The return value must be explained. 10. Create a Java file called clszipcodeConverter.java in the same project directory as clszipcode.java. Add a main() method to clszipcodeConverter.java 11. At the top of your .java file add the following documentation comments // Your Name // CS110 Section XX // Assignment XX // Todays Date 12. In the main method, do the following: a. Create an object of type clsZipCode named morgantown with 26505 for the zip code. b. Create an object of type clsZipCode named beverlyhills with "1... ||..........||||...|..|." as the barcode. C. Call the .tobar() method on morgantown and print the result to the console. d. Call the .toZip() method on beverlyhills and print the result to the console

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

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions

Question

=+b) What was the purpose of using Major as a blocking factor?

Answered: 1 week ago