Question

Please code the following Java App using Swing

No additional classes are required. With that in mind ...

Write a program that emulates a calculator. Create a Label with a title of "Super Calculator", a textbox, where the numbers are displayed, a series of buttons labeled as shown, and a “Clear” button.

As the user enters the numbers, pressing the number buttons, display the numbers right justified to the textbox. When the user press any one of the operators, +, -, x, -, and /, clear the text screen and save the number as the right operand, then enter the numbers for the right operand. When the = sign is pressed, compute, and display the answer to the text screen. The . (decimal point) is considered part of a number, indicating a floating point value.

The clear button clears the screen. It allows the user to re-enter another number.

Super Calculator Clear

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: My color choice is worst. Please change it as per your choice.

Code

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class SuperCalculator implements ActionListener
{
JTextField TD=new JTextField("");
String data="";
String tmp="";
double result=0;
int function = 0;//to keep track of +, -, *, /
JLabel jL=new JLabel("<html><font color='#00FF00' size=+4>Super Calculator</font></html>");
JButton bClear=new JButton("<html><font color='#808000' size=+2>Clear</font></html>");
JButton bExit=new JButton("<html><font color='#0000FF' size=+2>Exit</font></html>");

//buttons for GUI
JButton jb0=new JButton("<html><font color='#1bd11' size=+3>0</font></html>");
JButton jb1=new JButton("<html><font color='#1bd11' size=+3>1</font></html>");
JButton jb2=new JButton("<html><font color='#1bd11' size=+3>2</font></html>");
JButton jb3=new JButton("<html><font color='#1bd11' size=+3>3</font></html>");
JButton jb4=new JButton("<html><font color='#1bd11' size=+3>4</font></html>");
JButton jb5=new JButton("<html><font color='#1bd11' size=+3>5</font></html>");
JButton jb6=new JButton("<html><font color='#1bd11' size=+3>6</font></html>");
JButton jb7=new JButton("<html><font color='#1bd11' size=+3>7</font></html>");
JButton jb8=new JButton("<html><font color='#1bd11' size=+3>8</font></html>");
JButton jb9=new JButton("<html><font color='#1bd11' size=+3>9</font></html>");
JButton jb10=new JButton("<html><font color='#1bd11' size=+3>+</font></html>");
JButton jb11=new JButton("<html><font color='#1bd11' size=+3>-</font></html>");
JButton jb12=new JButton("<html><font color='#1bd11' size=+3>*</font></html>");
JButton jb13=new JButton("<html><font color='#1bd11' size=+3>/</font></html>");
JButton jb14=new JButton("<html><font color='#1bd11' size=+3>.</font></html>");
JButton jb15=new JButton("<html><font color='#0000FF' size=+3>=</font></html>");

//Main Form
JFrame jf=new JFrame("Java Form");

//for setting font for text field
Font font1 = new Font("SansSerif", Font.BOLD, 20);

//panel for command buttons
JPanel jp;


SuperCalculator()
    {
    //designing main window
    jf.setBounds(500,100,600,640); //left, top, width, height
    jf.setVisible(true);
    jf.setLayout(null); //i will place each control on a fixed position using setBounds
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jL.setBounds(150, 0, 390, 80);   //left, top, width, height
    jf.add(jL);    //add button to panel
    TD.setBounds(120, 80, 350, 40);
    jf.add(TD);
    TD.setHorizontalAlignment(SwingConstants.RIGHT);
    //remove this lin if you want to enter using keyboard
    TD.setEditable(false); //so that you can not enter anything in result text field using keyboard
    TD.setFont(font1);

    jp = new JPanel();
    jp.setLayout(null);
    jp.setBounds(150, 150, 315, 315);
    jp.setBackground(Color.CYAN); //update color, my color choice is bad

    //GUI Setup
    //setBounds is to set exact position -- left, top, width, height
    //.add is to add control on panel or frame
    jb0.setBounds(40, 40, 60, 60);
    jp.add(jb0);
    jb1.setBounds(100, 40, 60, 60);
    jp.add(jb1);
    jb2.setBounds(160, 40, 60, 60);
    jp.add(jb2);
    jb3.setBounds(220, 40, 60, 60);
    jp.add(jb3);
    jb4.setBounds(40, 100, 60, 60);
    jp.add(jb4);
    jb5.setBounds(100, 100, 60, 60);
    jp.add(jb5);
    jb6.setBounds(160, 100, 60, 60);
    jp.add(jb6);
    jb7.setBounds(220, 100, 60, 60);
    jp.add(jb7);
    jb8.setBounds(40, 160, 60, 60);
    jp.add(jb8);
    jb9.setBounds(100, 160, 60, 60);
    jp.add(jb9);
    jb10.setBounds(160, 160, 60, 60);
    jp.add(jb10);
    jb11.setBounds(220, 160, 60, 60);
    jp.add(jb11);
    jb12.setBounds(40, 220, 60, 60);
    jp.add(jb12);
    jb13.setBounds(100, 220, 60, 60);
    jp.add(jb13);
    jb14.setBounds(160, 220, 60, 60);
    jp.add(jb14);
    jb15.setBounds(220, 220, 60, 60);
    jp.add(jb15);

    bExit.setBounds(100, 500, 180, 50);
    jf.add(bExit);
    bClear.setBounds(320, 500, 180, 50);
    jf.add(bClear);

    jf.add(jp);

    //add event on all the buttons
    bClear.addActionListener(this);
    bExit.addActionListener(this);

    jb0.addActionListener(this);
    jb1.addActionListener(this);
    jb2.addActionListener(this);
    jb3.addActionListener(this);
    jb4.addActionListener(this);
    jb5.addActionListener(this);
    jb6.addActionListener(this);
    jb7.addActionListener(this);
    jb8.addActionListener(this);
    jb9.addActionListener(this);
    jb10.addActionListener(this);
    jb11.addActionListener(this);
    jb12.addActionListener(this);
    jb13.addActionListener(this);
    jb14.addActionListener(this);
    jb15.addActionListener(this);

    }
    //this function triggers when user click on any button
    //using if condition you can track which button is pressed
    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getSource()==bExit) //means Exit button is pressed
       {
       System.exit(0);   //exit condition
       }
    else if(ae.getSource()==bClear)
       {
       //clear all
       TD.setText("");
       result=0;
       function=0;
       tmp="";
       }
    else if(ae.getSource()==jb0)
       {
       tmp = tmp + "0";
       TD.setText(tmp); //show
       }
    else if(ae.getSource()==jb1)
       {
       tmp = tmp + "1";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb2)
       {
       tmp = tmp + "2";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb3)
       {
       tmp = tmp + "3";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb4)
       {
       tmp = tmp + "4";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb5)
       {
       tmp = tmp + "5";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb6)
       {
       tmp = tmp + "6";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb7)
       {
       tmp = tmp + "8";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb8)
       {
       tmp = tmp + "8";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb9)
       {
       tmp = tmp + "9";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb10)
       {
       result = Double.parseDouble(TD.getText());
       TD.setText("");
       function=1; //1 for add
       tmp="";
       }
    else if(ae.getSource()==jb11)
       {
       result = Double.parseDouble(TD.getText());
       TD.setText("");
       function=2;   //2 for -
       tmp="";
       }
    else if(ae.getSource()==jb12)
       {
       result = Double.parseDouble(TD.getText());
       TD.setText("");
       function=3;   //3 for *
       tmp="";
       }
    else if(ae.getSource()==jb13)
       {
       result = Double.parseDouble(TD.getText());
       TD.setText("");
       function=4;    //4 for /
       tmp="";
       }
    else if(ae.getSource()==jb14)
       {
       tmp = tmp + ".";
       TD.setText(tmp);
       }
    else if(ae.getSource()==jb15)
       {
       //check function value if it is +, -, * or /
       if(function==1)
         {
         result = result + Double.parseDouble(TD.getText());     //perform addition
         }
       else if(function==2)
         {
         result = result - Double.parseDouble(TD.getText());
         }
       else if(function==3)
         {
         result = result * Double.parseDouble(TD.getText());
         }
       else if(function==4)
         {
         result = result / Double.parseDouble(TD.getText());
         }
       if(result-(int)result!=0)
          TD.setText("" + result);
       else
          TD.setText("" + (int)result);

       //if you enter anything after = button than it will add that after result
       //if you want to clear after = symbol than set tmp="";
       tmp = TD.getText();
       }

    }
public static void main(String args[]) //main function to start application
    {
    new SuperCalculator(); try
      {
      UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
      }
    catch (Exception e){}
    }
}

Output:

Java Form - O X Super Calculator 0 1 2 3 4 5 6 7 8 9 + - Exit Clear

Code Screenshot

import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.border.*; public class SuperCalculator i

Add a comment
Know the answer?
Add Answer to:
Please code the following Java App using Swing No additional classes are required. With that in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This assignemnt for Android development using Java Weather Forecaster App Goal: Create an app to display...

    This assignemnt for Android development using Java Weather Forecaster App Goal: Create an app to display a weather forecast for the user’s current location Create the app titled Weather Forecaster The first screen will have a textbox for the user to enter a zip code and a button to submit When the user enters the zip code and clicks the button, the app will navigate to the weather forecast screen. The weather forecast screen will show the current weather for...

  • Visual Studio Code C# Well Document everyline saying what the code does. Include designer code and...

    Visual Studio Code C# Well Document everyline saying what the code does. Include designer code and .cscode Extra 6-1 Create a simple calculator In this exercise, you'l1 create a form that accepts two operands and an operator from the user and then performs the requested operation. Simple Calculator Operand 1: 86 Operator Operand 2 11.11 Resut 7.7408 1. Start a new project named SimpleCalculator in the Extra Exercises Chapter 06SimpleCalculator directory 2. Add labels, text boxes, and buttons to the...

  • C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to...

    C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to create a Windows Form Application that will mimics a calculator. This is the screenshot how it shouldl look like. Calculator It should have a textbox and the following buttons: + Addition - Subtraction * Multiplication / Division = Equals (Will perform the final calculation) C Clear (Will clear the text box) There will be no maximize or minimize buttons. The rules are these: 2...

  • I need this done in java plz, add comments for every single method. also, make as simple as po...

    i need this done in java plz, add comments for every single method. also, make as simple as possible. 3. Write a GUI for computing the statistics of numbers input from the user. The GUI should have a text field and a button for adding floating point numbers. Each time a number is entered have a label show the current max, min and average. Have your GUI accept numbers whether the button is pressed or the enter key is pressed...

  • Write a calculator program using JavaScript in HTML in the same HTML file. (There will only...

    Write a calculator program using JavaScript in HTML in the same HTML file. (There will only be 1 HTML file containing everything that you use for this program.) *************JavaScript Functions should be written in the HTML <head> .............. </head> tag, **************** (Please be mindful of the formatting of the text of your program. Meaning that do not copy paste everything in a single line. Add clear comments for a better understanding of your program) as follows Assignment: create a form...

  • A fitness tracking app company has asked you to develop a windows application using a GUI...

    A fitness tracking app company has asked you to develop a windows application using a GUI to determine the total amount of hours somebody has exercised during their lifetime, assuming on average a user has exercises 2.5 hours per week. When the user uses the app, they will enter the following: First Name: Last Name: Date of Birth (in mm/dd/yyyy): Current Date (in mm/dd/yyyy) This program should display the users name and the number of hours the user has exercised...

  • Using AndroidStudio Create a mobile app UI that has the following labels and textboxes. Customer Name...

    Using AndroidStudio Create a mobile app UI that has the following labels and textboxes. Customer Name Customer ID Customer Address The mobile app UI should also contain a Submit button. The following validation should be executed as soon as user clicks the Submit button. Customer ID should be between 0-1000. If user enters any value above 1000, then you need to display an error message. Customer Name should not contain any numeric characters. If user enters customer name with numbers,...

  • Visual Basic Programming Step 1-2 not important, it's just file naming.

    Visual Basic Programming Step 1-2 not important, it's just file naming. 3. Form contains nine Labels, one TextBox, and three Button controls. You use labels to let user know what to enter and what will be displayed; TextBoxes to input a number between 1 and 99. Buttons to Calculate change. Clear Input and Exit program. See below Form Layout with Controls for more details 4. Declare variables to store the entered value in TextBox, and what will be displayed in...

  • Program using visual basic.net You will create a very simple two numbers calculator with save options;...

    Program using visual basic.net You will create a very simple two numbers calculator with save options; here is the specifications for the application. Create a form divided vertically in two halves with right and left panels 1- In the left panel you will create the following control, the label or the value of the control will be in "" and the type of the control will in [] a- "First Number" [textbox] b- "Second number" [texbox] c- "Result" [textbox] -...

  • Visual Basic Programming Step 1-2 not important, it's just file naming. 3. Form contains nine Labels,...

    Visual Basic Programming Step 1-2 not important, it's just file naming. 3. Form contains nine Labels, one TextBox, and three Button controls. You use labels to let user know what to enter and what will be displayed; TextBoxes to input a number between 1 and 99. Buttons to Calculate change. Clear Input and Exit program. See below Form Layout with Controls for more details 4. Declare variables to store the entered value in TextBox, and what will be displayed in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT