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.
Operations
In mathematics and computer science, an operation is an event that is carried out to satisfy a given task. Basic operations of a computer system are input, processing, output, storage, and control.
Basic Operators
An operator is a symbol that indicates an operation to be performed. We are familiar with operators in mathematics; operators used in computer programming are—in many ways—similar to mathematical operators.
Division Operator
We all learnt about division—and the division operator—in school. You probably know of both these symbols as representing division:
Modulus Operator
Modulus can be represented either as (mod or modulo) in computing operation. Modulus comes under arithmetic operations. Any number or variable which produces absolute value is modulus functionality. Magnitude of any function is totally changed by modulo operator as it changes even negative value to positive.
Operators
In the realm of programming, operators refer to the symbols that perform some function. They are tasked with instructing the compiler on the type of action that needs to be performed on the values passed as operands. Operators can be used in mathematical formulas and equations. In programming languages like Python, C, and Java, a variety of operators are defined.
The Code must be Java language
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
Enter a UPC (enter a blank line to quit): 036000291452
Check digit should be: 2
Check digit is: 2
UPC is valid
Enter a UPC (enter a blank line to quit): 014633149077
Check digit should be: 4
Check digit is: 7
UPC is not valid
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images