Conversion from C to Java

-2

Consider a method that takes 2 parameters and does a calculation to generate a check digit. Here is the snippet of code I'm having trouble with:

char metodoDigito(char num, char qtos)
{

  short soma,
        peso,
        remainder;

  while( qtos ) 
  {
   soma += (*(num + --qtos) - '0') peso++;
  }

  remainder = soma % 11;
  return((char) ((remainder == 0 || remainder == 1) ? '0' : 59 - remainder));
                               /* = 11 - remainder + '0'*/
}

Some points:

  • while in Java we use boolean to say that the condition is true, in that case it uses a char . Tips on how to transform into Java?

  • soma += (*(num + qtos) - '0' ) peso++; at the end of the sum it multiplies by peso++ ?

  • I understand that remainder is the rest of the division.

asked by anonymous 24.09.2018 / 19:06

3 answers

2
  

While in java we use boolean to say that the condition is true, in which case it uses a char. Tips on how to turn into java?

In C a condition is false when it is 0, any other value is true. Only that. In Java it needs to be explicit, so you check if the variable is nonzero explicitly, using the != operator gives a Boolean result which is what while expects.

  

sum + = (* (num + qtos) - '0') weight ++; at the end of the sum it multiplies by weight ++?

This code does not make sense and does not compile.

I suggest writing a code from scratch is best to learn and to get a suitable result.

    
24.09.2018 / 19:14
0

Responding to your points:

  • The condition of while is equivalent to qtos != 0
  • The soma += (*(num + qtos) - '0' ) peso++; line gives compilation error. You need an operation (or semicolon) there between parentheses and peso++
  • This variable contains the rest of the division by 11.
24.09.2018 / 19:15
0

First, I think you forgot a * before peso++ , which I think should be ++peso in your case.

The variables soma and peso should be initialized. Their initial value is zero.

I suggest separating the use of the ++ and -- operators into their own isolated statements. Otherwise it gets very confusing. As they are incremented, decremented before being used in soma += ... , then they would be before soma += ... :

  while (qtos)
  {
      qtos--;
      peso++;
      soma += (*(num + qtos) - '0') * peso;
  }

Instead of while( qtos ) just use while (qtos >= 0) .

I think you wanted num to be String and not char . If in your original code, char num was char *num , that would be it. If this is the case, *(num + qtos) would become Java num.charAt(qtos) . In fact, the variable qtos is the size of String , so it does not need to be passed as a parameter, and should be set to the initial value of num.length() .

This can be simplified here:

remainder = soma % 11;
return ((char) ((remainder == 0 || remainder == 1) ? '0' : 59 - remainder));

The (remainder == 0 || remainder == 1) can be simplified to remainder <= 1 :

remainder = soma % 11;
return ((char) (remainder <= 1 ? '0' : 59 - remainder));

The idea is to return 11 - remainder , except in cases where this would result in 10 or 11. So you can do this:

remainder = 11 - (soma % 11);
return (char) ('0' + (remainder >= 10 ? 0 : remainder));

It is also a good idea to declare the variable only when you need it.

Your code looks like this:

char metodoDigito(String num) {
    short soma = 0, peso = 0, qtos = num.length();

    while (qtos >= 0) {
        qtos--;
        peso++;
        soma += (num.charAt(qtos) - '0') * peso;
    }

    short remainder = 11 - (soma % 11);
    return (char) ('0' + (remainder >= 10 ? 0 : remainder));
}

Three more comments:

  • I am wondering if it is best to return char or return int , I think int would be better.

  • There is also little reason to use short in Java since the default integer type in Java is int .

  • Your while can also be converted to a for loop.

  • It does not make much sense to declare the name metodo in a method. It is totally redundant and unnecessary. The name digitoVerificador (or even just dv ) would be better.

  • Probably this method would be static and public because it does not use instances of the class where it is inserted and does not appear to be an internal functionality of some class.

  • You can see that there is a relationship between the size of num , peso and qtos . The qtos value starts at num.length() - 1 and decreases to zero. The peso value goes from 1 to num.length() . In fact, in your code, when one increases the other decreases. With this, we can use the qtos == num.length() - peso relation and then delete the variable peso and we can also iterate the characters of num in any order.

With this, the code would be this:

public static int digitoVerificador(String num) {
    int soma = 0, peso = 0;

    for (char c : num.toCharArray()) {
        peso++;
        soma += (c - '0') * peso;
    }

    int remainder = 11 - (soma % 11);
    return remainder >= 10 ? 0 : remainder;
}
    
24.09.2018 / 19:48