Skip to main content

Java Code For a Simple Calculator

Java Code For a Simple Calculator

Code:
public class JavaCalculator extends javax.swing.JFrame {

    double firstNum;
    double secondNum;
    double total;
    double plusminus;

    int plusClick;
    int minusClick;
    int multiplyClick;
    int devideClick;
    int decimalClick;


 
    public JavaCalculator() {
        initComponents();
    }


 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    display.setText(display.getText()+jButton1.getText());
    }                                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     display.setText(display.getText()+jButton2.getText());
    }                                      

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     display.setText(display.getText()+jButton3.getText());
    }                                      

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     display.setText(display.getText()+jButton4.getText());
    }                                      

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     display.setText(display.getText()+jButton5.getText());
    }                                      

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     display.setText(display.getText()+jButton6.getText());
    }                                      

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     display.setText(display.getText()+jButton7.getText());
    }                                      

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     display.setText(display.getText()+jButton8.getText());
    }                                      

    private void Button9ActionPerformed(java.awt.event.ActionEvent evt) {                                      
     display.setText(display.getText()+Button9.getText());
    }                                      

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     display.setText(display.getText()+jButton10.getText());
    }                                        

    private void decimalActionPerformed(java.awt.event.ActionEvent evt) {                                      
     if(decimalClick==0){
        display.setText(display.getText()+decimal.getText());
        decimalClick=1;
    }                                      
    }
    private void clearActionPerformed(java.awt.event.ActionEvent evt) {                                    
     display.setText("");
     decimalClick=0;
    }                                    

    private void posnegActionPerformed(java.awt.event.ActionEvent evt) {                                      
    plusminus=(Double.parseDouble(String.valueOf(display.getText())));
    plusminus=plusminus*(-1);
    display.setText(String.valueOf(plusminus));
    }                                    

    private void plusActionPerformed(java.awt.event.ActionEvent evt) {                                    
     firstNum=(Double.parseDouble(String.valueOf(display.getText())));
     display.setText("");
     plusClick=1;
     decimalClick=0;
    }                                  

    private void minusActionPerformed(java.awt.event.ActionEvent evt) {                                    
     firstNum=(Double.parseDouble(String.valueOf(display.getText())));
     display.setText("");
     minusClick=1;
     decimalClick=0;
    }                                    

    private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {                                        
     firstNum=(Double.parseDouble(String.valueOf(display.getText())));
     display.setText("");
     multiplyClick=1;
     decimalClick=0;
    }                                      

    private void divideActionPerformed(java.awt.event.ActionEvent evt) {                                      
     firstNum=(Double.parseDouble(String.valueOf(display.getText())));
     display.setText("");
     devideClick=1;
     decimalClick=0;
    }                                    

    private void equalsActionPerformed(java.awt.event.ActionEvent evt) {                                      
     secondNum=(Double.parseDouble(String.valueOf(display.getText())));
     if(plusClick>0){
     total = firstNum + secondNum;
     display.setText(String.valueOf(total));
     firstNum = 0;
     secondNum = 0;
     plusClick = 0;
     }


     if(minusClick>0){
     total = firstNum - secondNum;
     display.setText(String.valueOf(total));
     firstNum = 0;
     secondNum = 0;
     minusClick = 0;

    }                                    
   
     if(multiplyClick>0){
     total = firstNum * secondNum;
     display.setText(String.valueOf(total));
     firstNum = 0;
     secondNum = 0;
     multiplyClick = 0;
   
    }

     if(devideClick>0){
     total = firstNum / secondNum;
     display.setText(String.valueOf(total));
     firstNum = 0;
     secondNum = 0;
     devideClick = 0;

    }
    }
   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JavaCalculator().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JButton Button9;
    private javax.swing.JButton clear;
    private javax.swing.JButton decimal;
    private javax.swing.JTextField display;
    private javax.swing.JButton divide;
    private javax.swing.JButton equals;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton minus;
    private javax.swing.JButton multiply;
    private javax.swing.JButton plus;
    private javax.swing.JButton posneg;
    // End of variables declaration                  

}


Output:



<<<Go To TEACH-U-TECH (Click Here)

Comments

Popular posts from this blog

MATCH REPORT: BNI INDONESIA ALL-STARS 1 CHELSEA 8

MATCH REPORT: BNI INDONESIA ALL-STARS 1 CHELSEA 8 Summary  An enormous and vociferous crowd of Chelsea fans came to see the team on our Indonesian debut and they were rewarded with goals galore, including some quality strikes, as the Asia tour ended in style. The Chelsea goals were split equally between the halves - Eden Hazard, Ramires, Demba Ba and John Terry the scorers before half-time, with Ramires again finding the net after the interval and Romelu Lukaku scoring two as well. Bertrand Traore added to his goal in Malaysia shortly after coming on at half-time. The sole goal from the Indonesian All-Stars was an own-goal, conceded when we were already 8-0 up. Gary Cahillwas the one Chelsea man to play 90 minutes. Chelsea  (4-3-3): Blackman (Schwarzer h-t); Wallace (Ivanovic h-t), Cahill, Terry (c) (Kalas h-t), Bertrand (Cole h-t);Ramires (Feruz 59), Chalobah (Essien h-t), Van Ginkel (McEachran 59); Moses (Traore h-t), Ba (Lukaku h-t), Hazard (Piazon 59). Scorers

ARJUN COOM's Kabhi-Kabhi [feat. Shivali & Natasha] Song Lyrics

MUSIC Written, produced and performed by Arjun Hindi chorus performed by Shivali and Natasha Originally written by Khayyam  Mixing - Elliot Bradley, Soho Studios ------------------------------------------------------------------------------------- Chorus: Kabhi Kabhi Mere Dil Mein Khayal ata Hai Kabhi Kabhi Mere Dil Mein Khayal Ata Hai I feel like a plane on a runaway about to lift off I feel like an ocean so deep, deep as my love You should know that it's you that I've chosen and I'm ready to give you my all Yes I'm right open Come and get, come and get it. You know that I could be. The one that makes you complete 'Cause I've fallen head over heels, and you're the only one. Chorus: Kabhi Kabhi Mere Dil Mein Khayal Aata Hai Kabhi Kabhi Mere Dil Mein Khayal Aata Hai Kii Jaise Tujhko Banaya Gyaa Hai Mere Liye Kii Jaise Tujhko Banaya Gyaa Hai Mere Liye I used to be lost in the darkness, but you were my star. I feel like a king when I'm in your arms, and

Calculator - Netbeans Java Code

Run Shot of Calculator Java Code: public class Calculator { public static void main(String[] args) { boolean go = true; //sets up loop while(go) //creates loop to top { System.out.println("Hello this is my calculator!"); System.out.println("To add, type a, to subtract, type s."); System.out.println("To multiply, type m, to divide, type d."); Scanner scan = new Scanner(System.in); //sets up scanners Scanner scan1 = new Scanner(System.in); String action = scan.nextLine(); //tells comp. to take user input if("a".equals(action)) //addition { System.out.println("Now type in the first number you would like to add."); int add1 = scan.nextInt(); System.out.println("Now type the second number."); int add2 = scan.nextInt();