SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Defining Classes
Exercise:
Programming Projects
Question:13 | ISBN:9780321531346 | Edition: 7

Question

The U.S. Postal Service prints a bar code on every envelope that represents a five (or more) digit zip code using a format called POSTNET (this format is being deprecated in favor of a new system, OneCode, in 2009).

The bar code consists of long and short bars as shown:

For this program, we will represent the bar code as a string of digits.

The digit 1 represents a long bar, and the digit 0 represents a short bar.

Therefore, the bar code would be represented in our program as:

110100101000101011000010011

The first and last digits of the bar code are always 1. Removing these leave

25 digits. If these 25 digits are split into groups of five digits each, we have: 10100 10100 01010 11000 01001

Next, consider each group of five digits. There will always be exactly two

1’s in each group of digits. Each digit stands for a number. From left to right, the digits encode the values 7, 4, 2, 1, and 0. Multiply the corresponding value with the digit and compute the sum to get the final encoded digit

for the zip code. The table below shows the encoding for 10100.

Bar Code

Digits

1 0 1 0 0

Value 7 4 2 1 0

Product of

Digit * Value

7 0 2 0 0

Zip Code Digit _ 7 _ 0 _ 2 _ 0 _ 0 _ 9

Repeat this for each group of five digits and concatenate to get the

complete zip code. There is one special value. If the sum of a group of five

digits is 11, then this represents the digit 0 (this is necessary because with

two digits per group it is not possible to represent zero). The zip code for

the sample bar code decodes to 99504. While the POSTNET scheme may

seem unnecessarily complex, its design allows machines to detect if errors

have been made in scanning the zip code.

Write a zip code class that encodes and decodes five-digit bar codes used

by the U.S. Postal Service on envelopes. The class should have two

constructors. The first constructor should input the zip code as an integer,

and the second constructor should input the zip code as a bar code string

consisting of 0’s and 1’s, as described above. Although you have two ways

to input the zip code, internally, the class should only store the zip code

using one format (you may choose to store it as a bar code string or as a

zip code number.) The class should also have at least two public member

functions, one to return the zip code as an integer, and the other to return

the zip code in bar code format as a string. All helper functions should be

declared private. Embed your class definition in a suitable test program.

Your program should print an error message if an invalid bar code is

passed to the constructor.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.