Skip to main content

Write a java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -, *, % operations. Add a text field to display the result.

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class calculator extends JFrame implements ActionListener
{
      static JFrame f;
      static JTextField l;
      String s0,s1,s2;
      calculator()
      {
           s0=s1=s2="";
      }
      public static void main(String args[])
      {
           f=new JFrame("Calculator");
          /*try
           {
                 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
           }
           catch(Exception e)
           {
                 System.out.println(e.getMessage());
           }*/
           calculator c=new calculator();
           l=new JTextField(16);
           l.setEditable(false);
           JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bs,bm,bd,be,beq,beq1;
           
           b0=new JButton("0");
           b1=new JButton("1");
           b2=new JButton("2");
           b3=new JButton("3");
           b4=new JButton("4");
           b5=new JButton("5");
           b6=new JButton("6");
           b7=new JButton("7");
           b8=new JButton("8");
           b9=new JButton("9");
           
           beq1=new JButton("=");
           beq=new JButton("C");
           be=new JButton(".");
           ba=new JButton("+");
           bs=new JButton("-");
           bm=new JButton("*");
           bd=new JButton("/");
           
           JPanel p=new JPanel();
           
           b0.addActionListener(c);
           b1.addActionListener(c);
           b2.addActionListener(c);
           b3.addActionListener(c);
           b4.addActionListener(c);
           b5.addActionListener(c);
           b6.addActionListener(c);
           b7.addActionListener(c);
           b8.addActionListener(c);
           b9.addActionListener(c);
           
           ba.addActionListener(c);
           bs.addActionListener(c);
           bm.addActionListener(c);
           bd.addActionListener(c);
           be.addActionListener(c);
           beq.addActionListener(c);
           beq1.addActionListener(c);
           
           p.add(l);
           p.add(ba);
           p.add(bs);
           p.add(bm);
           p.add(bd);
           p.add(be);
           p.add(beq);
           p.add(beq1);
           
           p.add(b0);
           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);
           
           f.add(p);
           f.setLocation(200,300);
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           f.setSize(200,220);
           f.show();
      }
     
      public void actionPerformed(ActionEvent ae)
      {
           String s=ae.getActionCommand();
           
           if(s.charAt(0)>='0' && s.charAt(0)<='9' || s.charAt(0)=='.')
           {
                  if(!s1.equals(""))
                        s2=s2+s;
                  else
                        s0=s0+s;
                       
                  l.setText(s0+s1+s2);
           }
           else if(s.charAt(0)=='C')
           {
                   s0=s1=s2="";
                   l.setText(s0+s1+s2);
           }
           else if(s.charAt(0)=='=')
           {
                   double te;
                   
                   if(s1.equals("+"))
                         te=(Double.parseDouble(s0) + Double.parseDouble(s2));
                   else if(s1.equals("-"))
                         te=(Double.parseDouble(s0) - Double.parseDouble(s2));
                   else if(s1.equals("/"))
                         te=(Double.parseDouble(s0) / Double.parseDouble(s2));
                   else
                         te=(Double.parseDouble(s0) * Double.parseDouble(s2));
                         
                   l.setText(s0+s1+s2+"="+te);
                   
                   s0=Double.toString(te);
                   s1=s2="";
           }
           else
           {
                   if(s1.equals("") || s2.equals(""))
                          s1=s;
                   else
                   {
                         double te;
                   
                         if(s1.equals("+"))
                               te=(Double.parseDouble(s0) + Double.parseDouble(s2));
                         else if(s1.equals("-"))
                               te=(Double.parseDouble(s0) - Double.parseDouble(s2));
                         else if(s1.equals("/"))
                               te=(Double.parseDouble(s0) / Double.parseDouble(s2));
                         else
                               te=(Double.parseDouble(s0) * Double.parseDouble(s2));
                         
                         s0=Double.toString(te);
                         s1=s;
                         s2="";
                   }
                   l.setText(s0+s1+s2);
            }
       }
}

Comments

Popular posts from this blog

Write a program to read book information (bookid, bookname, bookprice, bookqty) in file “book.dat”. Write a menu driven program to perform the following operations using Random access file: i. Search for a specific book by name. ii. Display all book and total cost

  import java . io .*; import java . util .*; class Book {       String name , id ;       int qty ;       double price , total ;       Book ( String i , String n , String p , String q )      {               name = n ;               id = i ;               qty = Integer . parseInt ( q );               price = Double . parseDouble ( p );               total = qty * price ;      }       public String toString ()      {               System . out . println ( "name\t id\t qty\t price\t total" );               String s = name + "\t" + id + "\t" + qty + "\t" + price + "\t" + total ;           ...

Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid. Examples of invalid dates : 03 15 2019, 31 6 2000, 29 2 2021

  import java . io .*; import java . util .*; class InvalidDateException extends Exception {       InvalidDateException ()       {               System . out . println ( "Invalid Date" );       } } class MyDate {       int day , mon , yr ;       void accept ( int d , int m , int y )       {             day = d ;             mon = m ;             yr = y ;       }       void display ()       {             System . out . println ( "Date is valid : " + day + "/" + mon + "/" + yr );       } } class Date {       public static void main ( String args []) throws Exception       {             Scanner sc = new Sca...

Write a Java program to design a screen using Awt that will take a user name and password. If the user name and password are not same, raise an Exception with appropriate message. User can have 3 login chances only. Use clear button to clear the TextFields.

  import java . awt .*; import java . awt . event .*; import javax . swing .*; class InvalidPasswordException extends Exception {       InvalidPasswordException ()       {             System . out . println ( "Username and password is not same" );       } } public class Password extends Frame implements ActionListener {       Label uname , upass ;       TextField nametext ;       TextField passtext , msg ;       Button login , Clear ;       Panel p ;       int attempt = 0 ;       char c = '*' ;             public void login ()       {             p = new Panel ();             uname = new Label ( "Username : " , Label . CENTER );             upass = n...