Opis aplikacji

Ćwiczenie 1 Rozbudować kalkulator o odejmowanie, mnożenie, dzielenie, ewentualnie inne działania i funkcje matematyczne nie uwzględniając kolejności działań.

Ćwiczenie 2 Dodać możliwość wprowadzania działań z nawiasami oraz zaimplementować właściwą kolejność działań (np. 2+2*2=6).

Kod źródłowy

import java.awt.* ; import java.awt.event.* ;

public class calc extends Frame {

Button btn; TextField tfield; Double buffer; String lastCommand; boolean lastKeyIsCommand;

private class DigitPressedActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { if ( lastKeyIsCommand ) tfield.setText("") ; tfield.setText( tfield.getText() + e.getActionCommand() ); lastKeyIsCommand = false ;

private class CommandPressedActionListener implements ActionListener { public void actionPerformed(ActionEvent e) {

if( lastCommand.equals("+") ) buffer += new Double( tfield.getText() );

if( lastCommand.equals("=") ) buffer = new Double( tfield.getText() );

tfield.setText( buffer.toString() ); lastCommand = e.getActionCommand() ; lastKeyIsCommand = true ;

public calc()

this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0);

} );

DigitPressedActionListener digitPressedAL = new DigitPressedActionListener(); CommandPressedActionListener commandPressedAL = new CommandPressedActionListener();

buffer = 0.0 ; lastCommand = new String("=") ; lastKeyIsCommand = true ;

setTitle("Kalkulator") ; setLayout( new BorderLayout() ) ; Panel p = new Panel() ; p.setLayout( new GridLayout(5,4) ) ; tfield = new TextField("0.0") ; add( tfield, BorderLayout.PAGE_START ) ;

btn = new Button(""); p.add( btn ); btn = new Button(""); p.add( btn ); btn = new Button(""); p.add( btn ); btn = new Button(""); p.add( btn );

btn = new Button("1"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("2"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("3"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("+"); p.add( btn ); btn.addActionListener(commandPressedAL);

btn = new Button("4"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("5"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("6"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button(""); p.add( btn );

btn = new Button("7"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("8"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("9"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button(""); p.add( btn );

btn = new Button("0"); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("."); p.add( btn ); btn.addActionListener(digitPressedAL); btn = new Button("="); p.add( btn ); btn.addActionListener(commandPressedAL); btn = new Button(""); p.add( btn );

add( p, BorderLayout.CENTER ) ; pack(); setVisible(true);

public static void main( String args[] ) { calc e = new calc();