Java calculator
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Java calculator

  • 4,614 views
Uploaded on

I made a calculator with using Menus and Button Array

I made a calculator with using Menus and Button Array

More in: Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
4,614
On Slideshare
4,612
From Embeds
2
Number of Embeds
1

Actions

Shares
Downloads
46
Comments
0
Likes
0

Embeds 2

http://www.docshut.com 2

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Java CalculatorChapter 6
    Sarah McNellis
  • 2. Entering Variables into the Calculator Application Code mean
    Displays the code to begin the Calculator application. Lines 17 through 25 declare the private variables.
  • 3. Entering Variables into theCalculator Application
    Import
    Statements
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
    public class Calculator extends Frame implements ActionListener
    {
    private Button keys[];
    private Panel keypad;
    private TextField lcd;
    private double op1;
    private boolean first;
    private boolean foundKey;
    private boolean clearText;
    private int lastOp;
    private DecimalFormat calcPattern;
    Class
    Header
    Private
    Variables
    Declared
  • 4. Using Menus in the Calculator Application code means
    Displays the beginning of the Calculator() constructor method also the code in the slide also shows the code that to set up the menu system. These are some of the meaning of the codes
  • 5. Using Menus in the Calculator Application
    Constructor
    Method header
    public Calculator()
    {
    // create an instance of the menu
    MenuBar mnubar = new MenuBar();
    setMenuBar(mnuBar);
    // construct and populate the File Menu
    Menu mnuFile = new Menu("File", true);
    setMenuBar(mnuBar);
    MenuItem mnuFileExit = new MenuItem("Exit");
    mnuFile.and(mnuFileExit);
    Code to create
    Menu bar
    Code to create
    File menu
  • 6. Using Menus in the Calculator Application Continue
    // construct and populate the Edit menu
    Menu mnuEdit = new Menu("Edit", true);
    mnuBar.and(mnuEdit);
    MenuItem mnuEditClear = new MenuItem("Clear");
    mnuEdit.and(mnuEditClear);
    mnuEdit.insertSeparator(1);
    MenuItem mnuEditCopy = new MenuItem("Copy");
    mnuEdit.and(mnuEditCopy);
    MenuItem mnuEditPaste = new MenuItem("Paste");
    mnuEdit.and(mnuEditPaste);
    // construct and populate the About menu
    Menu mnuAbout = new Menu("About", true);
    mnuBar.and(mnuAbout);
    MenuItem mnuAboutCalculator = new MenuItem("About Calculator");
    mnuAbout.add(mnuAboutCalculator);
    Code to create
    Edit menu
    Code to create
    About menu
  • 7. Using Menus in the Calculator Application Continue
    // add the ActionListener to each menu item
    mnuFileExit.addActionListener(this);
    mnuEditClear.addActionListener(this);
    mnuEditCopy.addActionListener(this);
    mnuEditPaste.addActionListener(this);
    mnuAboutCalculator.addActionListener(this);
    // assign an ActionCommand to each menu item
    mnuFileExit.setActionCommand("Exit");
    mnuEditClear.setActionCommand("Clear");
    mnuEditCopy.setActionCommand("Copy");
    mnuEditPaste.setActionCommand("Paste");
    mnuAboutCalculator.setActionCommand("About");
    addActionListener()
    Methods for each menu item
    setActionCommand() methods for each menu item
  • 8. Initializing the Calculator Variables code means
    It display the code to set initial values for the Calculator application.
  • 9. Initializing the Calculator Variables
    // construct components and initialize beginning values
    lcd = new TextField(20);
    lcd.setEditable(false);
    keypad = new Panel();
    keys = new Button[16];
    first = true;
    op1 = 0.0;
    clearText = true;
    lastOp= 0;
    calcPattern = new DecimalFormat("########.########");
    Code to initialize variables
  • 10. Creating the Keypad code means
    Displays the code to construct and label the keypad buttons, as well as the method to establish the GridLayout to organize the buttons.
  • 11. Creating the Keypad
    // construct and assign captions to the Buttons
    for (int i=0; i<=9; i++)
    key[i] = new Button(String.valueOf(i));
    keys[10] = new Button("/");
    keys[11] = new Button("*");
    keys[12] = new Button("-");
    keys[13] = new Button("+");
    keys[14] = new Button("=");
    keys[15] = new Button(".");
    // set Frame and keypad layout to grid layout
    setLayout(new BorderLayout());
    keypad.setLayout(new GridLayout(4,4,10,10));
    Code to construct
    Buttons using array
    setLayout() methods set BorderLayout for Frame and GridLayout for keypad
  • 12. Adding components to the interface code means
    Displays the code to add the 16 buttons to the panel using the four-row, four-column grid. Line comments within the code will help you understand how each loop and add method takes its turn creating buttons in the keypad.
  • 13. Adding components to the interface
    for (int i=7; i<=10; i++) // 7, 8, 9, divide
    keypad.add(keys[i]);
    for (int i=4; i<=6; i++) // 4, 5, 6
    keypad.add(keys[i]);
    keypad.add(keys[11]); // multiply
    for (int i=1; i<=3; i++) // 1, 2, 3
    keypad.add(keys[i]);
    keypad.add(keys[12]); // subtract
    keypad.add(keys[0]); // 0 key
    for (int i=15; i>=13; i--)
    keypad.add(keys[i]); // decimal point, =, add (+) keys
    Code to add buttons keypad
  • 14. Continuation of adding components to the Interface code means
    It displays the code to add the ActionListener to each of the Buttons.
  • 15. Continue adding componentsto the Interface
    Code to add
    Buttons to keypad
    for (int i=0; i<keys.length; i++)
    keys[i].addActionListener(this);
    add(lcd, BorderLayout.NORTH);
    add(keypad, BorderLayout.CENTER);
    Lcd TextField and
    keypad Panel components
    Added to frame
  • 16. Coding the addWindowListenerMethod means
    It displays the code to add a WindowListener for the Frame
  • 17. Coding the addWindowListenerMethod
    Creates occurrence
    Of windowAdapter()
    class
    addWindowListener(
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );
    }// end of constructor method
    addWindowListener()
    method
    Overrides
    windowClosing()
    method
    Brace to add method
  • 18. Searching for the Exitand Clear Commands code means
    Displays the code used to search to menu items for a click. During program execution, the ActionCommand of any click in the interface, menu, or button will be stored in the variable arg.
  • 19. Searching for the Exitand Clear Commands
    ActionPerformed()
    Method header
    public void actionPerformed(ActionEvent e)
    {
    //test for menu item clicks
    String arg = e.getActionCommand();
    if (arg == "Exit")
    System.exit(0);
    if (arg == "Clear")
    {
    clearText = true;
    first = true;
    op1 = 0.0;
    lcd.setText("")
    lcd.requestFocus();
    }
    Tests
    For Exit
    command
    Code to exit
    program
    Tests for
    Clear
    command
    Code to clear
    Number displayed in
    Lcd TextField and
    Reset initial values
  • 20. Searching for the Copy and Paste Commands code means
    It displays the code executed if the user clicks Copy or Paste on the Edit menu.
  • 21. Searching for the Copy and Paste Commands Code
    Tests if Copy
    Command clicked
    if (arg == "Copy")
    {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection centents = new StringSelection(lcd.getText());
    cb.setContents(contents, null);
    }
    if (arg == "Paste")
    {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable content = cb.getContents(this);
    try
    {
    String s = (String)content.getTransferData(DataFlavor.stringFlavor);
    lcd.setText(calcPattern.format(Double.parseDouble(s)));
    }
    catch (Throwable exc)
    {
    lcd.setText("")
    }
    }
    Code to copy display
    To clipboard
    Test if Paste command
    clicked
    Code to paste data
    From clipboard
  • 22. Searching for the About Calculator Command mean in code
    It display the code to search for the Calculator command.
  • 23. Searching for the About Calculator Command Code
    Test if the About
    Command was clicked
    if (arg == "About")
    {
    String message = "Calculator ver. 1.0nOpenExhibit SoftwarenCopyright 2007nAll right reserved";
    JOptionPane.showMessageDialog(null ,message,"About Calculator", JOptionPane.INFORMATION_MESSAGE);
    }
    Code to display
    About Calculator
    Message box
  • 24. Searching the Numeric Buttons Code means
    It displays the code to search the array of buttons to determine which button the user clicked and to add functionality to the numeric buttons and the decimal point in the actionPerformed() method.
  • 25. Searching the Numeric Buttons Code
    // test for button clicks
    foundKey = false;
    // search for the clicked key
    for (int i=0; i<keys.length && !foundKey; i++)
    {
    if(e.getSource() == keys[i])
    {
    foundKey = true;
    switch(i)
    {
    // number and decimal point buttons
    case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 15:
    if(clearText)
    {
    lcd.setText("");
    clearText = false;
    }
    lcd.setText(lcd.getText() + keys[i].getLabel());
    break;
    It displays the code
    To test for button click
    Of the numeric
    Buttons or the
    Decimal point button
  • 26. Searching for the First Operator ButtonClick Code means
    It displays the code to search the array and add functionality to the operator buttons within the actionPerformed() method.
  • 27. Searching for the First Operator ButtonClick Code
    // operator buttons
    case 10: case 11: case 12: case 13: case 14:
    clearText = true;
    if (first) // first operand
    {
    if(lcd.getText().length()==0) op1 = 0.0;
    else op1 = Double.parseDouble(lcd.getText());
    first = false;
    clearText = true;
    lastOp = i; //save last operator
    }
    It displays the code to
    Handle the first operator
    Button click in a
    calculation
  • 28. Searching for Subsequent OperatorClicks code means
    This displays is the code that executes on subsequent operator click
  • 29. Searching for Subsequent OperatorClicks code
    else // second operand
    {
    switch(lastOp)
    {
    case 10: // divide button
    op1 /= Double.parseDouble(lcd.getText());
    break;
    case 11: // multiply button
    op1 *= Double.parseDouble(lcd.getText());
    break;
    case 12: // minus button
    op1 -= Double.parseDouble(lcd.getText());
    break;
    case 13: // plus button
    op1 += Double.parseDouble(lcd.getText());
    break;
    } // end of switch(lastOp)
    lcd.setText(calcPattern.format(op1));
    clearText = true;
    The code to handle
    Subsequent operator
    Button click in a calculation
  • 30. Searching for the Equal Button code means
    When the user specifically clicks the equal button, the lastOp variable is assigned a value of 14, which is the index number of the equal button.
  • 31. Searching for the Equal Button code
    The cod to handle a click of
    The equal button,
    Represented by the subscript
    14 in the array.
    if(i==14) first = true;//equal button
    else lastOp = i; // save last operator
    } // end else
    break;
    } // end of switch(i)
    } // end of if
    } // end of for
    } // end of actionPerformed
  • 32. Coding the main() Method for the Calculator class coding means
    The main() method for the Calculator application constructs an instance of the Calculator class and then sets three attributes.
  • 33. Coding the main() Method for the Calculator class
    public static void main(String args[])
    {
    // set frame properties
    Calculator f = new Calculator();
    f.setTitle("Calculator Application");
    f.setBounds(200,200,300,300);
    f.setVisible(true);
    // set image properties and add to frame
    Image icon = Toolkit.getDefaultToolkit().getImage("calcImage.gif");
    f.setIconImage(icon);
    } // end of main
    }// end of class
  • 34. Print screen of the Calculator
  • 35. Link to the file
    Calculator
  • 36. Work Cited
    Shelly Cashman Starks Mick: “Java Programming comprehensive Concepts and Techniques” third Edition print 2006