[Source Code] Zahlensystem Rechner [Java]

11/06/2011 20:23 NikM#1
Schaut es euch an und ich wäre froh wenn ihr Verbesserungsvorschläge habt
Auch wenn ihr Fragen habt bin ich natürlich bereit diese zu beantworten.
Eine Erklärung zu dem Code befindet sich in den Kommentaren.
Vllt kann es jemand gebrauchen, vllt aber auch nicht :P

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Object.*;
import java.lang.Math.*;

//Zahlenrechner (Binär - Octal - Dezimal - DuoDezimal - HexaDezimal)
class cZahlenRechner extends JFrame implements ActionListener
{
    //globale Variablen
    JLabel BinaryLabel, OctalLabel, DecimalLabel, DuoDecimalLabel, HexaDecimalLabel = null;
    JButton BinaryButton, OctalButton, DecimalButton, DuoDecimalButton, HexaDecimalButton = null;
    JTextField BinaryTextField, OctalTextField, DecimalTextField, DuoDecimalTextField, HexaDecimalTextField = null;
    
    //Main Funktion
    //Hier wird lediglich eine neue Instanz auf die ZahlenRechner Klasse erstellt (dadurch der Konstruktor automatisch aufgerufen
    //Keine Parameter da für das Programm keine Variablen schon vor Programmstart vordefiniert werden müssen
    public static void main ()
    {
        new cZahlenRechner (); //Instanz auf die eigene Klasse weil die main static ist
    } //End main
    
    //Konstruktor
    //Hier werden die einzelnen GUI Elemente erzeugt
    cZahlenRechner ()
    { 
        //Hier werden die Wichtigsten Variablen für das Fenster gesetzt
        setLayout (null);
        setSize (400, 210);
        setLocation (0, 0);
        
        //Labels erstellen und adden
        BinaryLabel = new JLabel ("Binary:");
        BinaryLabel.setBounds (10, 10, 80, 20);
        add (BinaryLabel);
        
        OctalLabel = new JLabel ("Octal:");
        OctalLabel.setBounds (10, 40, 80, 20);
        add (OctalLabel);
        
        DecimalLabel = new JLabel ("Decimal:");
        DecimalLabel.setBounds (10, 70, 80, 20);
        add (DecimalLabel);
        
        DuoDecimalLabel = new JLabel ("DuoDecimal:");
        DuoDecimalLabel.setBounds (10, 100, 80, 20);
        add (DuoDecimalLabel);
        
        HexaDecimalLabel = new JLabel ("HexaDecimal:");
        HexaDecimalLabel.setBounds (10, 130, 80, 20);
        add (HexaDecimalLabel);
        
        //TextFelder erstellen und adden
        BinaryTextField = new JTextField ("");
        BinaryTextField.setBounds (110, 10, 150, 20);
        add (BinaryTextField);
        
        OctalTextField = new JTextField ("");
        OctalTextField.setBounds (110, 40, 150, 20);
        add (OctalTextField);
        
        DecimalTextField = new JTextField ("");
        DecimalTextField.setBounds (110, 70, 150, 20);
        add (DecimalTextField);
        
        DuoDecimalTextField = new JTextField ("");
        DuoDecimalTextField.setBounds (110, 100, 150, 20);
        add (DuoDecimalTextField);
        
        HexaDecimalTextField = new JTextField ("");
        HexaDecimalTextField.setBounds (110, 130, 150, 20);
        add (HexaDecimalTextField);
        
        //Button erstellen und adden
        BinaryButton = new JButton ("Rechne!");
        BinaryButton.addActionListener (this);
        BinaryButton.setBounds (280, 10, 80, 20);
        add (BinaryButton);
        
        OctalButton = new JButton ("Rechne!");
        OctalButton.addActionListener (this);
        OctalButton.setBounds (280, 40, 80, 20);
        add (OctalButton);
        
        DecimalButton = new JButton ("Rechne!");
        DecimalButton.addActionListener (this);
        DecimalButton.setBounds (280, 70, 80, 20);
        add (DecimalButton);
        
        DuoDecimalButton = new JButton ("Rechne!");
        DuoDecimalButton.addActionListener (this);
        DuoDecimalButton.setBounds (280, 100, 80, 20);
        add (DuoDecimalButton);
        
        HexaDecimalButton = new JButton ("Rechne!");
        HexaDecimalButton.addActionListener (this);
        HexaDecimalButton.setBounds (280, 130, 80, 20);
        add (HexaDecimalButton);
        
        //sichtbar machen
        setVisible (true);
    } //End Konstruktor

    //Der ActionListener
    //Diese Funktion reagiert wenn ein Button gedrückt wird
    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource () == BinaryButton)
        {
            OctalTextField.setText          (BinaryWandler (BinaryTextField.getText (),  8));
            DecimalTextField.setText        (BinaryWandler (BinaryTextField.getText (), 10));
            DuoDecimalTextField.setText     (BinaryWandler (BinaryTextField.getText (), 12));
            HexaDecimalTextField.setText    (BinaryWandler (BinaryTextField.getText (), 16));
        }
        
        else if (e.getSource () == OctalButton)
        {
            BinaryTextField.setText         (OctalWandler (OctalTextField.getText (),  2));
            DecimalTextField.setText        (OctalWandler (OctalTextField.getText (), 10));
            DuoDecimalTextField.setText     (OctalWandler (OctalTextField.getText (), 12));
            HexaDecimalTextField.setText    (OctalWandler (OctalTextField.getText (), 16));
        }
        
        else if (e.getSource () == DecimalButton)
        {
            BinaryTextField.setText         (DecimalWandler (DecimalTextField.getText (),  2));
            OctalTextField.setText          (DecimalWandler (DecimalTextField.getText (),  8));
            DuoDecimalTextField.setText     (DecimalWandler (DecimalTextField.getText (), 12));
            HexaDecimalTextField.setText    (DecimalWandler (DecimalTextField.getText (), 16));
        }
        
        else if (e.getSource () == DuoDecimalButton)
        {
            BinaryTextField.setText         (DuoDecimalWandler (DuoDecimalTextField.getText (),  2));
            OctalTextField.setText          (DuoDecimalWandler (DuoDecimalTextField.getText (),  8));           
            DecimalTextField.setText        (DuoDecimalWandler (DuoDecimalTextField.getText (), 10));
            HexaDecimalTextField.setText    (DuoDecimalWandler (DuoDecimalTextField.getText (), 16));
        }
        
        else if (e.getSource () == HexaDecimalButton)
        {
            BinaryTextField.setText         (HexaDecimalWandler (HexaDecimalTextField.getText (),  2));
            OctalTextField.setText          (HexaDecimalWandler (HexaDecimalTextField.getText (),  8));           
            DecimalTextField.setText        (HexaDecimalWandler (HexaDecimalTextField.getText (), 10));
            DuoDecimalTextField.setText     (HexaDecimalWandler (HexaDecimalTextField.getText (), 12));
        }
    } //End ActionListener
    
    //Wandelt eine BinaryZahl in einen anderen ZahlenTyp
    private String BinaryWandler (String sBinary, int iSize)
    {
        //Locale Variablen
        String sNewCount = "";
        int iLength = sBinary.length ();
        char [] chBinary = sBinary.toCharArray ();
        int iDecimal = 0;
        String sDecimal = "";
        double iCounter = 0.0;
        
        for (int i = iLength - 1 ; i >= 0 ; i--)
        {
            if (chBinary [i] == '1')
            {
                iDecimal += (int)(Math.pow (2.0, iCounter));
            }
            iCounter ++;
        }
        
        sDecimal += iDecimal;
        
        if (iSize != 10)
        {
            sNewCount = DecimalWandler (sDecimal, iSize);
        }
        
        else
        {
            sNewCount = sDecimal;
        }
        
        return sNewCount;
    } //End BinaryWandler
    
    //Wandelt eine OctalZahl in einen anderen ZahlenTyp
    private String OctalWandler (String sOctal, int iSize)
    {
        //Locale Variablen
        String sNewCount = "";
        int iLength = sOctal.length ();
        char [] chOctal = sOctal.toCharArray ();
        int iDecimal = 0;
        String sDecimal = "";
        double iCounter = 0.0;
        int iTemp = 0;
        
        for (int i = iLength - 1 ; i >= 0 ; i--)
        {
         
            iTemp = (int)(chOctal [i]) - 48; //Cast Wert entspricht nicht dem richtigen!
            iDecimal += (int)(iTemp * (Math.pow (8.0, iCounter)));
      
            iCounter ++;
        }
        
        sDecimal += iDecimal;
      
        if (iSize != 10)
        {
            sNewCount = DecimalWandler (sDecimal, iSize);
        }
        
        else
        {
            sNewCount = sDecimal;
        }
  
        return sNewCount;
    } //End OctalWandler
    
    //Wandelt eine DezimalZahl in einen anderen ZahlenTyp
    private String DecimalWandler (String sDecimal, int iSize)
    {
        //Locale Variablen
        String sNewCount = "";
        int iDecimal = Integer.parseInt (sDecimal);
        int [] iToChange = new int [128];
        int iRest = 0;
        int i = 0;
        
        do 
        {
            iRest = 0;
            iRest = iDecimal%iSize;
            iToChange [i] = iRest;
            iDecimal /= iSize;
            i++;
        } while (iDecimal != 0);
        
        for (int j = i - 1 ; j >= 0 ; j--)
        {
            switch (iToChange [j])
            {
                case (10): sNewCount += 'A'; break;
                case (11): sNewCount += 'B'; break;
                case (12): sNewCount += 'C'; break;
                case (13): sNewCount += 'D'; break;
                case (14): sNewCount += 'E'; break;
                case (15): sNewCount += 'F'; break;
                default:   sNewCount += iToChange [j];
            }
        }
  
        return sNewCount;
    } //End DecimalWandler
    
    //Wandelt eine DuoDezimalZahl in einen anderen ZahlenTyp
    private String DuoDecimalWandler (String sDuoDecimal, int iSize)
    {
         //Locale Variablen
        String sNewCount = "";
        int iLength = sDuoDecimal.length ();
        char [] chDuo = sDuoDecimal.toCharArray ();
        int iDecimal = 0;
        String sDecimal = "";
        double iCounter = 0.0;
        int iTemp = 0;
        
        for (int i = iLength - 1 ; i >= 0 ; i--)
        {
            switch (chDuo [i])
            {
                case ('A' | 'a'): iTemp = 10; break;
                case ('B' | 'b'): iTemp = 11; break;
                default: iTemp = (int)(chDuo [i]) - 48; //Cast Wert entspricht nicht dem richtigen!
            }
            
            iDecimal += (iTemp * (int)(Math.pow (12.0, iCounter)));
   
            iCounter ++;
        }
        
        sDecimal += iDecimal;
        
        if (iSize != 10)
        {
            sNewCount = DecimalWandler (sDecimal, iSize);
        }
        
        else
        {
            sNewCount = sDecimal;
        }
       
        return sNewCount;
    } //End DuoDecimalWandler
    
    //Wandelt eine HexaDezimalZahl in einen anderen ZahlenTyp
    private String HexaDecimalWandler (String sHexaDecimal, int iSize)
    {
         //Locale Variablen
        String sNewCount = "";
        int iLength = sHexaDecimal.length ();
        char [] chHexa = sHexaDecimal.toCharArray ();
        int iDecimal = 0;
        String sDecimal = "";
        double iCounter = 0.0;
        int iTemp = 0;
        
        for (int i = iLength - 1 ; i >= 0 ; i--)
        {
            switch (chHexa [i])
            {
                case ('A' | 'a'): iTemp = 10; break;
                case ('B' | 'b'): iTemp = 11; break;
                case ('C' | 'c'): iTemp = 12; break;
                case ('D' | 'd'): iTemp = 13; break;
                case ('E' | 'e'): iTemp = 14; break;
                case ('F' | 'f'): iTemp = 15; break;
                default: iTemp = (int)(chHexa [i]) - 48; //Cast Wert entspricht nicht dem richtigen!
            }
            
            iDecimal += (iTemp * (int)(Math.pow (16.0, iCounter)));
   
            iCounter ++;
        }
        
        sDecimal += iDecimal;
        
        if (iSize != 10)
        {
            sNewCount = DecimalWandler (sDecimal, iSize);
        }
        
        else
        {
            sNewCount = sDecimal;
        }
       
        return sNewCount;
    } //End HexaDecimalWandler
} //End cZahlenWandler Class
Credits: NikM
11/06/2011 23:47 Obilee#2
Würd für die GUI Elemente lieber entsprechende Layoutmanager benutzen anstatt alle Koordinaten etc. per Hand zu positionieren.

Ansonsten gibts nicht viel zu meckern.
11/07/2011 01:51 yannickminecraft#3
DecimalWandler();

Code:
for (int j = i - 1 ; j >= 0 ; j--)
{
switch (iToChange [j])
{
case (10): sNewCount += 'A'; break;
case (11): sNewCount += 'B'; break;
case (12): sNewCount += 'C'; break;
case (13): sNewCount += 'D'; break;
case (14): sNewCount += 'E'; break;
case (15): sNewCount += 'F'; break;
default: sNewCount += iToChange [j];
}
}
zu

Code:
        for (int j = i - 1 ; j >= 0 ; j--)
        {
            if (iToChange [j] > 9 || iToChange [j] < 16 )
                sNewCount += chr(55 + iToChange [j]); //Wandelt Ascii nummer 65 + die im array in A, B, C, D, E, F (A = 65, B = 66 usw..)
            else
                sNewCount += iToChange[j];
        }
Ka obs chr() in Java gibt ^^