Question

Hi, please could someone be of assistance with this assignment for me. Thank you The objective...

Hi, please could someone be of assistance with this assignment for me. Thank you

The objective of this assignment is to practice the use of control objects in GUI design.
You are required to create a 4- function calculator. See pic below.

Calculator.png



The calculator should have buttons for all 10 numbers, the decimal point, the four operations (+,-,/,*), an equal “=” button, a "main display" (TextBox or RichTextBot), a backspace button and a clear “C” button.
When you create your controls, you need to give them proper names (e.g. ButtonOne, ButtonResult, ButtonClear,etc)
The only programming in this assignment is

  • To make sure that whenever a number button is clicked, that number will appear in the mainDiaply
    • e.g. if the user click 3, then the number 3 will appear. If the user then clicks 4, then 34 will appear.
    • make sure that include fractional numbers like "5.44", this means that you need to activate the '.' button
  • If the user clicks on the 'C' button, the main Display will be cleared

Homework Answers

Answer #1

import java.awt.event.*;
import javax.swing.*;
public class CalcGUI extends JFrame implements ActionListener {
   static JFrame frame;
   //for result display
   static JTextField textField;
   String str0, str1, str2;
   CalcGUI()
   {
       str0 = str1 = str2 = "";
   }
   //driver programme
   public static void main(String args[])
   {
       frame = new JFrame("CalcGUI");
       CalcGUI calc = new CalcGUI();
       textField = new JTextField(16);
       textField.setEditable(false);
       //for buttons
       JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, ButtonPlus, ButtonSubstract, ButtonDivition, ButtonMultiplication, ButtonDot, ButtonClear, buttonEqual;
       //intilizing buttons
       button0 = new JButton("0");
       button1 = new JButton("1");
       button2 = new JButton("2");
       button3 = new JButton("3");
       button4 = new JButton("4");
       button5 = new JButton("5");
       button6 = new JButton("6");
       button7 = new JButton("7");
       button8 = new JButton("8");
       button9 = new JButton("9");
       buttonEqual = new JButton("=");
       ButtonPlus = new JButton("+");
       ButtonSubstract = new JButton("-");
       ButtonDivition = new JButton("/");
       ButtonMultiplication = new JButton("*");
       ButtonClear = new JButton("C");
       ButtonDot = new JButton(".");
       JPanel panel = new JPanel();
       //adding action listeners to the buttons
       ButtonMultiplication.addActionListener(calc);
       ButtonDivition.addActionListener(calc);
       ButtonSubstract.addActionListener(calc);
       ButtonPlus.addActionListener(calc);
       button9.addActionListener(calc);
       button8.addActionListener(calc);
       button7.addActionListener(calc);
       button6.addActionListener(calc);
       button5.addActionListener(calc);
       button4.addActionListener(calc);
       button3.addActionListener(calc);
       button2.addActionListener(calc);
       button1.addActionListener(calc);
       button0.addActionListener(calc);
       ButtonDot.addActionListener(calc);
       ButtonClear.addActionListener(calc);
       buttonEqual.addActionListener(calc);
       //adding buttons to the panel
       panel.add(textField);
       panel.add(ButtonPlus);
       panel.add(button1);
       panel.add(button2);
       panel.add(button3);
       panel.add(ButtonSubstract);
       panel.add(button4);
       panel.add(button5);
       panel.add(button6);
       panel.add(ButtonMultiplication);
       panel.add(button7);
       panel.add(button8);
       panel.add(button9);
       panel.add(ButtonDivition);
       panel.add(ButtonDot);
       panel.add(button0);
       panel.add(ButtonClear);
       panel.add(buttonEqual);
       frame.add(panel);
       frame.setSize(200, 220);
       frame.show();
   }
   //this is the function which will perform when user clicks on it
   public void actionPerformed(ActionEvent e)
   {
       String actionCommand = e.getActionCommand();
       //if user press other than operators we need to append
       if ((actionCommand.charAt(0) >= '0' && actionCommand.charAt(0) <= '9') || actionCommand.charAt(0) == '.') {
           if (!str1.equals(""))
               str2 = str2 + actionCommand;
           else
               str0 = str0 + actionCommand;
           textField.setText(str0 + str1 + str2);
       }
       //this is for clear button
       else if (actionCommand.charAt(0) == 'C') {
           str0 = str1 = str2 = "";
           textField.setText(str0 + str1 + str2);
       }
       //this is the for calculation part
       else if (actionCommand.charAt(0) == '=') {
           double result;
           if (str1.equals("+"))
               result = (Double.parseDouble(str0) + Double.parseDouble(str2));
           else if (str1.equals("-"))
               result = (Double.parseDouble(str0) - Double.parseDouble(str2));
           else if (str1.equals("/"))
               result = (Double.parseDouble(str0) / Double.parseDouble(str2));
           else
               result = (Double.parseDouble(str0) * Double.parseDouble(str2));
           textField.setText(str0 + str1 + str2 + "=" + result);
           str0 = Double.toString(result);
           str1 = str2 = "";
       }
       else {
           if (str1.equals("") || str2.equals(""))
               str1 = actionCommand;
           else {
               double result;
               if (str1.equals("+"))
                   result = (Double.parseDouble(str0) + Double.parseDouble(str2));
               else if (str1.equals("-"))
                   result = (Double.parseDouble(str0) - Double.parseDouble(str2));
               else if (str1.equals("/"))
                   result = (Double.parseDouble(str0) / Double.parseDouble(str2));
               else
                   result = (Double.parseDouble(str0) * Double.parseDouble(str2));
               str0 = Double.toString(result);
               str1 = actionCommand;
               str2 = "";
           }
           textField.setText(str0 + str1 + str2);
       }
   }
}

Know the answer?
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
Similar Questions
I need the java code for a 4-function calculator app on android studio. Please make sure...
I need the java code for a 4-function calculator app on android studio. Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on...
Q1. Recently, there has been a noticeable increase in the demand on cryptocurrency investments. As a...
Q1. Recently, there has been a noticeable increase in the demand on cryptocurrency investments. As a result, many online companies have created electronic exchange apps to facilitate the process of buying and selling cryptocurrencies online. For this assignment, you are asked to mimic a simple cryptocurrency exchange applications. a. Create the following VB windows form: (5 points) - Make the title of the form “Redbirds Exchange”. - Disable the textboxes under Buy/Sell. - Make the GroupBox invisible (it is visible...
Please linked both files. For this assignment you need to create a ToDo list using Javascript,...
Please linked both files. For this assignment you need to create a ToDo list using Javascript, along with HTML and CSS. Begin by creating a HTML page called todo.html. Then create a Javascript file called todo.js and link it in to the HTML page using a script tag. All Javascript for the assignment must be in the separate file. (For CSS, feel free to include styles in a style block at the top of the HTML page, or to link...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS OR SWITCH STATEMENTS IN THE JAVASCRIPT. Even though there is stylesheet in the HTML file do no create a new one. Project Standards: Students will use click events to capture user input. Students will use variables to store information needed by their application and keep track of their program’s state. Students will use conditionals to control project flow. Project Task You will be building...
In Assignment 1, you created a program for Marshall’s Murals that prompts a user for the...
In Assignment 1, you created a program for Marshall’s Murals that prompts a user for the number of interior and exterior murals scheduled to be painted during the next month. The program computes the expected revenue for each type of mural when interior murals cost $500 each and exterior murals cost $750 each. In this assignment, you are going to design a GUI version using Visual Studio Form. The programs prompt a user for the number of interior and exterior...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include additional functions called by the main. Make sure to use ES6 style of keywords => instead of the older function and for local scope variables use the keyword let and not a keyword var. Make sure to follow the requirements and recheck before submitting. PROJECT GOAL: Write a program that is required to use nested loops to generate a triangle as shown in the...
For this assignment you'll be creating an application that has the user input a subtotal, tax...
For this assignment you'll be creating an application that has the user input a subtotal, tax rate and tip percentage and then displays the sales tax, tip amount and the total. You'll use JQuery instead of the getElementByX functions AND you will display all messages on the page (no alert or prompt boxes) The starter file is based off of the Lab 2 sales_tax application. Feel free to borrow code from your lab solution but realize you will need to...
Coding in Python Add radio button options for filing status to the tax calculator program of...
Coding in Python Add radio button options for filing status to the tax calculator program of Project 1. The user selects one of these options to determine the tax rate. The Single option’s rate is 20%. The Married option is 15%. The Divorced option is 10%. The default option is Single. Be sure to use the field names provided in the comments in your starter code. ================== Project 1 code: # Initialize the constants TAX_RATE = 0.20 STANDARD_DEDUCTION = 10000.0...
Programming Assignment Practice Concepts ●Registering events ●Protecting inputs through logic Problem Description For PA1, you are...
Programming Assignment Practice Concepts ●Registering events ●Protecting inputs through logic Problem Description For PA1, you are to create a Windows Forms Application using the .NET Framework. Name this project LastName_FirstName_PA1 . In this application you will create a tip calculator that takes two inputs from the user: A Meal Cost value (double), and a Tip Percentage amount (double). The application should do the following: 1.) The form text should display the name of your app (i.e. Tip Calculator) 2.) The...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT