Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 7th, 2012  |  syntax: None  |  size: 24.43 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package Trabajo;
  2. import javax.swing.JOptionPane;
  3.  
  4. public class Proyecto
  5. {
  6.         /**
  7.          * Metodo inicial.
  8.          */
  9.         public static void main(String[] args)          
  10.         {
  11.                 try
  12.                 {      
  13.                         int operacion = Integer.parseInt(JOptionPane.showInputDialog("Ingrese una opcion \n"
  14.                                                                                                                                         + "1-Conversion \n"
  15.                                                                                                                                         + "2-Suma \n"
  16.                                                                                                                                         + "3-Resta \n"
  17.                                                                                                                                         + "4-Multiplicacion"));
  18.                         iniciarMenu(operacion);
  19.                 }
  20.                 catch (Exception err)
  21.        
  22.                 {
  23.                 JOptionPane.showMessageDialog(null, "Error","Informacion Invalida!",JOptionPane.ERROR_MESSAGE);
  24.                 }
  25.         }
  26.  
  27.         /**
  28.          * Aplicacion para Conversion,sumar,resta o multiplicar Sistemas numericos
  29.          *
  30.          * @param operacion
  31.          * Codigo es de la opreacion
  32.          * @Autor: Diego Fernando Echverry
  33.          * @Autor: Pedro Holguin
  34.          */
  35.        
  36.        
  37.         /**
  38.          * Menu para seleccionar base en la operación de conversión.
  39.          *
  40.          * @param seleccionado
  41.          *            Numero seleccionado anteriormente, si se le pasa un -1 se
  42.          *            mostre todas las opciones.
  43.          *
  44.          * @return La base seleccionada por el usuario.
  45.          */
  46.         public static int menuBase(int seleccionado)
  47.         {
  48.  
  49.                 int baseSeleccionada = -1;
  50.                 if (seleccionado == -1)
  51.                 {
  52.                         // 1 binario, 2 octal, 3 decimal, 4 hexadecimal.
  53.  
  54.                         baseSeleccionada = Integer.parseInt(JOptionPane.showInputDialog("Ingrese una Base \n"
  55.                                                                                                                                                         + "1 - Binario \n"
  56.                                                                                                                                                         + "2 - Octal \n" + "3 - Decimal \n"
  57.                                                                                                                                                         + "4 - Hexadecimal"));
  58.                 } else
  59.                 {
  60.  
  61.                         String opcionBinario = " Binario \n";
  62.                         String opcionOctal =   " Octal \n";
  63.                         String opcionDecimal = " Decimal \n";
  64.                         String opcionHexadecimal = " Hexadecimal";
  65.                         String opcionesSeleccionables = "";
  66.                         int contador =2;
  67.                         if (seleccionado != 1)
  68.                                 opcionesSeleccionables +=contador++ +opcionBinario;
  69.                         if (seleccionado != 2)
  70.                                 opcionesSeleccionables +=contador++ + opcionOctal;
  71.                         if (seleccionado != 3)
  72.                                 opcionesSeleccionables +=contador++ + opcionDecimal;
  73.                         if (seleccionado != 4)
  74.                                 opcionesSeleccionables +=contador++ + opcionHexadecimal;
  75.  
  76.                         baseSeleccionada = Integer.parseInt(JOptionPane.showInputDialog("Ingrese una Base \n"
  77.                                                                                                                                                         + opcionesSeleccionables));
  78.                 }
  79.  
  80.                 if (baseSeleccionada <= 0 || baseSeleccionada > 4)
  81.                         {
  82.                                 JOptionPane.showMessageDialog(null, "Rango Invalido", "Error!",
  83.                                 JOptionPane.ERROR_MESSAGE);
  84.                         }
  85.  
  86.                 return baseSeleccionada;
  87.         }
  88.  
  89.         public static String ingresarNumero()
  90.         {
  91.                 return JOptionPane.showInputDialog("Ingrese el numero en la base origen");
  92.         }
  93.  
  94.  
  95.         public static void iniciarMenu(int operacion)
  96.         {
  97.  
  98.                 if (operacion >= 1 && operacion <= 4)
  99.                 {
  100.                        
  101.                                 switch (operacion)
  102.                                 {
  103.                                 // Conversion
  104.                                 case 1:
  105.                                         int baseOrigen = menuBase(-1);
  106.                                            String numeroIngresado = ingresarNumero();
  107.                                                 int baseDestino = menuBase(baseOrigen);
  108.                                                 if (numeroIngresado.length()>10) {
  109.                                                         JOptionPane.showMessageDialog(null,  "No se permiten numeros de mas de 10 digitos!","Error",JOptionPane.ERROR_MESSAGE);
  110.                                                 }else
  111.                                                 {
  112.                                                         if (numeroIngresado.charAt(0) == '-') {
  113.                                                                 JOptionPane.showMessageDialog(null, "Error","No se permiten numeros negativos!",JOptionPane.ERROR_MESSAGE);
  114.                                                         }else
  115.                                                         {
  116.                                                                
  117.                                                                         JOptionPane.showMessageDialog(null,convertirNumeroBase(baseOrigen,
  118.                                                                                         baseDestino, numeroIngresado));
  119.                                                         }
  120.                                                 }
  121.                                                
  122.                                                
  123.                                         break;
  124.  
  125.                                 // Suma
  126.                                 case 2:
  127.                                         int baseOperacion = menuBase(-1);
  128.                                         String[] datosOperacion = ingresarDatosOperacion();
  129.                                        
  130.                                                 if (datosOperacion[0].length()>10 ||datosOperacion[1].length()>10)
  131.                                                 {
  132.                                                         JOptionPane.showMessageDialog(null, "No se permiten numeros de mas de 10 digitos!","Error",JOptionPane.ERROR_MESSAGE);
  133.                                                 }else
  134.                                                 {
  135.                                                         if (datosOperacion[0].charAt(0)=='-' || datosOperacion[1].charAt(0)=='-')
  136.                                                         {
  137.                                                                 JOptionPane.showMessageDialog(null, "No se permiten numeros negativos!","Error",JOptionPane.ERROR_MESSAGE);
  138.                                                         }else
  139.                                                         {
  140.                                                                         JOptionPane.showMessageDialog(null,sumar(
  141.                                                                                         datosOperacion[0],
  142.                                                                                         datosOperacion[1],
  143.                                                                                         baseOperacion));
  144.                                                         }
  145.                                                
  146.                                         }
  147.                                         break;
  148.                                 // Resta
  149.                                 case 3:
  150.                                         int baseOperacionResta = menuBase(-1);
  151.                                        
  152.                                                 String[] datosOperacionResta = ingresarDatosOperacion();
  153.                                                 if (datosOperacionResta[0].charAt(0)=='-' || datosOperacionResta[1].charAt(0)=='-') {
  154.                                                         JOptionPane.showMessageDialog(null, "No se permiten numeros negativos!","Error",JOptionPane.ERROR_MESSAGE);
  155.                                                 }else
  156.                                                         {if (datosOperacionResta[0].length()>10 || datosOperacionResta[0].length()>10) {
  157.                                                                 JOptionPane.showMessageDialog(null,  "No se permiten numeros de mas de 10 digitos!","Error",JOptionPane.ERROR_MESSAGE);
  158.                                                                 }else
  159.                                                                 {
  160.                                                                         JOptionPane.showMessageDialog(null,restar(
  161.                                                                         datosOperacionResta[0],
  162.                                                                         datosOperacionResta[1],
  163.                                                                         baseOperacionResta));  
  164.                                                         }
  165.                                                 }
  166.                                         break;
  167.  
  168.                                 // Multiplicacion
  169.                                 case 4:
  170.                                         int baseOperacionMultiplicacion = menuBase(-1);
  171.                                         String[] datosOperacionMultiplicacion = ingresarDatosOperacion();
  172.                                         if (datosOperacionMultiplicacion[0].charAt(0)=='-' || datosOperacionMultiplicacion[1].charAt(0)=='-') {
  173.                                                 JOptionPane.showMessageDialog(null, "No se permiten numeros negativos!","Error",JOptionPane.ERROR_MESSAGE);
  174.                                         }else
  175.                                         {
  176.                                                 JOptionPane.showMessageDialog(null,multiplicar(
  177.                                                                 datosOperacionMultiplicacion[0],
  178.                                                                 datosOperacionMultiplicacion[1],
  179.                                                                 baseOperacionMultiplicacion)); 
  180.                                         }
  181.                                         if (datosOperacionMultiplicacion[0].length()>10 || datosOperacionMultiplicacion[0].length()>10) {
  182.                                                 JOptionPane.showMessageDialog(null,  "No se permiten numeros de mas de 10 digitos!","Error",JOptionPane.ERROR_MESSAGE);
  183.                                         }
  184.                                         break;
  185.                                 }
  186.  
  187.                        
  188.                 } else
  189.                 {
  190.                         JOptionPane.showMessageDialog(null, "No esta entre las opciones del Menu", "Error!",
  191.                                         JOptionPane.ERROR_MESSAGE);
  192.                 }
  193.         }
  194.  
  195.         /**
  196.          *
  197.          * @param primerOperando
  198.          * @param segundoOperando
  199.          * @param baseOperacion
  200.          * @return resultado de las sumas
  201.          */
  202.         private static String sumar(String primerOperando, String segundoOperando,int baseOperacion)
  203.         {
  204.  
  205.                 String retorno = "Resultado Suma: ";
  206.                 try {
  207.                         switch (baseOperacion)
  208.                         {
  209.                         // Binario
  210.                         case 1:
  211.                                 retorno += sumarBinarios(primerOperando, segundoOperando);
  212.                                 break;
  213.                         // Octal
  214.                         case 2:
  215.                                 retorno += sumarOctal(primerOperando, segundoOperando);
  216.                                 break;
  217.  
  218.                         // Decimal
  219.                         case 3:
  220.                                 retorno += sumarDecimal(primerOperando, segundoOperando);
  221.                                 break;
  222.  
  223.                         // Hexadecimal
  224.                         case 4:
  225.                                 retorno += sumarHexadecimal(primerOperando, segundoOperando);
  226.                                 break;
  227.                         }
  228.                 } catch (Exception err)
  229.                 {
  230.                         JOptionPane.showMessageDialog(null, "Error",
  231.                                         "Has introducido carcateres invalidos!",
  232.                                         JOptionPane.ERROR_MESSAGE);
  233.                 }
  234.                 if (retorno.equals("Resultado Suma: ")) {
  235.                         return null;
  236.                 }else {
  237.                         return retorno;
  238.                 }
  239.                
  240.         }
  241.  
  242.         /**
  243.          *
  244.          * @param primerOperando
  245.          * @param segundoOperando
  246.          * @return suma de binarios
  247.          */
  248.         private static String sumarBinarios(String primerOperando,String segundoOperando)
  249.         {
  250.                 int[] arreglo1 = convertirCadenaArreglo(primerOperando);
  251.                 int[] arreglo2 = convertirCadenaArreglo(segundoOperando);
  252.                 int mayor = arreglo1.length > arreglo2.length ? arreglo1.length: arreglo2.length;
  253.                 int[] resultado = new int[mayor + 1];
  254.                 boolean iguales = arreglo1.length == arreglo2.length;
  255.                 int loQuellevo = 0;
  256.                 String resultadofinal = "";
  257.                 for (int i = 0; i < mayor; i++)
  258.                 {
  259.                         // Cuando los arreglos tiene el mismo tamaño.
  260.                         if (iguales)
  261.                         {
  262.                                 int suma = arreglo1[i] + arreglo2[i] + loQuellevo;
  263.                                 if (suma == 2)
  264.                                 {
  265.                                         loQuellevo = 1;
  266.                                         resultado[i] = 0;
  267.                                 } else if (suma == 3)
  268.                                 {
  269.                                         loQuellevo = 1;
  270.                                         resultado[i] = 1;
  271.                                 } else {
  272.                                         loQuellevo = 0;
  273.                                         resultado[i] = suma;
  274.                                 }
  275.                                 if (i == mayor - 1)
  276.                                 {
  277.                                         resultado[i + 1] = loQuellevo;
  278.                                 }
  279.                         }
  280.                         // Cuando los arreglos NO tienen el mismo tamaño.
  281.                         else {
  282.                                 int[] pequeno = arreglo1.length < arreglo2.length ? arreglo1
  283.                                                 : arreglo2;
  284.                                 int[] grande = arreglo1.length > arreglo2.length ? arreglo1
  285.                                                 : arreglo2;
  286.                                 int operandoPequeno = 0;
  287.                                 // Cuando se puede acceder al indice del arreglo pequeño
  288.                                 if (i < pequeno.length) {
  289.                                         operandoPequeno = pequeno[i];
  290.                                 }
  291.                                 // Cuando NO se puede acceder al indice del arreglo pequeño
  292.                                 else
  293.                                 {
  294.                                         operandoPequeno = 0;
  295.                                 }
  296.                                 int suma = operandoPequeno + grande[i] + loQuellevo;
  297.                                 if (suma == 2)
  298.                                 {
  299.                                         loQuellevo = 1;
  300.                                         resultado[i] = 0;
  301.                                 } else if (suma == 3)
  302.                                 {
  303.                                         loQuellevo = 1;
  304.                                         resultado[i] = 1;
  305.                                 } else {
  306.                                         loQuellevo = 0;
  307.                                         resultado[i] = suma;
  308.                                 }
  309.                                 if (i == mayor - 1)
  310.                                 {
  311.                                         resultado[i + 1] = loQuellevo;
  312.                                 }
  313.                         }
  314.                 }
  315.                 int[] reversado = new int[resultado.length];
  316.                 for (int i = 0; i < resultado.length; i++) {
  317.                         reversado[i] = resultado[resultado.length - 1 - i];
  318.                         resultadofinal += reversado[i];
  319.                         if (resultadofinal.charAt(i)!='0' || resultadofinal.charAt(i)!='1') {
  320.                                 return "la informacion que ingreso no es valida! ";
  321.                         }else {
  322.                                
  323.                         }
  324.                 }
  325.                 return resultadofinal;
  326.         }
  327.         //invierte las los binarios para procesarlos*******************************************************=>
  328.                 public static int[] convertirCadenaArreglo(String operando) {
  329.                         int[] arrreglo = new int[operando.length()];
  330.                         for (int i = 0; i < arrreglo.length; i++) {
  331.                                 arrreglo[i] = Integer.parseInt(""+ operando.charAt(arrreglo.length - 1 - i));
  332.                         }
  333.                         return arrreglo;
  334.                 }
  335.  
  336.         /**
  337.          *
  338.          * @param primerOperando
  339.          * @param segundoOperando
  340.          * @return suma de Octal
  341.          */
  342.         private static String sumarOctal(String primerOperando,
  343.                         String segundoOperando)
  344.         {
  345.                 String primero =convertirOctalADecimal(primerOperando);
  346.                 String segundo= convertirOctalADecimal(primerOperando);
  347.                 String sumaDecimal = sumarDecimal(primero,segundo);
  348.                 String resultado = convertirDecimalAoctal(sumaDecimal);
  349.                 return resultado;
  350.         }
  351.         /**
  352.          *
  353.          * @param primerOperando
  354.          * @param segundoOperando
  355.          * @return suma de Decimal
  356.          */
  357.         private static String sumarDecimal(String primerOperando,String segundoOperando) {
  358.                 String resultadoCad = "";
  359.                 String sumaFinal = "";
  360.                 int[] arreglo1 = convertirCadena2Arreglo(primerOperando);
  361.                 int[] arreglo2 = convertirCadena2Arreglo(segundoOperando);
  362.                 int loQuellevo = 0;
  363.                 int[] resultado = new int[arreglo1.length];
  364.                 for (int i = 0; i < arreglo1.length; i++) {
  365.                         int suma =  arreglo1[i] + arreglo2[i]+loQuellevo;
  366.                         if (suma < 10) {
  367.                                 loQuellevo = 0;
  368.                                 resultado[i] = suma;
  369.                         }
  370.                         if (suma >= 10) {
  371.                                 loQuellevo = 1;
  372.                                 resultado[i] = suma - 10;
  373.                         }
  374.                         else {
  375.                                 loQuellevo = 0;
  376.                                 resultado[i] = suma;
  377.                         }
  378.  
  379.                 }
  380.                
  381.                 for (int j = resultado.length-1; j >=0 ; j--) {
  382.                         resultadoCad += resultado[j];
  383.                 }
  384.                 int resultadoFinal = Integer.parseInt(resultadoCad);
  385.                 sumaFinal += resultadoFinal;
  386.                 return sumaFinal;
  387.                
  388.         }
  389.        
  390.  
  391.         /**
  392.          *
  393.          * @param operando
  394.          *  invierte y da una misma longitud a los 2 arreglos
  395.          * @return
  396.          */
  397.         public static int[] convertirCadena2Arreglo(String operando) {
  398.                 int[] arrreglo = new int[10];
  399.                 for (int i = 0; i < arrreglo.length; i++) {
  400.                         arrreglo[i] = 0;
  401.                 }
  402.                 for (int i = operando.length() - 1; i >= 0; i--) {
  403.                         arrreglo[i] += Integer.parseInt(""
  404.                                         + operando.charAt(operando.length() - 1 - i));
  405.                 }
  406.                 return arrreglo;
  407.         }
  408.         /**
  409.          *
  410.          * @param primerOperando
  411.          * @param segundoOperando
  412.          * @return suma de hexadecimal
  413.          */
  414.         private static String sumarHexadecimal(String primerOperando,
  415.                         String segundoOperando)
  416.         {
  417.                 String primerOctal = convertirHexadecimalADecimal(primerOperando);
  418.                 String segundoOctal = convertirHexadecimalADecimal(segundoOperando);
  419.                 int primero = Integer.parseInt(primerOctal);
  420.                 int segundo = Integer.parseInt(segundoOctal);
  421.                 int operacion = primero + segundo;
  422.                 String resultado = "" + operacion;
  423.                 return convertirDecimalAhexadecimal(resultado);
  424.         }
  425.         /**
  426.          *
  427.          * @param primerOperando
  428.          * @param segundoOperando
  429.          * @param baseOperacion
  430.          * @return Resultado de las restas
  431.          */
  432.         private static String restar(String primerOperando, String segundoOperando,
  433.                         int baseOperacion)
  434.         {
  435.  
  436.                 String retorno = "Resultado Resta: ";
  437.  
  438.                 switch (baseOperacion)
  439.                 {
  440.                 // Binario
  441.                 case 1:
  442.                         retorno += restarBinarios(primerOperando, segundoOperando);
  443.                         break;
  444.                 // Octal
  445.                 case 2:
  446.                         retorno += restarOctal(primerOperando, segundoOperando);
  447.                         break;
  448.  
  449.                 // Decimal
  450.                 case 3:
  451.                         retorno += restarDecimal(primerOperando, segundoOperando);
  452.                         break;
  453.  
  454.                 // Hexadecimal
  455.                 case 4:
  456.                         retorno += restarHexadecimal(primerOperando, segundoOperando);
  457.                         break;
  458.                 }
  459.  
  460.                 return retorno;
  461.         }
  462.  
  463.         /**
  464.          *
  465.          * @param primerOperando
  466.          * @param segundoOperando
  467.          * @return Resta de Binarios
  468.          */
  469.         private static String restarBinarios(String primerOperando,
  470.                 String segundoOperando) {
  471.                 String primerBinario = convertirBinarioADecimal(primerOperando);
  472.                 String segundoBinario = convertirBinarioADecimal(segundoOperando);
  473.                 int primero = Integer.parseInt(primerBinario);
  474.                 int segundo = Integer.parseInt(segundoBinario);
  475.                 int operacion = primero - segundo;
  476.                 String resultado = "" + operacion;
  477.                 return convertirDecimalABinario(resultado);
  478.         }
  479.         /**
  480.          *
  481.          * @param primerOperando
  482.          * @param segundoOperando
  483.          * @return Resta de Octal
  484.          */
  485.         private static String restarOctal(String primerOperando,
  486.                         String segundoOperando) {
  487.                 String primerOctal = convertirOctalADecimal(primerOperando);
  488.                 String segundoOctal = convertirOctalADecimal(segundoOperando);
  489.                 int primero = Integer.parseInt(primerOctal);
  490.                 int segundo = Integer.parseInt(segundoOctal);
  491.                 int operacion = primero - segundo;
  492.                 String resultado = "" + operacion;
  493.                 return convertirDecimalAoctal(resultado);
  494.         }
  495.         /**
  496.          *
  497.          * @param primerOperando
  498.          * @param segundoOperando
  499.          * @return Resta de Decimal
  500.          */
  501.         private static String restarDecimal(String primerOperando,
  502.                         String segundoOperando) {
  503.                 int primero = Integer.parseInt(primerOperando);
  504.                 int segundo = Integer.parseInt(segundoOperando);
  505.                 int operacion = primero - segundo;
  506.                 String resultado = "" + operacion;
  507.                 return resultado;
  508.         }
  509.         /**
  510.          *
  511.          * @param primerOperando
  512.          * @param segundoOperando
  513.          * @return Resta de hexadecimal
  514.          */
  515.         private static String restarHexadecimal(String primerOperando,
  516.                         String segundoOperando) {
  517.                 String primerOctal = convertirHexadecimalADecimal(primerOperando);
  518.                 String segundoOctal = convertirHexadecimalADecimal(segundoOperando);
  519.                 int primero = Integer.parseInt(primerOctal);
  520.                 int segundo = Integer.parseInt(segundoOctal);
  521.                 int operacion = primero - segundo;
  522.                 String resultado = "" + operacion;
  523.                 return convertirDecimalAhexadecimal(resultado);
  524.         }
  525.  
  526.         /**
  527.          *
  528.          * @param primerOperando
  529.          * @param segundoOperando
  530.          * @param baseOperacion
  531.          * @return Resultado de la Multiplicacion
  532.          */
  533.         private static String multiplicar(String primerOperando,
  534.                         String segundoOperando, int baseOperacion)
  535.         {
  536.  
  537.                 String retorno = "Resultado Multiplicacion: ";
  538.  
  539.                 switch (baseOperacion)
  540.                 {
  541.                 // Binario
  542.                 case 1:
  543.                         retorno += multiplicarBinarios(primerOperando, segundoOperando);
  544.                         break;
  545.                 // Octal
  546.                 case 2:
  547.                         retorno += multiplicarOctal(primerOperando, segundoOperando);
  548.                         break;
  549.  
  550.                 // Decimal
  551.                 case 3:
  552.                         retorno += multiplicarDecimal(primerOperando, segundoOperando);
  553.                         break;
  554.  
  555.                 // Hexadecimal
  556.                 case 4:
  557.                         retorno += multiplicarHexadecimal(primerOperando, segundoOperando);
  558.                         break;
  559.                 }
  560.  
  561.                 return retorno;
  562.         }
  563.         /**
  564.          *
  565.          * @param primerOperando
  566.          * @param segundoOperando
  567.          * @return Multiplicacion de Binarios
  568.          */
  569.         private static String multiplicarBinarios(String primerOperando,
  570.                         String segundoOperando)
  571.         {
  572.                 String primerBinario = convertirBinarioADecimal(primerOperando);
  573.                 String segundoBinario = convertirBinarioADecimal(segundoOperando);
  574.                 int primero = Integer.parseInt(primerBinario);
  575.                 int segundo = Integer.parseInt(segundoBinario);
  576.                 int operacion = primero * segundo;
  577.                 String resultado = "" + operacion;
  578.                 return convertirDecimalABinario(resultado);
  579.         }
  580.         /**
  581.          *
  582.          * @param primerOperando
  583.          * @param segundoOperando
  584.          * @return Multiplicacion de Octal
  585.          */
  586.         private static String multiplicarOctal(String primerOperando,
  587.                         String segundoOperando)
  588.         {
  589.                 String primerOctal = convertirOctalADecimal(primerOperando);
  590.                 String segundoOctal = convertirOctalADecimal(segundoOperando);
  591.                 int primero = Integer.parseInt(primerOctal);
  592.                 int segundo = Integer.parseInt(segundoOctal);
  593.                 int operacion = primero * segundo;
  594.                 String resultado = "" + operacion;
  595.                 return convertirDecimalAoctal(resultado);
  596.         }
  597.         /**
  598.          *
  599.          * @param primerOperando
  600.          * @param segundoOperando
  601.          * @return Multiplicacion de Decimal
  602.          */
  603.         private static String multiplicarDecimal(String primerOperando,
  604.                         String segundoOperando)
  605.         {
  606.                 int primero = Integer.parseInt(primerOperando);
  607.                 int segundo = Integer.parseInt(segundoOperando);
  608.                 int operacion = primero * segundo;
  609.                 String resultado = "" + operacion;
  610.                 return resultado;
  611.         }
  612.         /**
  613.          *
  614.          * @param primerOperando
  615.          * @param segundoOperando
  616.          * @return Multiplicacion de hexadecimal
  617.          */
  618.         private static String multiplicarHexadecimal(String primerOperando,
  619.                         String segundoOperando)
  620.         {
  621.                 String primerOctal = convertirHexadecimalADecimal(primerOperando);
  622.                 String segundoOctal = convertirHexadecimalADecimal(segundoOperando);
  623.                 int primero = Integer.parseInt(primerOctal);
  624.                 int segundo = Integer.parseInt(segundoOctal);
  625.                 int operacion = primero * segundo;
  626.                 String resultado = "" + operacion;
  627.                 return convertirDecimalAhexadecimal(resultado);
  628.         }
  629.         /**
  630.          * Toma los datos Suma Resta Multiplicacion a operar
  631.          * @return Primer y Segundo Dato
  632.          */
  633.         public static String[] ingresarDatosOperacion()
  634.         {
  635.                 String primerDato = JOptionPane.showInputDialog("Primera Base:");
  636.                 String segundoDato = JOptionPane.showInputDialog("Segunda Base:");
  637.                 return new String[] { primerDato, segundoDato };
  638.         }
  639.  
  640.         /**
  641.  
  642.          *
  643.  
  644.          * @param primerOperando
  645.  
  646.          * @param segundoOperando
  647.  
  648.          * @param baseOperacion
  649.  
  650.          * @return
  651.  
  652.          */
  653.         public static String convertirNumeroBase(int baseOrigen, int baseDestino,
  654.                         String numeroIngresado)
  655.         {
  656.  
  657.                 String retorno = "Resultado: ";
  658.  
  659.                 switch (baseOrigen)
  660.                 {
  661.                 // Binario
  662.                 case 1:
  663.  
  664.                         switch (baseDestino)
  665.                         {
  666.                         // Octal
  667.                         case 2:
  668.                                         retorno += convertirBinarioAOctal(numeroIngresado);
  669.                                 break;
  670.  
  671.                         // Decimal
  672.                         case 3:
  673.                                 retorno += convertirBinarioADecimal(numeroIngresado);
  674.                                 break;
  675.  
  676.                         // Hexadecimal
  677.                         case 4:
  678.                                 retorno += convertirBinarioAHexadecimal(numeroIngresado);
  679.                                 break;
  680.                         }
  681.  
  682.                         break;
  683.  
  684.                 //BASE Octal
  685.                 case 2:
  686.                         switch (baseDestino)
  687.                         {
  688.                         // Binario
  689.                         case 2:
  690.                                 retorno += convertirOctalABinario(numeroIngresado);
  691.                                 break;
  692.  
  693.                         // Decimal
  694.                         case 3:
  695.                                 retorno += convertirOctalADecimal(numeroIngresado);
  696.                                 break;
  697.  
  698.                         // Hexadecimal
  699.                         case 4:
  700.                                 retorno += convertirOctalAhexdecimal(numeroIngresado);
  701.                                 break;
  702.                         }
  703.                         break;
  704.  
  705.                 //BASE Decimal
  706.                 case 3:
  707.                         switch (baseDestino)
  708.                         {
  709.                         // Binario
  710.                         case 2:
  711.                                 retorno += convertirDecimalABinario(numeroIngresado);
  712.                                 break;
  713.                         // octal
  714.                         case 3:
  715.                                 retorno += convertirDecimalAoctal(numeroIngresado);
  716.                                 break;
  717.  
  718.                         // hexadecimal
  719.                         case 4:
  720.                                 retorno += convertirDecimalAhexadecimal(numeroIngresado);
  721.                                 break;
  722.                         }
  723.                         break;
  724.  
  725.                 //BASE Hexadecimal
  726.                 case 4:
  727.                         switch (baseDestino)
  728.                         {
  729.                         // Binario
  730.                         case 2:
  731.                                 retorno += convertirHexadecimalABinario(numeroIngresado);
  732.                                 break;
  733.                         // octal
  734.                         case 3:
  735.                                 retorno += convertirHexadecimalAOctal(numeroIngresado);
  736.                                 break;
  737.  
  738.                         // decimal
  739.                         case 4:
  740.                                 retorno += convertirHexadecimalADecimal(numeroIngresado);
  741.                                 break;
  742.                         }
  743.                         break;
  744.                 }
  745.  
  746.                 System.out.println("baseOrigen " + baseOrigen
  747.                                                 + " baseDestino "+ baseDestino
  748.                                                 + " numeroIngresado " + numeroIngresado);
  749.                 return retorno;
  750.         }
  751.         /**
  752.          * Pasa un numero de Binario A Octal.
  753.          *
  754.          * @param numeroIngresado
  755.          *            Numero a convertir
  756.          */
  757.         public static String convertirBinarioAOctal(String base)
  758.         {
  759.                 String a = convertirBinarioADecimal(base);
  760.                 String octal = convertirDecimalAoctal(a);
  761.                 return octal;
  762.  
  763.         }
  764.         /**
  765.          * Pasa un numero de Binario A Decimal.
  766.          *
  767.          * @param numeroIngresado
  768.          *            Numero a convertir
  769.          */
  770.         public static String convertirBinarioADecimal(String base)
  771.  
  772.         {
  773.  
  774.                 String cadenaInver = "";
  775.                 int b = 0;
  776.                 int c = 0;
  777.                 String resulString = "";
  778.                 for (int i = base.length() - 1; i >= 0; i--)
  779.                 {
  780.                         cadenaInver += "" + base.charAt(i);
  781.                 }
  782.                 double[] arreglo;
  783.                 for (int i = 0; i <= cadenaInver.length(); i++)
  784.                 {
  785.                         arreglo = new double[99];
  786.                         arreglo[i] = (Math.pow(2, i));
  787.  
  788.                         for (int m = 0; m < cadenaInver.length(); m++)
  789.                         {
  790.                                 String convertir = "" + cadenaInver.charAt(m);
  791.                                 b = Integer.parseInt(convertir);
  792.                                 c += b * arreglo[m];
  793.                         }
  794.                 }
  795.                 resulString += c;
  796.                 return resulString;
  797.         }
  798.         /**
  799.          * Pasa un numero de Binario A hexadecimal.
  800.          *
  801.          * @param numeroIngresado
  802.          *            Numero a convertir
  803.          */
  804.         public static String convertirBinarioAHexadecimal(String base)
  805.         {
  806.                 String a = convertirBinarioADecimal(base);
  807.                 String hexadecimal = convertirDecimalAhexadecimal(a);
  808.                 return hexadecimal;
  809.  
  810.         }
  811.        
  812.         /**
  813.          * Pasa un numero de Octal A Binario.
  814.          *
  815.          * @param numeroIngresado
  816.          *            Numero a convertir
  817.          */
  818.         public static String convertirOctalABinario(String base)
  819.         {
  820.                 String a = convertirOctalADecimal(base);
  821.                 String bianrio = convertirDecimalABinario(a);
  822.                 return bianrio;
  823.         }
  824.         /**
  825.          * Pasa un numero de Octal A Decimal.
  826.          *
  827.          * @param numeroIngresado
  828.          *            Numero a convertir
  829.          */
  830.         public static String convertirOctalADecimal(String base)
  831.         {
  832.                 String cadenaInver = "";
  833.                 int b = 0;
  834.                 int c = 0;
  835.                 String decimal="";
  836.                 for (int i = base.length()-1; i >= 0; i--) {
  837.                         cadenaInver += ""+base.charAt(i);
  838.                 }
  839.  
  840.                 for (int i = 0; i < cadenaInver.length(); i++) {
  841.                                 String convertir = "" + cadenaInver.charAt(i);
  842.                                 b = Integer.parseInt(convertir);
  843.                                 c += (b * Math.pow(8, i));
  844.                 }
  845.                 decimal +=c;
  846.                 return decimal;
  847.         }
  848.         /**
  849.          * Pasa un numero de Octal A hexadecimal.
  850.          *
  851.          * @param numeroIngresado
  852.          *            Numero a convertir
  853.          */
  854.         public static String convertirOctalAhexdecimal(String base)
  855.         {
  856.                 String a = convertirOctalADecimal(base);
  857.                 String hexadecimal = convertirDecimalAhexadecimal(a);
  858.                 return hexadecimal;
  859.  
  860.         }
  861.         /**
  862.          *
  863.          * Pasa un numero de decimal a Binario.
  864.          *
  865.          * @param numeroIngresado
  866.          *            Numero a convertir
  867.          */
  868.         public static String convertirDecimalABinario(String base)
  869.         {
  870.                 int decimal = Integer.parseInt(base);
  871.                 String binario = "";
  872.                 while (decimal > 0)
  873.                 {
  874.                         binario = decimal % 2 + binario;
  875.                         decimal /= 2;
  876.                 }
  877.                 return binario;
  878.         }
  879.         /**
  880.          *
  881.          * Pasa un numero de decimal a Octal.
  882.          *
  883.          * @param numeroIngresado
  884.          *            Numero a convertir
  885.          */
  886.         public static String convertirDecimalAoctal(String base)
  887.         {
  888.                 int inicio = Integer.parseInt(base);
  889.                 String acumulador = "";
  890.                 if (inicio <= 8)
  891.                 {
  892.                         acumulador += inicio;
  893.                 } else {
  894.                         int div1 = inicio / 8;
  895.                         int div2 = div1 / 8;
  896.                         int div3 = div2 / 8;
  897.                         int div4 = div3 / 8;
  898.                         int div5 = div4 / 8;
  899.                         int div6 = div5 / 8;
  900.                         int div7 = div6 / 8;
  901.                         int div8 = div7 / 8;
  902.                         int div9 = div8 / 8;
  903.                         int[] arreglo = new int[10];
  904.                         arreglo[0] = inicio % 8;
  905.                         arreglo[1] = div1 % 8;
  906.                         arreglo[2] = div2 % 8;
  907.                         arreglo[3] = div3 % 8;
  908.                         arreglo[4] = div4 % 8;
  909.                         arreglo[5] = div5 % 8;
  910.                         arreglo[6] = div6 % 8;
  911.                         arreglo[7] = div7 % 8;
  912.                         arreglo[8] = div8 % 8;
  913.                         arreglo[9] = div9 % 8;
  914.                         for (int i = arreglo.length - 1; i >= 0; i--)
  915.                         {
  916.                                 if (arreglo[i] != 0)
  917.                                 {
  918.                                         acumulador += arreglo[i];
  919.                                 }
  920.                         }
  921.  
  922.                 }
  923.                 return acumulador;
  924.         }
  925.         /**
  926.          * Pasa un numero de decimal a hexadecimal.
  927.          *
  928.          * @param numeroIngresado
  929.          *            Numero a convertir
  930.          *
  931.          */
  932.         public static String convertirDecimalAhexadecimal(String bas)
  933.         {
  934.                 int decimal = Integer.parseInt(bas);
  935.                 String digits = "0123456789ABCDEF";
  936.                 if (decimal == 0)
  937.                         return "0";
  938.                 String hexde = "";
  939.                 while (decimal > 0) {
  940.                         int mod = decimal % 16; // Dígito de la derecha
  941.                         hexde = digits.charAt(mod) + hexde; // Concatenación de cadenas
  942.                         decimal = decimal / 16;
  943.                 }
  944.                 return hexde;
  945.         }
  946.         /**
  947.          * Pasa un numero de Hexadecimal A Binario
  948.          *
  949.          * @param numeroIngresado
  950.          *            Numero a convertir
  951.          */
  952.         public static String convertirHexadecimalABinario(String base)
  953.         {
  954.                 String a = convertirHexadecimalADecimal(base);
  955.                 String binario = convertirDecimalABinario(a);
  956.                 return binario;
  957.  
  958.         }
  959.         /**
  960.          * Pasa un numero de Hexadecimal A Octal
  961.          *
  962.          * @param numeroIngresado
  963.          *            Numero a convertir
  964.          */
  965.         public static String convertirHexadecimalAOctal(String base)
  966.         {
  967.                 String a = convertirHexadecimalADecimal(base);
  968.                 String octal = convertirDecimalAoctal(a);
  969.                 return octal;
  970.  
  971.         }
  972.         /**
  973.          *
  974.          * Pasa un numero de hexadecimal A Decimal.
  975.          *
  976.          * @param numeroIngresado
  977.          *            Numero a convertir
  978.          */
  979.         public static String convertirHexadecimalADecimal(String base)
  980.         {
  981.                 String digitos = "0123456789ABCDEF";
  982.                 String decimal = "";
  983.                 base = base.toUpperCase();
  984.                 int decimalInt = 0;
  985.                 for (int i = 0; i < base.length(); i++)
  986.                 {
  987.                         char c = base.charAt(i);
  988.                         int d = digitos.indexOf(c);
  989.                         decimalInt = 16 * decimalInt + d;
  990.                 }
  991.                 decimal += decimalInt;
  992.                 return decimal;
  993.         }
  994.  
  995.        
  996. }