You are on page 1of 20

UNIVERSIDAD TECNICA DE AMBATO

FACULTAD DE INGENIERIA EN SISTEMAS,


ELECTRONICA E INDUSTRIAL
NOMBRE: Jair Vela
NIVEL: 3 A
Creacin de un programa para transformar nmeros a
diferentes bases (Hexadecimal, Octal, Binario,
Decimal).
PROGRAMA EJECTUTABLE MENU
import java.util.Scanner;
public class convertidor {
public static void main(String[] args) {
Scanner tecla = new Scanner(System.in);
int op,valor4;
String valor1,valor2,valor3;
op = 0;
String temp;
do {
do {
General.TituloEnmarcado(" Convertidor ");
System.out.printf("%15s%s\n"
+ "%15s%s\n"
+ "%15s%s\n"
+ "%15s%s\n"
+ "%15s%s\n\n",
"", "1._ Hexadecimal ",
"", "2._ Binario ",
"", "3._ Octal",
"", "4._ Decimal",
"", "5._ Salir");
System.out.printf("%30s",
"Opcion (1-5) = ");
temp = tecla.nextLine();
op = Integer.valueOf(temp);
if (op < 1 || op > 5) {
System.out.println("ERROR: Eleccion fuera de rango
de 1-5.......");
}
} while (op < 1 || op > 5);
if (op < 5) {
switch (op) {

case 1:
System.out.printf("%30s",
"Ingrese el valor Hexadecimal = ");
valor1 = tecla.nextLine().toUpperCase();
System.out.println("Valor Decimal ==> " +
General.Hexadecimal_Decimal(valor1));
System.out.println("Valor Octal ==> " +
General.Hexadecimal_Octal(valor1));
System.out.println("Valor Binario ==> " +
General.Hexadecimal_Binario(valor1));
break;
case 2:
System.out.printf("%30s",
"Ingrese el valor Binario = ");
valor2 = tecla.nextLine().toUpperCase();
if(valor2.length() != 0 || valor2.length() !=
1){
System.out.println("ERROR!!:El valor
ingresado no es binario...");
}else{
System.out.println("Valor Decimal ==> " +
General.Binario_Decimal(valor2));
System.out.println("Valor Octal ==> " +
General.Binario_Octal(valor2));
System.out.println("Valor Hexadecimal ==> " +
General.Binario_Hexadecimal(valor2));
}
break;
case 3:
System.out.printf("%30s",
"Ingrese el valor Octal ==> ");
valor3 = tecla.nextLine().toUpperCase();
if(valor3.length() < 9){
System.out.println("ERROR!!: Ingrese
numeros entre 0-8");
}else{
System.out.println("Valor en Decimal ==> " +
General.Octal_Decimal(valor3));
System.out.println("Valor en Binario ==> " +
General.Octal_Binario(valor3));
System.out.println("Valor en Hexadecimal ==> "
+ General.Octal_Hexadecimal(valor3));
}
break;
case 4:
System.out.printf("%30s",
"Ingrese el valor Decimal ==> ");
valor4 = Integer.valueOf(tecla.nextLine());

System.out.println("Valor Binario ==> "+


General.Decimal_Binario(valor4));
System.out.println("Valor hexadecimal ==> " +
General.Decimal_Hexadecimal(valor4));
System.out.println("Valor Octal ==> " +
General.Decimal_Octal(valor4));
break;
}
}
} while (op != 5);
System.out.println(
"GRACIAS POR UTILIZAR NUESTRO PROGRAMA... ");
}
}

CLASE GENERAL DE METODOS


import Ensayos.*;
import practicas.*;
public class General {
public static void uta() {
System.out.println("Universidad Tcnica de Ambato");
}
public static void uta1() {
System.out.printf("%50S\n\n",
"Universidad Tcnica de Ambato");
}
public static void titulo(String mensaje) {
System.out.printf("%50S\n\n",
mensaje);
}
public static void tituloCentrado(String mensaje) {
int n = mensaje.length();
int x = (80 - n) / 2;
for (int i = 1; i <= x; i++) {
System.out.print(" ");
}
System.out.printf("%S\n\n",
mensaje);
}
public static void Realizado() {
System.out.println("Realizado por: Jair Vela");
}
public static void vendedor(String mensaje) {

System.out.printf("%s%s\n",
"Vendedor: ", mensaje);
}
public static double numeroAzar() {
double x;
x = Math.random();
return x;
}
public static double numeroAzar(int n) {
double x;
x = Math.random() * n;
return x;
}
public static double numeroAzar(int x1, int y1) {
double x;
x = (Math.random() * (y1 - x1)) + x1;
return x;
}
public static void Nombre() {
System.out.println("Realizado por: Jair Vela");
}
public static void TituloEnmarcado(String str) {
int n = str.length();
int x = (80 - n) / 2;
for (int i = 1; i <= x; i++) {
System.out.print(" ");
}
for (int i = 0; i < n + 4; i++) {
System.out.print("*");
}
System.out.println();
for (int i = 1; i <= x; i++) {
System.out.print(" ");
}
System.out.println("* " + str + " *");
for (int i = 1; i <= x; i++) {
System.out.print(" ");
}
for (int i = 0; i < n + 4; i++) {
System.out.print("*");
}
System.out.println();
}
public static void Dato(String mensaje) {
System.out.printf("%20s",
mensaje);
}

public static void Ecunciado(String mensaje) {


System.out.printf("%20s\n",
mensaje);
}
public static float RaizCuadrada(float a, float b) {
float c;
c = (float) Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
return c;
}
public static float UnNumero() {
float x;
x = (float) Math.random();
return x;
}
public static void escribirSalida(String mensaje, double mensaje2)
{
System.out.printf("%20s\n"
+ "%20.1f\n",
mensaje, mensaje2);
}
public static void escribirSalida2(String mensaje, double
mensaje2) {
System.out.printf("%20s"
+ "%.2f\n",
mensaje, mensaje2);
}
public static int Binario_Decimal(String num) {
int decimal = Integer.parseInt(num, 2);
return decimal;
}
public static String Binario_Octal(String num) {
int oc = Integer.parseInt(num, 2);
String octal = Integer.toOctalString(oc);
return octal;
}
public static String Binario_Hexadecimal(String num) {
int h = Integer.parseInt(num, 2);
String hexa = Integer.toHexString(h).toUpperCase();
return hexa;
}
public static String Decimal_Binario(int num) {
String bin = Integer.toBinaryString(num);
return bin;

}
public static String Decimal_Hexadecimal(int num) {
String hexa = Integer.toHexString(num).toUpperCase();
return hexa;
}
public static String Decimal_Octal(int num) {
String oct = Integer.toOctalString(num);
return oct;
}
public static int Hexadecimal_Decimal(String num) {
int dc = Integer.valueOf(num, 16);
return dc;
}
public static String Hexadecimal_Octal(String num) {
int oc = Integer.parseInt(num, 16);
String octal = Integer.toOctalString(oc).toUpperCase();
return octal;
}
public static String Hexadecimal_Binario(String num) {
int hc = Integer.parseInt(num, 16);
String binario = Integer.toBinaryString(hc);
return binario;
}
public static String Octal_Hexadecimal(String num) {
int b = Integer.parseInt(num, 8);
String hexa = Integer.toHexString(b).toLowerCase();
return hexa;
}
public static String Octal_Binario(String num) {
int b = Integer.parseInt(num, 8);
String binario = Integer.toBinaryString(b);
return binario;
}
public static int Octal_Decimal(String num) {
int b = Integer.parseInt(num, 8);
return b;
}
}

CAPTURAS DEL PROGRAJA EJECUTABLE

PROGRAMA MODO GRAFICO

/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Calculadora;
/**
*
* @author USUARIO
*/
import Claculadora.Conversion;
import Claculadora.Decimal;
import Claculadora.Hexadecimal;
import Claculadora.Octal;
import Claculadora.Sistema;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AppCalculadora implements ActionListener{


private JFrame frame;
private ButtonGroup btgSistema;
private JButton[] btnBotones;
private JRadioButton[] jrbSistemas;
private JTextField txtResultado;
private Container cpane;
int s;
Sistema d;
Conversion c;
public AppCalculadora() {
initComponents();
}
private void initComponents(){
s=10;
d = new Decimal();
c = new Conversion();
frame = new JFrame("Calculadora de Cuatro Sistemas") ;
btgSistema = new ButtonGroup();
btnBotones = new JButton[24];
jrbSistemas = new JRadioButton[4] ;
txtResultado = new JTextField(0);
int cont=0;
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new

JButton("D");
JButton("E");
JButton("F");
JButton("/");

btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
btnBotones[cont++]=new
cont=0;
jrbSistemas[cont++]=new
jrbSistemas[cont++]=new
jrbSistemas[cont++]=new
jrbSistemas[cont++]=new

JButton("A");
JButton("B");
JButton("C");
JButton("*");
JButton("7");
JButton("8");
JButton("9");
JButton("-");
JButton("4");
JButton("5");
JButton("6");
JButton("+");
JButton("1");
JButton("2");
JButton("3");
JButton("=");
JButton("0");
JButton("Acerca de ... ");
JButton("Cl");
JButton("CE");
JRadioButton("BIN");
JRadioButton("OCT");
JRadioButton("DEC");
JRadioButton("HEX");

for(int i=0;i<jrbSistemas.length;i++){
btgSistema.add(jrbSistemas[i]);
}
cpane = frame.getContentPane();
cpane.setLayout(null);
txtResultado.setBounds(10,10,260,30);
txtResultado.setEditable(false);
txtResultado.setBackground(Color.WHITE);
txtResultado.setHorizontalAlignment(JTextField.RIGHT);
cpane.add(txtResultado);
int x=10;
int y=45;
for(int i=0;i<jrbSistemas.length;i++){
x=i*70+10;
jrbSistemas[i].setBounds(x,y,60,20);
jrbSistemas[i].addActionListener(this);
cpane.add(jrbSistemas[i]);
}
x=10;
y=35;
for(int i=0;i<btnBotones.length;i++){
if(i%4==0){
x=10;

y+=35;
}else{
x+=70;
}
if(i!=20 && i!=21){
btnBotones[i].setBounds(x,y,50,30);
}else{
if(i==20)
btnBotones[i].setBounds(x,y,120,30);
if(i==21)
btnBotones[i].setBounds(10,y+35,260,30);
}
btnBotones[i].addActionListener(this);
cpane.add(btnBotones[i]);
}
jrbSistemas[2].setSelected(true);
elegirSistema(10);
frame.pack();
frame.setSize(290,350);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void elegirSistema(int sistema){
for(int i=0;i<btnBotones.length;i++){
if(!btnBotones[i].getText().equals("+")&&
!btnBotones[i].getText().equals("-")&&
!btnBotones[i].getText().equals("*")&&
!btnBotones[i].getText().equals("/")&&
!btnBotones[i].getText().equals("=")){
btnBotones[i].setEnabled(false);
}
}
switch(sistema){
case 2:
for(int i=0;i<btnBotones.length;i++){
if(!btnBotones[i].getText().equals("A")&&
!btnBotones[i].getText().equals("B")&&
!btnBotones[i].getText().equals("C")&&
!btnBotones[i].getText().equals("D")&&
!btnBotones[i].getText().equals("E")&&
!btnBotones[i].getText().equals("F")&&
!btnBotones[i].getText().equals("9")&&
!btnBotones[i].getText().equals("8")&&
!btnBotones[i].getText().equals("7")&&
!btnBotones[i].getText().equals("6")&&
!btnBotones[i].getText().equals("5")&&

!btnBotones[i].getText().equals("4")&&
!btnBotones[i].getText().equals("3")&&
!btnBotones[i].getText().equals("2")){
btnBotones[i].setEnabled(true);
}
}
break;
case 8:
for(int i=0;i<btnBotones.length;i++){
if(!btnBotones[i].getText().equals("A")&&
!btnBotones[i].getText().equals("B")&&
!btnBotones[i].getText().equals("C")&&
!btnBotones[i].getText().equals("D")&&
!btnBotones[i].getText().equals("E")&&
!btnBotones[i].getText().equals("F")&&
!btnBotones[i].getText().equals("9")&&
!btnBotones[i].getText().equals("8")){
btnBotones[i].setEnabled(true);
}
}
break;
case 10:
for(int i=0;i<btnBotones.length;i++){
if(!btnBotones[i].getText().equals("A")&&
!btnBotones[i].getText().equals("B")&&
!btnBotones[i].getText().equals("C")&&
!btnBotones[i].getText().equals("D")&&
!btnBotones[i].getText().equals("E")&&
!btnBotones[i].getText().equals("F")){
btnBotones[i].setEnabled(true);
}
}
break;
case 16:
for(int i=0;i<btnBotones.length;i++){
btnBotones[i].setEnabled(true);
}
break;
}
txtResultado.setText(c.combertirSistema(s, sistema,
txtResultado.getText()));
s=sistema;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
AppCalculadora c = new AppCalculadora();
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Cl")||
e.getActionCommand().equals("CE")){

txtResultado.setText("");
}
if(e.getActionCommand().equals("BIN")){
elegirSistema(2);
d = new Binario();
}
if(e.getActionCommand().equals("OCT")){
elegirSistema(8);
d = new Octal();
}
if(e.getActionCommand().equals("DEC")){
elegirSistema(10);
d = new Decimal();
}
if(e.getActionCommand().equals("HEX")){
elegirSistema(16);
d = new Hexadecimal();
}
if(!e.getActionCommand().equals("+")&&
!e.getActionCommand().equals("-")&&
!e.getActionCommand().equals("*")&&
!e.getActionCommand().equals("/")&&
!e.getActionCommand().equals("=")&&
!e.getActionCommand().equals("Acerca de ... ")&&
!e.getActionCommand().equals("Cl")&&
!e.getActionCommand().equals("CE")&&
!e.getActionCommand().equals("BIN")&&
!e.getActionCommand().equals("OCT")&&
!e.getActionCommand().equals("DEC")&&
!e.getActionCommand().equals("HEX")){
txtResultado.setText(txtResultado.getText()
+e.getActionCommand());
}
if(e.getActionCommand().equals("+")||
e.getActionCommand().equals("-")||
e.getActionCommand().equals("*")||
e.getActionCommand().equals("/")){
d.setOperacion(e.getActionCommand().charAt(0));
switch(s){
case 2:
d.establecerNumeroA(txtResultado.getText());
break;
case 8:
d.setNumeroA(c.fromOctal(txtResultado.getText()));
break;
case 10:
d.setNumeroA(Integer.parseInt(txtResultado.getText()));
break;
case 16:
d.setNumeroA(c.fromHexadecimal(txtResultado.getText()));
break;

}
txtResultado.setText("");
}
if(e.getActionCommand().equals("=")){
switch(s){
case 2:
d.setNumeroB(c.fromBinario(txtResultado.getText()));
break;
case 8:
d.setNumeroB(c.fromOctal(txtResultado.getText()));
break;
case 10:
d.setNumeroB(Integer.parseInt(txtResultado.getText()));
break;
case 16:
d.setNumeroB(c.fromHexadecimal(txtResultado.getText()));
break;
}
switch(d.getOperacion()){
case '+':
d.suma();
break;
case '-':
d.resta();
break;
case '*':
d.multiplicacion();
break;
case '/':
d.division();
break;
}
switch(s){
case 2:
txtResultado.setText(c.toBinario(d.getResultado()));
break;
case 8:
txtResultado.setText(c.toOctal(d.getResultado()));
break;
case 10:
txtResultado.setText(String.valueOf(d.getResultado()));
break;
case 16:
txtResultado.setText(c.toHexadecimal(d.getResultado()));
break;
}
}
if(e.getActionCommand().equals("Acerca de ... ")){

JOptionPane.showMessageDialog(null, "Calculadora de
ejemplo");
}
}
}

METODOS UTILIZADOS
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Calculadora;
import Claculadora.Sistema;
/**
*
* @author USUARIO
*/
public class Binario extends Sistema{
public Binario() {
this.base=2;
}
}

CONVERSION
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Claculadora;
/**
*
* @author USUARIO
*/
public class Conversion {
public Integer fromHexadecimal(String cad) {

return Integer.parseInt(cad, 16);


}
public Integer fromOctal(String cad) {
return Integer.parseInt(cad, 8);
}
public Integer fromBinario(String cad) {
return Integer.parseInt(cad, 2);
}
public String toHexadecimal(int i) {
return Integer.toHexString(i);
}
public String toOctal(int i) {
return Integer.toOctalString(i);
}
public String toBinario(int i) {
return Integer.toBinaryString(i);
}
public String combertirSistema(int actual, int nuevo, String cad)
{
String res = "";
int num = 0;
if (cad.length() > 0) {
switch (actual) {
case 2:
num = fromBinario(cad);
break;
case 8:
num = fromOctal(cad);
break;
case 10:
num = Integer.parseInt(cad, 10);
break;
case 16:
num = fromHexadecimal(cad);
break;
}
switch (nuevo) {
case 2:
res = toBinario(num);
break;
case 8:
res = toOctal(num);
break;
case 10:
res = Integer.toString(num);
break;
case 16:
res = toHexadecimal(num);

break;
}
}
return res;
}
}

DECIMAL
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Claculadora;
/**
*
* @author USUARIO
*/
public class Decimal extends Sistema{
public static String Decimal_Hexadecimal(int num) {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
public static String Decimal_Octal(int num) {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
public Decimal() {
this.base=10;
}
}

HEXADECIMAL
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Claculadora;
/**
*
* @author USUARIO
*/
public class Hexadecimal extends Sistema{
public Hexadecimal() {
this.base=16;

}
}

OCTAL
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Claculadora;
/**
*
* @author USUARIO
*/
public class Octal extends Sistema{
public Octal() {
this.base=8;
}
}

SISTEMA
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Claculadora;
/**
*
* @author USUARIO
*/
abstract public class Sistema {
protected int numeroA;
protected int numeroB;
protected int resultado;
protected char operacion;
protected int base;
public Sistema() {
this.numeroA =
this.numeroB =
this.resultado
this.operacion
}

0;
0;
= 0;
= ' ';

public void setNumeroA(int n){


this.numeroA = n;
}

public void setNumeroB(int n){


this.numeroB = n;
}
public void setResultado(int n){
this.resultado = n;
}
public void setOperacion(char o){
this.operacion = o;
}
public int getNumeroA(){
return this.numeroA;
}
public int getNumeroB(){
return this.numeroB;
}
public int getResultado(){
return this.resultado;
}
public char getOperacion(){
return this.operacion;
}
public void suma(){
this.resultado = this.numeroA
}
public void resta(){
this.resultado = this.numeroA
}
public void multiplicacion(){
this.resultado = this.numeroA
}
public void division(){
this.resultado = this.numeroA
}

+ this.numeroB;
- this.numeroB;
* this.numeroB;
/ this.numeroB;

public void establecerNumeroA(String a){


int n = Integer.parseInt(a,base);
this.setNumeroA(n);
}
public void establecerNumeroB(String b){
int n = Integer.parseInt(b,base);
this.setNumeroB(n);
}
public String retornarNumeroA(){
String cad="";
switch(base){
case 2:

cad = Integer.toBinaryString(numeroA);
break;
case 8:
cad = Integer.toOctalString(numeroA);
break;
case 10:
cad = String.valueOf(numeroA);
break;
case 16:
cad = Integer.toHexString(numeroA);
break;
}
return cad;
}
public String retornarNumeroB(){
String cad="";
switch(base){
case 2:
cad = Integer.toBinaryString(numeroB);
break;
case 8:
cad = Integer.toOctalString(numeroB);
break;
case 10:
cad = String.valueOf(numeroB);
break;
case 16:
cad = Integer.toHexString(numeroB);
break;
}
return cad;
}
public String retornarResultado(){
String cad="";
switch(base){
case 2:
cad = Integer.toBinaryString(resultado);
break;
case 8:
cad = Integer.toOctalString(resultado);
break;
case 10:
cad = String.valueOf(resultado);
break;
case 16:
cad = Integer.toHexString(resultado);
break;
}
return cad;
}
}

PROGRAMA EJECUTABLE

You might also like