Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 9:13 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Making calculator in JAVA
PostPosted: Tue Mar 31, 2009 2:10 pm 
Offline
Newbie
User avatar

Joined: Tue Mar 31, 2009 1:59 pm
Posts: 1
Has thanked: 0 time
Have thanks: 0 time

please help me in my assignment..
actually its our finals.our teacher gave a codes in java regarding making calculator.we have to put other functions without deleting the original code.all we have to do is to add a codes for the function in calculator.please help me i need it very badly. :swoon:
this is the code:
Code:

import java
.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JPanel implements ActionListener
{
    private JTextField display = new JTextField("0");
    private String button = "789/456*123.=+";
    private double result = 0;
    private String operator = "=";
    private boolean calculating = true;
    private boolean decimal = false;
public Calculator()
{
    setLayout(new BorderLayout());
    
    display
.setEditable(false);
    add(display, "North");
    
    JPanel p 
= new JPanel();
    p.setLayout (new GridLayout (4,4));
    
        for 
(int i = 0; i < button.length(); i++)
        {
            JButton b = new JButton(button.substring(i, i + 1));
            p.add(b);
            b.addActionListener(this);
        }
        add(p, "Center");
    }

public void actionPerformed(ActionEvent evt)
{
    String cmd = evt.getActionCommand();
    if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals("."))
    {
        System.out.println("Entered Here");
        if (calculating){
            System.out.println("Entered calculating "+ cmd.charAt(0));
            display.setText(cmd);
 }
        else
        display
.setText(display.getText() + cmd);
        calculating = false;
 }
    
    else
    
{
        if(calculating)
        {
            if (cmd.equals("-"))
            calculating = false;
        }
        else
        
{
            try{
                double x = Double.parseDouble(display.getText());
                calculate(x);
                operator = cmd;
                calculating = true;
            }catch(NumberFormatException e){
                display.setText("Illegal Number Format");
                calculating = false;
            }
 calculating = true;
        }
    }
}
private void calculate(double n)
{
 
if(operator.equals("+"))
    result += n;
    else if(operator.equals("-"))
    result -= n;
    else if(operator.equals("/"))
    result /= n;
    else if(operator.equals("*"))
    result *= n;
    else if(operator.equals("="))
    result = n;
    
    display
.setText("" + result);
}

public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("Calculator");
frame.setBounds(250,250,200,200);
frame.addWindowListener(new WindowAdapter()
{
 public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
});

Container contentPane = frame.getContentPane();
contentPane.add(new Calculator());
frame.show();
}
}
 


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team