Given two numbers A and B, is a third C created by alternating the first two? [closed]

2

Given two integers A and B, create a third integer C by following the following rules:

  • The first number of C is the first number of A;
  • The second number of C is the first number of B;
  • The third number of C is the second number of A;
  • The fourth number of C is the second number of B;

So successively ...

  • If the numbers of A or B are of different sizes, complete C with the remainder of the largest integer. Ex: A = 10256, B = 512, C should be 15012256.
  • If C is greater than 1,000,000, return -1

Develop an algorithm that meets all of the above requirements.

Proposed response code:

public class ManipulacaoNumerica {

public static void GerarC(String a, String b) {

    if (a != null && b != null) {

        StringBuilder c = new StringBuilder();
        System.out.println("Valores de Entrada: " + a + " - " + b);
        int i = 0;
        int j = 0;

        loop: for (; i <= a.length();) {
            System.out.println(i);
            int proxi = i + 1;
            if (proxi <= a.length()) {
                c.append(a.substring(i, proxi)).toString();
            }
            i++;

            for (; j <= b.length();) {
                System.out.println(j);
                int proxj = j + 1;
                if (proxj <= b.length()) {
                    c.append(b.substring(j, proxj)).toString();
                }
                j++;

                continue loop;

            }
        }
        try{
            int valor = Integer.valueOf(c.toString());
            if (valor < 1000000) {
                System.out.println("Valor de Saída: " + c);
            } else {
                valor = -1;
                System.out.println("Número maior que 1.000.000:" + valor);
            }
        }catch (Exception e) {
            // TODO: handle exception
            int valor = -1;
            System.out.println("Ocorreu um erro na aplicação: "+ e +" o valor de c é: "+valor);
        }
    }
}

public static void Executa(String a, String b) {

    if(a.length() > b.length()){
        GerarC(a,b);
    }else{
        GerarC(b,a);
    }

}

public static void main(String[] args) {

    Executa("24", "1999");

}

}

Is this code too bad? What can be improved?

    
asked by anonymous 22.09.2016 / 19:44

1 answer

3

Not that it's too bad, but it can be better. Some things are a matter of taste, others have more relevance. It has a case that can be bad in a more complex application, but in something simple, whether it is done well or not.

Looking at it, I find it more interesting to use%% than for each to scan texts. But if it is to use for that at least use the way it was thought and put the increment inside it and not in the body of the block.

This for does not seem to make any sense. In fact I find it unnecessary to have two loops, although I understand why they were created separately, except that they do not need to be already using continue loop; . You can walk on the two strings simultaneously on the same loop.

I think it's an exaggeration to use for for so few characters. But it's not wrong to use. In larger cases I would use.

Capturing StringBuilder is not usually an idea . I do not even know if there is a need for this exception. I'd rather avoid exceptions by analyzing the data before it occurs. There are several advantages to this.

I do not see a need for this Exception . Even Executa() is questionable, but it can be useful. I'll leave because you may have a reason not described in the question.

Some will say that it is bad to mix the screen with the calculation, that it would be better to have separation of responsibilities. Of course it depends on the goal. I gave a separate one, so I left the methods separate.

The code does not strictly do what is in the statement. It has logic errors. The statement is not good, it does not define everything that can occur. I invented something, even for simplicity.

Organizing a little more code helps, too.

I would do something like this:

import java.lang.Math;

class ManipulacaoNumerica {
    public static int GerarC(String a, String b) {
        if (a == null || b == null) {
            return -2; //inventei isso, não sei o que deveria fazer
        }
        String c = "";
        int limite = Math.max(a.length(), b.length());
        for (int i = 0; i <= limite; i++) {
            if (i < a.length()) {
                c += a.charAt(i);
            }
            if (i < b.length()) {
                c += b.charAt(i);
            }
        }
        return c.length() > 6 ? -1 : Integer.valueOf(c.toString()); //inventei,é quase isso
    }

    public static void Executa(String a, String b) {
        System.out.println("Valores de Entrada: " + a + " - " + b);
        System.out.println("Valor de Saída: " + GerarC(a, b));
    }

    public static void main(String[] args) {
        Executa("24", "1999");
    }
}

See running on ideone .

    
22.09.2016 / 20:32