Professional Documents
Culture Documents
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.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 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;
}
}
/*
* 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.*;
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) {
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;
= ' ';
+ this.numeroB;
- this.numeroB;
* this.numeroB;
/ this.numeroB;
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