Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Coding Releases
You last visited: Today at 14:36

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Source Code] number system calculator [Java]

Discussion on [Source Code] number system calculator [Java] within the Coding Releases forum part of the Coders Den category.

Reply
 
Old   #1
 
NikM's Avatar
 
elite*gold: 0
Join Date: Aug 2010
Posts: 972
Received Thanks: 1,583
Lightbulb [Source Code] number system calculator [Java]

Hey guys.
Today im going to release my number system calculator.
I hope it is pretty useful for you

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;
    Dimension ScreenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
    int iWindowX = 400;
    int iWindowY = 210;
    
    //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 (String[] args)
    {
        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 (iWindowX, iWindowY);
        setLocation (((ScreenSize.width - iWindowX) / 2), ((ScreenSize.height - iWindowY) / 2)); //Mitte des Bildschirms
        
        //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          (Calculator (BinaryTextField.getText (),  8, 2.0));
            DecimalTextField.setText        (Calculator (BinaryTextField.getText (), 10, 2.0));
            DuoDecimalTextField.setText     (Calculator (BinaryTextField.getText (), 12, 2.0));
            HexaDecimalTextField.setText    (Calculator (BinaryTextField.getText (), 16, 2.0));
        }
        
        else if (e.getSource () == OctalButton)
        {
            BinaryTextField.setText         (Calculator (OctalTextField.getText (),  2, 8.0));
            DecimalTextField.setText        (Calculator (OctalTextField.getText (), 10, 8.0));
            DuoDecimalTextField.setText     (Calculator (OctalTextField.getText (), 12, 8.0));
            HexaDecimalTextField.setText    (Calculator (OctalTextField.getText (), 16, 8.0));
        }
        
        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         (Calculator (DuoDecimalTextField.getText (),  2, 12.0));
            OctalTextField.setText          (Calculator (DuoDecimalTextField.getText (),  8, 12.0));          
            DecimalTextField.setText        (Calculator (DuoDecimalTextField.getText (), 10, 12.0));
            HexaDecimalTextField.setText    (Calculator (DuoDecimalTextField.getText (), 16, 12.0));
        }
        
        else if (e.getSource () == HexaDecimalButton)
        {
            BinaryTextField.setText         (Calculator (HexaDecimalTextField.getText (),  2, 16.0));
            OctalTextField.setText          (Calculator (HexaDecimalTextField.getText (),  8, 16.0));          
            DecimalTextField.setText        (Calculator (HexaDecimalTextField.getText (), 10, 16.0));
            DuoDecimalTextField.setText     (Calculator (HexaDecimalTextField.getText (), 12, 16.0));
        }
    } //End ActionListener
    
    //Wandelt einen ZahlenTyp in einen anderen ZahlenTyp
    private String Calculator (String sCount, int iSize, double dSize)
    {
        //Locale Variablen
        String sNewCount = "";
        int iLength = sCount.length ();
        char [] chCount = sCount.toCharArray ();
        int iDecimal = 0;
        String sDecimal = "";
        double iCounter = 0.0;
        int iTemp = 0;
        
        //Jeden char entsprechend umschreiben
        for (int i = iLength - 1 ; i >= 0 ; i--)
        {
            switch (chCount [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)(chCount [i]) - 48; //Cast Wert entspricht nicht dem richtigen!
            }
            
            iDecimal += (iTemp * (int)(Math.pow (dSize, iCounter)));
            iCounter ++;
        }
        
        sDecimal += iDecimal;
        
        if (iSize != 10)
        {
            sNewCount = DecimalWandler (sDecimal, iSize);
        }
        
        else
        {
            sNewCount = sDecimal;
        }
        
        return sNewCount;
    } //End BinaryWandler
    
    //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
} //End cZahlenWandler Class
Credits: NikM
NikM is offline  
Old 12/18/2011, 22:42   #2
 
Dr. Coxxy's Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
theres a release section in this forum:
Dr. Coxxy is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
[Source Code] Zahlensystem Rechner [Java]
11/07/2011 - General Coding - 2 Replies
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 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.Object.*;
[DEV] Java source
01/22/2010 - CO2 Private Server - 1 Replies
Well since the only language (it seems) in the co section is C# Ive decided to start a project This project will be coded in java and since im ALRIGHT in java i could probaly use some help Ill need approximatly 2 more people to help with this project you can add me on msn at [email protected] If you have no clue how to code in java do not bother adding me
Sro Traderun Calculator v 1.3(Soure Code included)
04/22/2008 - SRO Hacks, Bots, Cheats & Exploits - 15 Replies
What does it do it calculate's youre profit, loses can add horse, pots loses See Map added Chinese side and European side Waiting on how much trade goods i must add for 3,4,5 stars ... ----> Got ideas for this program plz post them xD <----



All times are GMT +2. The time now is 14:36.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.