473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Calculator App Help

2
Hello,
I am trying to get my calculator GUI to +,-,* and /. I have got all of them to work except my division. I was wondering if someone could helpme figure out the problem. Any input would help and I'll leave my code in here
================================================== =======
package chapfour;

/**
*
* @author Deezle
*/
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calc2 {

Container pane;
static JFrame frame;
static JButton back;
static JButton ce;
static JButton cc;
static JButton mc;
static JButton mr;
static JButton ms;
static JButton mplus;
static JButton seven;
static JButton eight;
static JButton nine;
static JButton four;
static JButton three;
static JButton five;
static JButton six;
static JButton one;
static JButton two;
static JButton slash;
static JButton multi;
static JButton div;
static JButton sub;
static JButton add;
static JButton root;
static JButton modulus;
static JButton frac;
static JButton equal;
static JButton zero;
static JButton plusmin;
static JButton decimal;
static JTextField tf2;
static JTextField tf;

double total = 0;
double total2 = 0;
String operation;
double dTotal;
/** Creates a new instance of Calc */
public Calc2 () {
frame = new JFrame ("Calculator");
pane = frame.getContentPane ();
pane.setLayout (new GridBagLayout ());
tf = new JTextField ("0");
GridBagConstraints c = new GridBagConstraints ();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
back = new JButton ("Back");
back.addActionListener (new MyListener ());
pane.add (back, c);
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 1;
ce = new JButton ("CE");
ce.addActionListener (new MyListener ());
pane.add (ce, c);
c.gridx = 4;
c.gridy = 1;
cc = new JButton ("C");
cc.addActionListener (new MyListener ());
pane.add (cc, c);
c.gridx = 0;
c.gridy = 2;
mc = new JButton ("MC");
mc.addActionListener (new MyListener ());
pane.add (mc, c);
c.gridx = 0;
c.gridy = 3;
mr = new JButton ("MR");
mr.addActionListener (new MyListener ());
pane.add (mr, c);
c.gridx = 0;
c.gridy = 4;
ms = new JButton ("MS");
ms.addActionListener (new MyListener ());
pane.add (ms, c);
c.gridx = 0;
c.gridy = 5;
mplus = new JButton ("M+");
mplus.addActionListener (new MyListener ());
pane.add (mplus, c);
c.gridx = 1;
c.gridy = 2;
seven = new JButton ("7");
seven.addActionListener (new MyListener ());
pane.add (seven, c);
c.gridx = 2;
c.gridy = 2;
eight = new JButton ("8");
eight.addActionListener (new MyListener ());
pane.add (eight, c);
c.gridx = 3;
c.gridy = 2;
nine = new JButton ("9");
nine.addActionListener (new MyListener ());
pane.add (nine, c);
c.gridx = 1;
c.gridy = 3;
four = new JButton ("4");
four.addActionListener (new MyListener ());
pane.add (four, c);
c.gridx = 2;
c.gridy = 3;
five = new JButton ("5");
five.addActionListener (new MyListener ());
pane.add (five, c);
c.gridx = 1;
c.gridy = 4;
three = new JButton ("3");
three.addActionListener (new MyListener ());
pane.add (three, c);
c.gridx = 3;
c.gridy = 3;
six = new JButton ("6");
six.addActionListener (new MyListener ());
pane.add (six, c);
c.gridx = 2;
c.gridy = 4;
two = new JButton ("2");
two.addActionListener (new MyListener ());
pane.add (two, c);
c.gridx = 3;
c.gridy = 4;
one = new JButton ("1");
one.addActionListener (new MyListener ());
pane.add (one, c);
c.gridx = 1;
c.gridy = 5;
zero = new JButton ("0");
zero.addActionListener (new MyListener ());
pane.add (zero, c);
c.gridx = 2;
c.gridy = 5;
plusmin = new JButton ("+/-");
plusmin.addActionListener (new MyListener ());
pane.add (plusmin, c);
c.gridx = 3;
c.gridy = 5;
decimal = new JButton (".");
decimal.addActionListener (new MyListener ());
pane.add (decimal, c);
c.gridx = 5;
c.gridy = 1;
slash = new JButton ("/");
slash.addActionListener (new MyListener ());
pane.add (slash, c);
c.gridx = 4;
c.gridy = 2;
multi = new JButton ("*");
multi.addActionListener (new MyListener ());
pane.add (multi, c);
c.gridx = 4;
c.gridy = 3;
div = new JButton ("/");
slash.addActionListener (new MyListener ());
pane.add (slash, c);
c.gridx = 4;
c.gridy = 4;
sub = new JButton ("-");
sub.addActionListener (new MyListener ());
pane.add (sub, c);
c.gridx = 4;
c.gridy = 5;
add = new JButton ("+");
add.addActionListener (new MyListener ());
pane.add (add, c);
c.gridx = 5;
c.gridy = 2;
root = new JButton ("sqrt");
root.addActionListener (new MyListener ());
pane.add (root, c);
c.gridx = 5;
c.gridy = 3;
modulus = new JButton ("%");
modulus.addActionListener (new MyListener ());
pane.add (modulus, c);
c.gridx = 5;
c.gridy = 4;
frac = new JButton ("1/x");
frac.addActionListener (new MyListener ());
pane.add (frac, c);
c.gridx = 5;
c.gridy = 5;
equal = new JButton ("=");
equal.addActionListener (new MyListener ());
pane.add (equal, c);

c.gridx = 0;
c.gridy = 0;
c.gridwidth = 6;
tf.addActionListener (new MyListener ());
pane.add (tf, c);
tf2 = new JTextField();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
tf2.addActionListener (new MyListener ());
pane.add (tf2, c);
frame.pack ();
frame.setVisible (true);
}
/**
* @param args the command line arguments
*/
public static void main (String[] args) {
Calc2 calc = new Calc2 ();
// TODO code application logic here
}
class MyListener
implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (e.getSource () == back) {
tf.setText ("Backspace");
}
if (e.getSource () == ce) {
total = 0;
tf.setText ("0");
}
if (e.getSource () == cc) {
total = 0;
tf.setText ("0");
}
if (e.getSource () == mc) {
tf.setText ("MC");
}
if (e.getSource () == mr) {
tf.setText ("MR");
}
if (e.getSource () == ms) {
tf.setText ("MS");
}
if (e.getSource () == mplus) {
tf.setText ("M+");
}
if (e.getSource () == seven) {
total = (total*10)+7;
tf.setText(""+total);
}
if (e.getSource () == eight) {
total = (total*10)+8;
tf.setText (""+total);
}
if (e.getSource () == nine) {
total = (total*10)+9;
tf.setText (""+total);
}
if (e.getSource () == four) {
total = (total*10)+4;
tf.setText (""+total);
}
if (e.getSource () == five) {
total = (total*10)+5;
tf.setText (""+total);
}
if (e.getSource () == six) {
total = (total*10)+6;
tf.setText (""+total);
}
if (e.getSource () == three) {
total = (total*10)+3;
tf.setText (""+total);
}
if (e.getSource () == two) {
total = (total*10)+2;
tf.setText (""+total);
}
if (e.getSource () == one) {
total = (total*10)+1;
tf.setText (""+total);
}
if (e.getSource () == zero) {
total = (total*10);
tf.setText (""+total);
}
if (e.getSource () == plusmin) {
total*=-1;
tf.setText (""+total);
}
if (e.getSource () == decimal) {
tf.setText (".");
}
if (e.getSource () == slash) {
total2 = total;
total = 0;
operation = "divide";
}
if (e.getSource () == multi) {
total2 = total;
total = 0;
operation = "multiply";
}
if (e.getSource () == sub) {
total2 = total;
total = 0;
operation = "subtract";
}
if (e.getSource () == add) {
total2 = total;
total = 0;
operation = "add";
}
if (e.getSource () == root) {
dTotal = Math.sqrt(total);
tf.setText ("" + dTotal);
}
if (e.getSource () == modulus) {
tf.setText ("%");
}
if (e.getSource () == frac) {
tf.setText ("1/X");
}
if (e.getSource () == equal) {
if (operation=="add"){
total = total+total2;
tf.setText(""+total);
total = 0;
total2 = 0;
}
if (operation=="multiply"){
total = total*total2;
tf.setText(""+total);
total = 0;
total2 = 0;
}
if (operation=="divide"){

dTotal = (total2/ total);
tf.setText(""+dTotal);
total = 0;
total2 = 0;
dTotal = 0;
}
if (operation=="subtract"){
total = total2-total;
tf.setText(""+total);
total = 0;
total2 = 0;
}
}
}
}
}
Nov 9 '07 #1
2 1891
JosAH
11,448 Expert 8TB
Hello,
I am trying to get my calculator GUI to +,-,* and /. I have got all of them to work except my division. I was wondering if someone could helpme figure out the problem. Any input would help and I'll leave my code in here
Don't mind all that code; I assume that the division operator should divide both
of its operands; like the multiplication operator multiplies them. What does the
division operator do instead?

kind regards,

Jos
Nov 9 '07 #2
Laharl
849 Expert 512MB
What isn't working about the division? Is it displaying nothing, displaying something incorrect, or giving you errors?
Nov 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: mwh | last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a calculator with javascript. I can't get this to work: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
2
by: XIII | last post by:
i just created this benefits calculator, but it doesn't work, there is no changes happen after submitting, anyone can help in that?? <html> <head> <title> |||Stocks Calculator||| </title> <style...
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
1
by: Synapse | last post by:
Hello... We were asked to create a simple calculator program in our C++ subject by using loops only. i have a problem in creating a loop in the multiplication and division operation so please can...
1
by: remya1000 | last post by:
from my system i need to open a calculator in remote machine. and i'm using Vb.net and WMI. i need to pop up the calculator in remote machine, while i run one program in my system. while running in...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.