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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

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

**NOTE:** For this assignment, you will need to use the `Character.getNumericValue()` method. This method takes a character value such as '2' and converts it to the appropriate integer value (in this case, the number 2). Also remember the `string_variable.CharAt()` method. Think about some of the work you have done in previous exercises involving traveling through strings. It is a great place for a couple of for loops.

```java
char input='2';
int inputInt = Character.getNumericValue(input);  // inputInt now has a value of 2
```

**NOTE 2:** For this assignment, you need to test whether or not a String is empty (a blank line). There are a few different ways that you can perform this check. One that will **NOT** work is to use the following if condition:

```java
if (myString == "") { /* do something if the string is empty */ }
```
Transcribed Image Text:**NOTE:** For this assignment, you will need to use the `Character.getNumericValue()` method. This method takes a character value such as '2' and converts it to the appropriate integer value (in this case, the number 2). Also remember the `string_variable.CharAt()` method. Think about some of the work you have done in previous exercises involving traveling through strings. It is a great place for a couple of for loops. ```java char input='2'; int inputInt = Character.getNumericValue(input); // inputInt now has a value of 2 ``` **NOTE 2:** For this assignment, you need to test whether or not a String is empty (a blank line). There are a few different ways that you can perform this check. One that will **NOT** work is to use the following if condition: ```java if (myString == "") { /* do something if the string is empty */ } ```
**Universal Product Code (UPC) Verification**

The Universal Product Code (UPC) is a widely used standard for barcodes. The UPC-A format specifies a 12-digit string where the first 11 digits form a unique code and the last digit serves as a "check digit" to verify the code's accuracy. When a UPC is correctly scanned, applying an algorithm to its first 11 digits should yield the check digit.

**Lab Task: Java Program for UPC Validation**

Your task is to develop a Java program that checks if UPC strings are valid.

1. **User Input:** Prompt the user to enter a UPC code or a blank line to quit the program.
2. **String Length Verification:** Ensure the entered string is 12 characters long. If not, display an error and prompt again.
3. **Check Digit Calculation:** Use the provided algorithm to compute the check digit and compare it to the given one in the string. Report if the UPC is valid or invalid. If invalid, specify the correct check digit.
4. **Continuation:** Continue accepting UPC codes until a blank line is entered.

**Algorithm for Validating a UPC:**

1. **Odd Numbered Positions:**
   - Sum the digits in odd-numbered positions (1 to 11) and multiply by 3.
2. **Even Numbered Positions:**
   - Add the digits in even-numbered positions to the result from step 1.
3. **Check Digit Calculation:**
   - Compute the remainder of the result in step 2 when divided by 10.
   - If the remainder is not zero, subtract it from 10 to find the check digit.
   - A remainder of zero means the check digit should be zero.
Transcribed Image Text:**Universal Product Code (UPC) Verification** The Universal Product Code (UPC) is a widely used standard for barcodes. The UPC-A format specifies a 12-digit string where the first 11 digits form a unique code and the last digit serves as a "check digit" to verify the code's accuracy. When a UPC is correctly scanned, applying an algorithm to its first 11 digits should yield the check digit. **Lab Task: Java Program for UPC Validation** Your task is to develop a Java program that checks if UPC strings are valid. 1. **User Input:** Prompt the user to enter a UPC code or a blank line to quit the program. 2. **String Length Verification:** Ensure the entered string is 12 characters long. If not, display an error and prompt again. 3. **Check Digit Calculation:** Use the provided algorithm to compute the check digit and compare it to the given one in the string. Report if the UPC is valid or invalid. If invalid, specify the correct check digit. 4. **Continuation:** Continue accepting UPC codes until a blank line is entered. **Algorithm for Validating a UPC:** 1. **Odd Numbered Positions:** - Sum the digits in odd-numbered positions (1 to 11) and multiply by 3. 2. **Even Numbered Positions:** - Add the digits in even-numbered positions to the result from step 1. 3. **Check Digit Calculation:** - Compute the remainder of the result in step 2 when divided by 10. - If the remainder is not zero, subtract it from 10 to find the check digit. - A remainder of zero means the check digit should be zero.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Operators
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education