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?