Question

In: Computer Science

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

Solutions

Expert Solution

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);
       }
   }
}


Related Solutions

The following are questions for discussion assignment. Could you please assist me with these questions? Thank...
The following are questions for discussion assignment. Could you please assist me with these questions? Thank you. Rite Aid Corporation; NYSE: RAD Income Statement: Listed as Consolidated of Operations, page 76 Balance Sheet: Listed as Consolidated Balance Sheets, page 75 Statement of Stockholder's Equity: Listed as Consolidated Statements of Stockholders' Equity, page 78 Statement of Cash Flows: Listed as Consolidated Statements of Cash Flows, page 79 https://www.sec.gov/Archives/edgar/data/84129/000104746918003207/a2235393z10-k.htm SEC 10K Project: Income Statement Respond to one or more question(s) from each...
Hi I'm having a hard time understand this can someone please explain it to me? Thank...
Hi I'm having a hard time understand this can someone please explain it to me? Thank you. Cholesterol is a type of fat found in the blood. It is measured as a concentration: the number of milligrams of cholesterol found per deciliter of blood (mg/dL). A high level of total cholesterol in the bloodstream increases risk for heart disease. For this problem, assume cholesterol in men and women follows a normal distribution, and that “adult man” and “adult woman” refers...
Could someone please explain the consequences of low lung compliance. Thank you
Could someone please explain the consequences of low lung compliance. Thank you
Can someone show me how to break this down by steps please and thank you. You...
Can someone show me how to break this down by steps please and thank you. You sell short 200 shares of a stock at $95 on a 60% initial margin requirement with a 30% maintenance margin. In 3 months, the stock is $79. What is your margin at this time? When would a margin call occur? If you cover your position when the stock is $79, what would be your HPR? Assume you did the same trade as before but...
Hi all, Can someone please answer this question with all steps! Thank you! As a result...
Hi all, Can someone please answer this question with all steps! Thank you! As a result of improvements in product engineering, United Automation is able to sell one of its two milling machines. Both machines perform the same function but differ in age. The newer machine could be sold today for $69,500. Its operating costs are $22,600 a year, but in five years the machine will require a $18,700 overhaul. Thereafter operating costs will be $31,300 until the machine is...
Can someone summary this artical by one paragraph for me, please? Thank you Teen pregnancy is...
Can someone summary this artical by one paragraph for me, please? Thank you Teen pregnancy is an important issue for several reasons. For example, there are health risks for the baby and children born to teenage mothers are more likely to suffer health, social, and emotional problems than children born to older mothers. Also, women who become pregnant during their teens are at increased risk for medical complications, such as premature labor, and social consequences. According to the Centres for...
Hello can someone please explain the AS AD curve step by step to me? Thank you
Hello can someone please explain the AS AD curve step by step to me? Thank you
*will someone please explain/show the steps in this process for me? Thank you so much! Amy...
*will someone please explain/show the steps in this process for me? Thank you so much! Amy company produces and sells a toy for $205 per unit. In the first year of operations, 100,000 units were produced and 75,000 were sold. other information for the year includes: direct materials $31 per unit direct manufacturing labor $3 per unit variable manufacturing overhead costs $4 per unit sales selling expenses $4 per unit total fixed manufacturing costs $1,250,000 total fixed administrative expenses $950,000...
Hi there Can someone tell me. If someone file a case against you and you dont...
Hi there Can someone tell me. If someone file a case against you and you dont have money to fight against it. Then what person should do in that situation.? If someone file a sexual harassment case?
Hi all, can someone please post formulas used for corporate finance course. thank you!
Hi all, can someone please post formulas used for corporate finance course. thank you!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT