For this lab you will write a Java program that checks UPC strings to see if they are valid. Your program should first prompt the user to enter a string of numbers as a UPC code, or enter a blank line to quit the program. If the user doesn't quit, your program should ensure that this string is exactly 12 characters in length. If the user enters a string that is not 12 characters in length, your program should print an error message and ask again for a valid string. Your program should use the algorithm below to compute the check digit and compare it to the actual value in the provided string, reporting whether the UPC is valid or invalid. If it is invalid, your program should report what the correct check digit should be for the input UPC. Your program should keep asking for new UPC codes until the user enters a blank line to quit the program. The algorithm for checking for a valid UPC is: 1. From left to right, add the digits in the odd-numbered positions (starting the count from 1 to 11) and multiply the result by 3. 2. From left to right, add the digits in the even-numbered positions to the total computed in step 1 3. Take the result from step 2 and compute the remainder when divided by 10 (result modulo 10). If the remainder is not zero, subtract this remainder from 10 to get the check digit. If the remainder is zero, then the check digit should be 0.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter4: Selection Structures
Section4.5: A Case Study: Solving Quadratic Equations
Problem 2E
icon
Related questions
Question

The code must be in JAVA language 

The Universal Product Code (UPC) is a widely used standard for barcodes. The standard for one
type of UPC (known as UPC-A) says that a valid UPC must be encoded as a 12 digit string (text)
where the first 11 digits form the unique code and the final digit is used as a "check digit" to
ensure that the UPC has been correctly read by the scanner. If the UPC has been correctly read,
then the application of a specific algorithm to the first 11 digits of the UPC should give a result
equal to the check digit.
For this lab you will write a Java program that checks UPC strings to see if they are valid. Your
program should first prompt the user to enter a string of numbers as a UPC code, or enter a blank
line to quit the program. If the user doesn't quit, your program should ensure that this string is
exactly 12 characters in length. If the user enters a string that is not 12 characters in length, your
program should print an error message and ask again for a valid string. Your program should use
the algorithm below to compute the check digit and compare it to the actual value in the provided
string, reporting whether the UPC is valid or invalid. If it is invalid, your program should report
what the correct check digit should be for the input UPC. Your program should keep asking for
new UPC codes until the user enters a blank line to quit the program.
The algorithm for checking for a valid UPC is:
1. From left to right, add the digits in the odd-numbered positions (starting the count from
1 to 11) and multiply the result by 3.
2. From left to right, add the digits in the even-numbered positions to the total computed in
step 1
3. Take the result from step 2 and compute the remainder when divided by 10 (result
modulo 10). If the remainder is not zero, subtract this remainder from 10 to get the check
digit. If the remainder is zero, then the check digit should be 0.
Transcribed Image Text:The Universal Product Code (UPC) is a widely used standard for barcodes. The standard for one type of UPC (known as UPC-A) says that a valid UPC must be encoded as a 12 digit string (text) where the first 11 digits form the unique code and the final digit is used as a "check digit" to ensure that the UPC has been correctly read by the scanner. If the UPC has been correctly read, then the application of a specific algorithm to the first 11 digits of the UPC should give a result equal to the check digit. For this lab you will write a Java program that checks UPC strings to see if they are valid. Your program should first prompt the user to enter a string of numbers as a UPC code, or enter a blank line to quit the program. If the user doesn't quit, your program should ensure that this string is exactly 12 characters in length. If the user enters a string that is not 12 characters in length, your program should print an error message and ask again for a valid string. Your program should use the algorithm below to compute the check digit and compare it to the actual value in the provided string, reporting whether the UPC is valid or invalid. If it is invalid, your program should report what the correct check digit should be for the input UPC. Your program should keep asking for new UPC codes until the user enters a blank line to quit the program. The algorithm for checking for a valid UPC is: 1. From left to right, add the digits in the odd-numbered positions (starting the count from 1 to 11) and multiply the result by 3. 2. From left to right, add the digits in the even-numbered positions to the total computed in step 1 3. Take the result from step 2 and compute the remainder when divided by 10 (result modulo 10). If the remainder is not zero, subtract this remainder from 10 to get the check digit. If the remainder is zero, then the check digit should be 0.
Sample Output
This is a sample transcript of what your program should do. Items in bold are user input and
should not be put on the screen by your program. (Hint: I will put an electronic copy of the UPC
codes on Carmen. For testing you can use it to copy and paste the values rather than typing
them,)
Enter a UPC (enter a blank line to quit): 036000291453
Check digit should be: 2
Check digit is: 3
UPC is not valid
Transcribed Image Text:Sample Output This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program. (Hint: I will put an electronic copy of the UPC codes on Carmen. For testing you can use it to copy and paste the values rather than typing them,) Enter a UPC (enter a blank line to quit): 036000291453 Check digit should be: 2 Check digit is: 3 UPC is not valid
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr