Converting C to Java

Asked

Viewed 491 times

-2

Consider a method that takes 2 parameters and makes a calculation to generate a check digit. Follow the code snippet I’m having difficulty:

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 which case he uses a char. Tips on how to turn into Java?

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

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

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

2

while in Java we use boolean to say that the condition is true, in this 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. That’s all. In Java it needs to be explicit, so you check whether the variable is non-zero explicitly, using the operator != gives a boolean result which is what the while waiting.

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

This code makes no sense and does not compile.

I suggest writing a code from scratch is better to learn and to get a proper result.

1

First, I think you missed one * before the peso++, which, by the way, 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 operators ++ and -- in their own isolated instructions. Otherwise it gets very confusing. How they are incremented, decremented before being used in soma += ..., then they would stay before the soma += ...:

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

In place of while( qtos ) just use while (qtos >= 0).

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

This can be simplified from 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 it is possible to do this:

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

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

Your code would look 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 remarks:

  • I have doubts if it’s best to return char or return int, I think that 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 into a loop for.

  • It doesn’t 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.

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

  • If we iterate the characters of num in the order they appear, we can use the syntax of Enhanced-for.

With that, the code would be:

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;
}

0

Answering your points:

  • The condition of while is equivalent to qtos != 0
  • The line soma += (*(num + qtos) - '0' ) peso++; gives build error. You need an operation (or semicolon) there between the parentheses and the peso++
  • This variable does contain the rest of the division by 11.
  • Say you have a semicolon between parenthesis and weight++, what is the purpose of subtracting '0' from the previous sum? This C code was passed to me to convert to Java, I didn’t get it from the Internet or anything like that

  • I do not know the intention, it seems strange (not to say poorly done) the calculation - I do not know the purpose of it. Subtraction by '0' would be a subtraction by the letter code, I believe. I think the idea here is to convert num for integer and subtract...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.