Question

Have you ever noticed the bar code printed on envelopes? That code helps the USPS process the mail. The barcode is generated as specified below.

In this assignment you are to write a JAVA program that will accept a 5 digit number from the user and then prints the appropriate symbols to the screen. Name your file ZipCode_yourInitials.java. The specifications for the code are PostalCodeAsignment.pdf. Preview the documentUsing a do while loop allow the user to continue entering numbers as long as they want. Using a do while loop trap the user (ask them to reenter the zip code) if the zipcode entered is < 0 or > 99999.

Save yourself some time on this assignment. Think about it and do some planning before you start to write your code. You should consider:

  • What will be the overall process of your program
  • What methods are needed
  • What will the methods do, what arguments will they need passed, what will they return (if anything)
  • What variables are needed
  • In what order should methods be called
  • What will be the input to your program
  • What will be the output from your program

Here is an example of input and output.

Please enter a 5 digit number, 00000 - 99999
11111
The postal code is:
|:::||:::||:::||:::||:::||:|:|:|
Would you like to enter another zip code, enter Y or N.
Y
Please enter a 5 digit number, 00000 - 99999
00000
The postal code is:
|||:::||:::||:::||:::||:::||:::|
Would you like to enter another zip code, enter Y or N.
Y
Please enter a 5 digit number, 00000 - 99999
02302
The postal code is:
|||:::::|:|::||;||:::::|:|::||;|
Would you like to enter another zip code, enter Y or N.
Y
Please enter a 5 digit number, 00000 - 99999
11111111
Please enter a 5 digit number, 00000 - 99999
90210
The postal code is:
||:|::||:::::|:|:::||||:::|::|:|
Would you like to enter another zip code, enter Y or N.
k
Would you like to enter another zip code, enter Y or N.
Y
Please enter a 5 digit number, 00000 - 99999
12345
The postal code is:
|:::||::|:|::||;:|::|:|:|::|:|:|
Would you like to enter another zip code, enter Y or N.
N

PDF

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, I have answered this question many times before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// ZipCode.java

import java.util.Scanner;

public class ZipCode {

      // constants to represent characters for full bar and half bar

      public static final char FB = '|', HB = ':';

      // an array of Strings to represent the barcodes of digits from 0 to 9

      // (based on the given table)

      private static String[] barcode = { "" + FB + FB + HB + HB + HB,

                  "" + HB + HB + HB + FB + FB, "" + HB + HB + FB + HB + FB,

                  "" + HB + HB + FB + FB + HB, "" + HB + FB + HB + HB + FB,

                  "" + HB + FB + HB + FB + HB, "" + HB + FB + FB + HB + HB,

                  "" + FB + HB + HB + HB + FB, "" + FB + HB + HB + FB + HB,

                  "" + FB + HB + FB + HB + HB };

      /**

      * method to print the bar code of a zipcode

      *

      * @param zipCode

      *            - zip code in String format

      */

      public static void printBarCode(String zipCode) {

            // printing start full frame bar

            System.out.print(FB);

            int sum = 0; // to store the sum of digits

            // looping through all digits in the zipcode

            for (int i = 0; i < zipCode.length(); i++) {

                  // parsing the digit at index i

                  int digit = Integer.parseInt("" + zipCode.charAt(i));

                  // adding to total

                  sum += digit;

                  // printing barcode of digit

                  printDigit(digit);

            }

            // finding remainder of sum when divided by 10

            int remainder = sum % 10;

            if (remainder == 0) {

                  // check digit is 0

                  printDigit(0);

            } else {

                  // check digit is 10-remainder

                  printDigit(10 - remainder);

            }

            // printing stop full frame bar

            System.out.print(FB + "\n");

      }

      /**

      * method to print the bar code of a digit between 0 and 9

      */

      public static void printDigit(int d) {

            if (d >= 0 && d <= 9) {

                  System.out.print(barcode[d]);

            }

      }

      public static void main(String[] args) {

            // scanner to read user input

            Scanner sc = new Scanner(System.in);

            String choice = "y";

            // looping

            do {

                  // prompting user for a 5 digit number

                  System.out

                              .println("Please enter a 5 digit number, 00000 - 99999: ");

                  // getting zip code as String

                  String zip = sc.nextLine();

                  // validating length

                  if (zip.length() == 5) {

                        try {

                              // making sure user entered a valid 5 digit number

                              Integer.parseInt(zip);

                              // if the control reach this line, it means that the zip

                              // code is parsable to integer without causing an

                              // exceptions.

                              System.out.println("The postal code is:");

                              // displaying bar code

                              printBarCode(zip);

                              // asking user if he wants to continue

                              System.out

                                          .println("Would you like to enter another zip code, enter Y or N.");

                              choice = sc.nextLine();

                        } catch (Exception e) {

                              // exception occurred due to non numeric input, just

                              // ignoring.

                        }

                  }

            } while (choice.equalsIgnoreCase("y"));

      }

}

/*OUTPUT*/

Please enter a 5 digit number, 00000 - 99999:

11111

The postal code is:

|:::||:::||:::||:::||:::||:|:|:|

Would you like to enter another zip code, enter Y or N.

y

Please enter a 5 digit number, 00000 - 99999:

00000

The postal code is:

|||:::||:::||:::||:::||:::||:::|

Would you like to enter another zip code, enter Y or N.

y

Please enter a 5 digit number, 00000 - 99999:

02302

The postal code is:

|||:::::|:|::||:||:::::|:|::||:|

Would you like to enter another zip code, enter Y or N.

y

Please enter a 5 digit number, 00000 - 99999:

11111111

Please enter a 5 digit number, 00000 - 99999:

10101

The postal code is:

|:::||||::::::||||::::::|||:::||

Would you like to enter another zip code, enter Y or N.

N

Add a comment
Know the answer?
Add Answer to:
Have you ever noticed the bar code printed on envelopes? That code helps the USPS process...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1 Until 2009, the US Postal Service printed a bar code on every envelope that represented...

    1 Until 2009, the US Postal Service printed a bar code on every envelope that represented the zip code using a format called POSTNET. We will be doing the same with only 5-digit zip codes. POSTNET consists of long and short lines, as seen below: The POSTNET representation of 67260, WSU's zip code In the program, the zipcode will be represented by an integer and the corresponding barcode will be represented by strings of digits. The digit 1 will represent...

  • Python Write a program that asks the user for a zip code and converts it to...

    Python Write a program that asks the user for a zip code and converts it to a postal bar code. A postal bar code uses two types of lines long and short. Since we can't print "long line” we will use: 0 - short line 1 - long line. The digits are represented by bars as explained in the below chart Value Encoding 1 00011 2 00101 00110 01001 01010 01100 10001 8 10010 9 10100 0 11000 The bar...

  • Write C++ program T 1030 UUIII DUCOUL The bar code on an envelope used by the...

    Write C++ program T 1030 UUIII DUCOUL The bar code on an envelope used by the US Postal Service 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 below: Illlllllllll For this program we will represent the bar code as a string of digits. The digit 1 represents a long bar...

  • please code in basic c++ program Write a program that uses the following formula: n (ax...

    please code in basic c++ program Write a program that uses the following formula: n (ax - ib) i=1 Your program will prompt the user for the number of iterations for the calculation. input values for n, x, a, b, and c. n must be a positive integer, but x, a, b, and c can be any real numbers. Then it will perform the calculation and output the result. Then, it will ask the user if they would like to...

  • the user should be prompted to enter an integer and then your program will produce another...

    the user should be prompted to enter an integer and then your program will produce another integer depending on whether the input was even or odd. The following is an example of what you might see when you run the program; the input you type is shown in green (press Enter at the end of a line), and the output generated by the program is shown in black text. Enter an integer: 1234 Number is even, taking every other digit...

  • This assignment must be completed on your own with your team mates. Don't give your code...

    This assignment must be completed on your own with your team mates. Don't give your code to others or use other students' code. This is considered academic m will result 0 marks) Write a java program that mange JUC Bank accounts services. The program should have the following: The total number of accounts (n) will be specified by the user at the beginning of the program. Depending upon the entered number, create three Arrays as following o o o Array...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • Part 1 – Data Encryption Data Encryption is the process of transforming data, using a cipher,...

    Part 1 – Data Encryption Data Encryption is the process of transforming data, using a cipher, to make it unreadable to anyone except those possessing special knowledge. In this problem you will implement a simple encryption technique on data that comprises of non-negative integers that have at least six digits with no leading zeroes. In order to encrypt the number the following functions are to be implemented: void input(int *num); int add4(int num); int shift(int num); void printOutput(int encryptNum, int...

  • C++ : Please include complete source code in answer This program will have names and addresses...

    C++ : Please include complete source code in answer This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record. When the program is run, it will search for a birthday or an anniversary using the current date to compare with the saved date. It will then generate the appropriate card message. Because this will be an interactive system, your program should begin by displaying a...

  • write programs with detailed instructions on how to execute. code is java What you need to...

    write programs with detailed instructions on how to execute. code is java What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT