Numerical expressions in Java are meant to mirror those of everyday algebra. The
ID: 440885 • Letter: N
Question
Numerical expressions in Java are meant to mirror those of everyday algebra. Therefore, arithmetic operators conform to a precedence relation; multiplication or division before addition or subtraction. Write a program called Calculator.java that implements a very simple calculator. It will accept as input exactly 3 numbers separated by two operators with no parenthesis. The allowed operators are + - * /. Output the value of the expression adhering to proper operator precedence rules. INPUT: a single line including 3 double values separated by two operators. The operators are separated from the numbers by a space. e.g. 3.4 + 2.5 * 5.7 OUTPUT: The result of the calculation as a double, printed to two decimal places The answer is supposed to look like this: Calculator.java: Input 3 decimal numbers separated by 2 operators: 10.7 + 12.4 / 6.2 The result is: 12.70 Input 3 decimal numbers separated by 2 operators: 3.23 * 5.01 + 2.44 The result is: 18.62 Input 3 decimal numbers separated by 2 operators: 8 - 4 - 4 The result is: 0.00Explanation / Answer
import java.awt.*; import javax.swing.*; import java.awt.event.*; class Calculator implements ActionListener { int c,n; String s1,s2,s3,s4,s5; Frame f; JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; Panel p; TextField tf1; GridLayout gl; Calculator() { f=new Frame("Calculator"); p=new Panel(); b1=new JButton("0"); b1.addActionListener(this); b2=new JButton("1"); b2.addActionListener(this); b3=new JButton("2"); b3.addActionListener(this); b4=new JButton("3"); b4.addActionListener(this); b5=new JButton("4"); b5.addActionListener(this); b6=new JButton("5"); b6.addActionListener(this); b7=new JButton("6"); b7.addActionListener(this); b8=new JButton("7"); b8.addActionListener(this); b9=new JButton("8"); b9.addActionListener(this); b10=new JButton("9"); b10.addActionListener(this); b11=new JButton("+"); b11.addActionListener(this); b12=new JButton("-"); b12.addActionListener(this); b13=new JButton("x"); b13.addActionListener(this); b14=new JButton("/"); b14.addActionListener(this); b15=new JButton("="); b15.addActionListener(this); b16=new JButton("A/C"); b16.addActionListener(this); tf1=new TextField(20); p.add(tf1); tf1.setBackground(Color.yellow); gl=new GridLayout(4,4,10,20); p.setLayout(gl); p.add(b1);p.add(b2);p.add(b3);p.add(b4);p.add(b5);p.add(b6);p.add(b7);p.add(b8);p.add(b9);p.add(b10);p.add(b11);p.add(b12);p.add(b13);p.add(b14);p.add(b15);p.add(b16); f.add(p); f.setSize(100,200); f.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { s3=tf1.getText(); s4="0"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b2) { s3=tf1.getText(); s4="1"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b3) { s3=tf1.getText(); s4="2"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b4) { s3=tf1.getText(); s4="3"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b5) { s3=tf1.getText(); s4="4"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b6) { s3=tf1.getText(); s4="5"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b7) { s3=tf1.getText(); s4="6"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b8) { s3=tf1.getText(); s4="7"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b9) { s3=tf1.getText(); s4="8"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b10) { s3=tf1.getText(); s4="9"; s5=s3+s4; tf1.setText(s5); } if(e.getSource()==b11) { s1=tf1.getText(); tf1.setText(""); c=1; } if(e.getSource()==b12) { s1=tf1.getText(); tf1.setText(""); c=2; } if(e.getSource()==b13) { s1=tf1.getText(); tf1.setText(""); c=3; } if(e.getSource()==b14) { s1=tf1.getText(); tf1.setText(""); c=4; } if(e.getSource()==b15) { s2=tf1.getText(); if(c==1) { n=Integer.parseInt(s1)+Integer.parseInt(s2); tf1.setText(String.valueOf(n)); } else if(c==2) { n=Integer.parseInt(s1)-Integer.parseInt(s2); tf1.setText(String.valueOf(n)); } else if(c==3) { n=Integer.parseInt(s1)*Integer.parseInt(s2); tf1.setText(String.valueOf(n)); } else if(c==4) { n=Integer.parseInt(s1)/Integer.parseInt(s2); tf1.setText(String.valueOf(n)); } } if(e.getSource()==b16) { tf1.setText(""); } } public static void main(String args[]) { Calculator c=new Calculator(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.