Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this lab we will build a calculator that can perform different mathematical o

ID: 3822867 • Letter: I

Question

In this lab we will build a calculator that can perform different mathematical operations as well as can check for valid inputs and display the results in a formatted manner Your task is to write a program that does all of the following 1. Displays the menu and asks user to enter a choice from the menu. 2. If the user enters something other than the right choices asks again for a valid choice 3. Reads two numbers from the user. You should use type float or double for the numbers 4. If the user enters wrong input, asks for the correct input again 5. If the user choses division then the second number cannot be zero. In that case, the program should ask user for the input again 6. Then displays the result in the form of mathematical equation. 7. Keeps displaying the menu and perform the tasks until the user decides to quit.

Explanation / Answer

PROGRAM:-

import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Calculator implements ActionListener
{
   Frame f;
   TextField t;
   double value1=0,result=0;
   String a="",o;
  
WindowCloser wc;
   Panel p1;
   Button b1,b2,b3,ba,b4,b5,b6,bs,b7,b8,b9,bm,bc,b0,be,bd;
   public Calculator()
   {
       f=new Frame("Calculator");
       f.setBackground(Color.white);
       f.setSize(400,300);
wc=new WindowCloser();
f.addWindowListener(wc);
      
   t=new TextField();
  
       f.add(t,"North");
       p1=new Panel();
       p1.setLayout(new GridLayout(4,4));
       f.add(p1);
   b1=new Button("1");
   p1.add(b1);
   b1.addActionListener(this);
   b2=new Button("2");
   p1.add(b2);
   b2.addActionListener(this);
   b3=new Button("3");
   p1.add(b3);
   b3.addActionListener(this);
      
   ba=new Button("+");
   p1.add(ba);
   ba.addActionListener(this);
      
b4=new Button("4");
   p1.add(b4);
   b4.addActionListener(this);
b5=new Button("5");
   p1.add(b5);
   b5.addActionListener(this);
   b6=new Button("6");
   p1.add(b6);
   b6.addActionListener(this);

   bs=new Button("-");
   p1.add(bs);
   bs.addActionListener(this);

   b7=new Button("7");
   p1.add(b7);
   b7.addActionListener(this);
   b8=new Button("8");
   p1.add(b8);
   b8.addActionListener(this);
   b9=new Button("9");
   p1.add(b9);
   b9.addActionListener(this);

   bm=new Button("*");
   p1.add(bm);
   bm.addActionListener(this);
   bc=new Button("C");
   p1.add(bc);
   bc.addActionListener(this);
b0=new Button("0");
   p1.add(b0);
   b0.addActionListener(this);
   bd=new Button("/");
   p1.add(bd);
   bd.addActionListener(this);
   be=new Button("=");
   p1.add(be);
   be.addActionListener(this);
   f.setVisible(true);
      
   }  
public void actionPerformed(ActionEvent e)
{
   String button=e.getActionCommand();
  
   if(Character.isDigit(button.charAt(0)))
   {
   a=(a+button.charAt(0));
   t.setText(a);
   }   
   else

   {
   if(a.length()>0)
   {
       value1=Double.parseDouble(a);
       if(o==null)
       {
       result=value1;
       }
       else
       {
      
           if("+".equals(o))
           result=result+value1;
           else if("-".equals(o))
           result=result-value1;
           else if("*".equals(o))
           result=result*value1;
           else if("/".equals(o))
           result=result/value1;
           else if("=".equals(o))
           result=value1;
          
           t.setText(result+"");
          
       }
          
   }
           a="";
           o=button;
       if("C".equals(o))
           {
           a="";
           t.setText("");
           o=null;
           result=0;
           }
   
   }
}
public static void main(String args[])
{
Calculator c=new Calculator();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote