Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create Java programs for the following two problems. 1.Write a Java application

ID: 638620 • Letter: C

Question

Create Java programs for the following two problems.

1.Write a Java application to display the following GUI. At this point you are only implementing the display. We are not ready to make the calculator actually do any calculations!

This program has the following requirements:

1.The size of the calculator is 250 x 250 pixels.

2.The background and foreground color of the calculator buttons must alternate in a checker board pattern as shown above. You can choose any pair of colors for your foreground and background colors.

3.The buttons should have at least 5 pixels of space between them.

4.The text on the buttons should be SanSerif size 16 and be bold.

5.Your application should be implemented in a single class. The main method of the class does nothing more than create an object of the class. The constructor of the class creates and displays the GUI. The constructor may call other methods of the class if needed.

6.The class must inherit from JFrame as the following demonstrates:

public myGUI extends JFrame {

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class JavaCalculator extends JFrame {
    private JButton jbtNum1;
    private JButton jbtNum2;
    private JButton jbtNum3;
    private JButton jbtNum4;
    private JButton jbtNum5;
    private JButton jbtNum6;
    private JButton jbtNum7;
    private JButton jbtNum8;
    private JButton jbtNum9;
    private JButton jbtNum0;
    private JButton jbtEqual;
    private JButton jbtAdd;
    private JButton jbtSubtract;
    private JButton jbtMultiply;
    private JButton jbtDivide;
    private JButton jbtSolve;
    private JButton jbtClear;
    private double TEMP;
    private double SolveTEMP;
private JTextField jtfResult;

Boolean addBool = false ;
Boolean subBool = false ;
Boolean divBool = false ;
Boolean mulBool = false ;


String display = "";

public JavaCalculator() {
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(10,5));
    p1.add(jbtNum1 = new JButton("1"));
    p1.add(jbtNum2 = new JButton("2"));
    p1.add(jbtNum3 = new JButton("3"));
    p1.add(jbtNum4 = new JButton("4"));
    p1.add(jbtNum5 = new JButton("5"));
    p1.add(jbtNum6 = new JButton("6"));
    p1.add(jbtNum7 = new JButton("7"));
    p1.add(jbtNum8 = new JButton("8"));
    p1.add(jbtNum9 = new JButton("9"));
    p1.add(jbtNum0 = new JButton("0"));
    p1.add(jbtClear = new JButton("C"));
    
  
    JPanel p2 = new JPanel(new BorderLayout());
    p2.add(new JTextField(""), BorderLayout.NORTH);
    p2.add(p1, BorderLayout.CENTER);
  
    }

  

            JPanel p3 = new JPanel();
            p3.setLayout(new GridLayout(5, 1));
            p3.add(jbtAdd = new JButton("+"));
            p3.add(jbtSubtract = new JButton("-"));
            p3.add(jbtMultiply = new JButton("*"));
            p3.add(jbtDivide = new JButton("/"));
            p3.add(jbtSolve = new JButton("="));

    JPanel p = new JPanel();
    p.setLayout(new GridLayout());
    p.add(p2, BorderLayout.NORTH);
    p.add(p1, BorderLayout.SOUTH);
    p.add(p3, BorderLayout.EAST);


    add(p);

    jbtNum1.addActionListener(new ListenToOne());
    jbtNum2.addActionListener(new ListenToTwo());
    jbtNum3.addActionListener(new ListenToThree());
    jbtNum4.addActionListener(new ListenToFour());
    jbtNum5.addActionListener(new ListenToFive());
    jbtNum6.addActionListener(new ListenToSix());
    jbtNum7.addActionListener(new ListenToSeven());
    jbtNum8.addActionListener(new ListenToEight());
    jbtNum9.addActionListener(new ListenToNine());
    jbtNum0.addActionListener(new ListenToZero());

    jbtAdd.addActionListener(new ListenToAdd());
    jbtSubtract.addActionListener(new ListenToSubtract());
    jbtMultiply.addActionListener(new ListenToMultiply());
    jbtDivide.addActionListener(new ListenToDivide());
    jbtSolve.addActionListener(new ListenToSolve());
    jbtClear.addActionListener(new ListenToClear());

} //JavaCaluclator()

class ListenToClear implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        //display = jtfResult.getText();
        jtfResult.setText("");
        addBool = false ;
        subBool = false ;
        mulBool = false ;
        divBool = false ;

        TEMP = 0;
        SolveTEMP =0 ;
    }
}

class ListenToOne implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "1");
    }
}
class ListenToTwo implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "2");
    }
}
class ListenToThree implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "3");
    }
}
class ListenToFour implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "4");
    }
}
class ListenToFive implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "5");
    }
}
class ListenToSix implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "6");
    }
}
class ListenToSeven implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "7");
    }
}
class ListenToEight implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "8");
    }
}
class ListenToNine implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "9");
    }
}
class ListenToZero implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = jtfResult.getText();
        jtfResult.setText(display + "0");
    }
}

    class ListenToAdd implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        TEMP = Double.parseDouble(jtfResult.getText());
                    jtfResult.setText("");
                    addBool = true ;

    }
}
class ListenToSubtract implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        TEMP = Double.parseDouble(jtfResult.getText());
        jtfResult.setText("");
        subBool =true;
    }
}
class ListenToMultiply implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        TEMP = Double.parseDouble(jtfResult.getText());
        jtfResult.setText("");
        mulBool =true;

    }
}
class ListenToDivide implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        TEMP = Double.parseDouble(jtfResult.getText());
        jtfResult.setText("");
        divBool =true;
    }
}
class ListenToSolve implements ActionListener {
    public void actionPerformed(ActionEvent e) {
                    SolveTEMP = Double.parseDouble( jtfResult.getText() );
                    if ( addBool == true )
                        SolveTEMP = SolveTEMP + TEMP;

                    else if ( subBool == true )
                        SolveTEMP = SolveTEMP - TEMP;
                    else if ( mulBool == true )
                        SolveTEMP = SolveTEMP * TEMP;
                    else if ( divBool == true )
                        SolveTEMP = SolveTEMP / TEMP;
        jtfResult.setText( Double.toString( SolveTEMP ) );

        addBool = false ;
        subBool = false ;
        mulBool = false ;
        divBool = false ;


    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    JavaCalculator calc = new JavaCalculator();
  
    calc.setTitle("Calculator");
    calc.setSize(250, 250);
    calc.setLocationRelativeTo(null);
    calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    calc.setVisible(true);

} //JavaCalculator