Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am struggling to make my code run without errors! any help would be greatly ap

ID: 672153 • Letter: I

Question

I am struggling to make my code run without errors! any help would be greatly appreciated

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 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 what the check digit should be and then compare it to the actual value in the provided string and report whether the UPC is valid or wrong. If it is wrong, your program should report what the correct check digit should be for the input UPC. Your program should keep asking for new UPC until the user enters a blank line to quit the program.

The algorithm for checking for a valid UPC is:

From left to right, add the digits in the odd-numbered positions (starting the count from 1) and multiply the result by 3.

From left to right, add the digits in the even-numbered positions to the total computed in step 1

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.

Following the instructions from Closed Lab 01, create a new project named Project06 and a new Java program in that project folder named Project06.java for this project.

Project 06 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.

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

Enter a UPC (enter a blank line to quit): 014633149074
Check digit should be: 4
Check digit is: 4
UPC is valid

Enter a UPC (enter a blank line to quit): 0853911765722
ERROR! UPC MUST have exactly 12 digits

Enter a UPC (enter a blank line to quit): 085391176572
Check digit should be: 2
Check digit is: 2
UPC is valid

Enter a UPC (enter a blank line to quit):
Goodbye!

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).

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:

if (myString == "") { /* do something if the string is empty */ }

Explanation / Answer

The UPC (Universal Product Code) is a barcode symbol and is used to track trade items in stores. The most common form of UPC is the UPC-A which consists of 12 digits which is unique for a trade item. It consists of a strip of black and white spaces which can be scanned. The area that can be scanned in a UPC-A follows this pattern:

Here S -> Start, M -> Middle and E -> End

L -> Left and R -> Right make the barcode unique. The last digit in the barcode is the check digit.

Calculation and Verification of the check digit

Verification: To verify the number, we can use this formula:

[3.d1 + 1.d2 + 3.d3 + 1.d4 + 3.d5 + 1.d6 + 3.d7 + 1.d8 + 3.d9 + 1.d10 + 3.d11 + 1.d12] mod10 = 0

Here d1, d2, d3...etc. are the digits. Starting from the left, we multiply the digits with 3 and 1 alternatively.

Example: 036000 291452

3x0 + 1x3 + 3x6 + 1x0 + 3x0 + 1x0 + 3x2 + 1x9 + 3x1 + 1x4 + 3x5 + 1x2

=> 0+3+18+0+0+0+9+3+4+15+2 => 60 => 60mod10 => 0.

Hence the number is verified:

Calculation: To calculate the check digit, we use the same formula but subtract the remainder from 10 to get the check digit.

Example: 036000 29145?

3x0 + 1x3 + 3x6 + 1x0 + 3x0 + 1x0 + 3x2 + 1x9 + 3x1 + 1x4 + 3x5 + x

=> 0+3+18+0+0+0+9+3+4+15+x => 58 => 58mod10 => 8

10 - 8 => 2

Hence 2 is the check digit.

C implementation - validate a UPC number (12 digits):

/*

    Function to validate a UPC number

*/

int upc_a(char a[])

{

    int checksum = 0;

    int i, len,mul=3,sum=0, m10=0;

   

    len = strlen(a);

   

    

     if(len!=12)

      return -1;

   

    for(i=0; i<len; i++)

    {

      sum += char2int(a[i]) * mul;

      if(mul==3) mul=1;

      else mul = 3;

    }

   

    m10 = sum%10;

   

    if(m10 == 0)

     return 1;

    else

     return 0;

}

You can also try this code for ur required out put

/*Program to verify a 12-digit UPC code*/

#include <stdio.h>

#define ARRAY_SIZE 12

int main(void)

{int a[ARRAY_SIZE],

     sum_odd,

     sum_even,

     last_digit,

     check_digit,

     ans_odd,

     ans_even;

      

printf("Enter the 12-digit UPC code: ", a);

scanf ("%s", &a);

sum_odd=a[0]+a[2]+a[4]+a[6]+a[8]+a[10];

ans_odd=sum_odd*3;

sum_even=a[1]+a[3]+a[5]+a[7]+a[9]+a[11];

ans_even=ans_odd+sum_even;

last_digit=ans_even;

if(last_digit==0)

{

check_digit=0;

}

else

check_digit=10-last_digit;

if(check_digit=a[12])

{

printf("Your code is valid. ");

}

else printf("Your code is incorrect. ");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote