473,788 Members | 3,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculator App Help

2 New Member
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.JBu tton;
import javax.swing.JFr ame;
import javax.swing.JTe xtField;
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.getConten tPane ();
pane.setLayout (new GridBagLayout ());
tf = new JTextField ("0");
GridBagConstrai nts c = new GridBagConstrai nts ();
c.fill = GridBagConstrai nts.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
back = new JButton ("Back");
back.addActionL istener (new MyListener ());
pane.add (back, c);
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 1;
ce = new JButton ("CE");
ce.addActionLis tener (new MyListener ());
pane.add (ce, c);
c.gridx = 4;
c.gridy = 1;
cc = new JButton ("C");
cc.addActionLis tener (new MyListener ());
pane.add (cc, c);
c.gridx = 0;
c.gridy = 2;
mc = new JButton ("MC");
mc.addActionLis tener (new MyListener ());
pane.add (mc, c);
c.gridx = 0;
c.gridy = 3;
mr = new JButton ("MR");
mr.addActionLis tener (new MyListener ());
pane.add (mr, c);
c.gridx = 0;
c.gridy = 4;
ms = new JButton ("MS");
ms.addActionLis tener (new MyListener ());
pane.add (ms, c);
c.gridx = 0;
c.gridy = 5;
mplus = new JButton ("M+");
mplus.addAction Listener (new MyListener ());
pane.add (mplus, c);
c.gridx = 1;
c.gridy = 2;
seven = new JButton ("7");
seven.addAction Listener (new MyListener ());
pane.add (seven, c);
c.gridx = 2;
c.gridy = 2;
eight = new JButton ("8");
eight.addAction Listener (new MyListener ());
pane.add (eight, c);
c.gridx = 3;
c.gridy = 2;
nine = new JButton ("9");
nine.addActionL istener (new MyListener ());
pane.add (nine, c);
c.gridx = 1;
c.gridy = 3;
four = new JButton ("4");
four.addActionL istener (new MyListener ());
pane.add (four, c);
c.gridx = 2;
c.gridy = 3;
five = new JButton ("5");
five.addActionL istener (new MyListener ());
pane.add (five, c);
c.gridx = 1;
c.gridy = 4;
three = new JButton ("3");
three.addAction Listener (new MyListener ());
pane.add (three, c);
c.gridx = 3;
c.gridy = 3;
six = new JButton ("6");
six.addActionLi stener (new MyListener ());
pane.add (six, c);
c.gridx = 2;
c.gridy = 4;
two = new JButton ("2");
two.addActionLi stener (new MyListener ());
pane.add (two, c);
c.gridx = 3;
c.gridy = 4;
one = new JButton ("1");
one.addActionLi stener (new MyListener ());
pane.add (one, c);
c.gridx = 1;
c.gridy = 5;
zero = new JButton ("0");
zero.addActionL istener (new MyListener ());
pane.add (zero, c);
c.gridx = 2;
c.gridy = 5;
plusmin = new JButton ("+/-");
plusmin.addActi onListener (new MyListener ());
pane.add (plusmin, c);
c.gridx = 3;
c.gridy = 5;
decimal = new JButton (".");
decimal.addActi onListener (new MyListener ());
pane.add (decimal, c);
c.gridx = 5;
c.gridy = 1;
slash = new JButton ("/");
slash.addAction Listener (new MyListener ());
pane.add (slash, c);
c.gridx = 4;
c.gridy = 2;
multi = new JButton ("*");
multi.addAction Listener (new MyListener ());
pane.add (multi, c);
c.gridx = 4;
c.gridy = 3;
div = new JButton ("/");
slash.addAction Listener (new MyListener ());
pane.add (slash, c);
c.gridx = 4;
c.gridy = 4;
sub = new JButton ("-");
sub.addActionLi stener (new MyListener ());
pane.add (sub, c);
c.gridx = 4;
c.gridy = 5;
add = new JButton ("+");
add.addActionLi stener (new MyListener ());
pane.add (add, c);
c.gridx = 5;
c.gridy = 2;
root = new JButton ("sqrt");
root.addActionL istener (new MyListener ());
pane.add (root, c);
c.gridx = 5;
c.gridy = 3;
modulus = new JButton ("%");
modulus.addActi onListener (new MyListener ());
pane.add (modulus, c);
c.gridx = 5;
c.gridy = 4;
frac = new JButton ("1/x");
frac.addActionL istener (new MyListener ());
pane.add (frac, c);
c.gridx = 5;
c.gridy = 5;
equal = new JButton ("=");
equal.addAction Listener (new MyListener ());
pane.add (equal, c);

c.gridx = 0;
c.gridy = 0;
c.gridwidth = 6;
tf.addActionLis tener (new MyListener ());
pane.add (tf, c);
tf2 = new JTextField();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
tf2.addActionLi stener (new MyListener ());
pane.add (tf2, c);
frame.pack ();
frame.setVisibl e (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(""+t otal);
}
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=="ad d"){
total = total+total2;
tf.setText(""+t otal);
total = 0;
total2 = 0;
}
if (operation=="mu ltiply"){
total = total*total2;
tf.setText(""+t otal);
total = 0;
total2 = 0;
}
if (operation=="di vide"){

dTotal = (total2/ total);
tf.setText(""+d Total);
total = 0;
total2 = 0;
dTotal = 0;
}
if (operation=="su btract"){
total = total2-total;
tf.setText(""+t otal);
total = 0;
total2 = 0;
}
}
}
}
}
Nov 9 '07 #1
2 1915
JosAH
11,448 Recognized Expert MVP
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 Recognized Expert Contributor
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
1932
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" "http://www.w3.org/TR/html4/loose.dtd"> <html> <title>Calculator</title> <script language="Javascript"> <!-- Begin Hiding var total = 0
6
7306
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.... #include <stdio.h> #include <stdlib.h>
2
1460
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 type="text/css"> <!-- body {font-size: 14pt} ..heading {font-size: 18pt; color: red} -->
3
15143
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 calculator. Ive made 4 classes 2 do this, my reasoning so it was easier to look through( when programming) rather than getting mixed up in my own code! My questions and problems: Ive been messing around with the windows look and feel,...
24
6341
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 have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
19
4033
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 java.awt.*; import java.awt.event.*; import java.io.*;
1
2942
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 anyone help me on this please. and also during the operation selection, if ill enter a character it wont go back to the main program. by the way, my compiler is Dev-C++. I need help badly..here's my code below... #include <iostream.h> #include...
1
5668
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 rmotre machine's taskmanager the calculator is displaying, but its not popuping. i tried this code for pop up using Win32_ProcessStartup like this... Module Module1 Sub Main() Dim retValue As String retValue = RunCommand("calc.exe",...
3
11857
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, my teacher wants the operators to follow the algebraic order of operations by chaining multiple operations. Such as, 7 + 4 * 2= 15. The operatorListener is the ActionListener for the operator buttons. Thanks for any help you can give me. ...
3
2897
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 the last operator entered /* Digit entered as integer value i * Updates the value of input accordingly to (input * 10) + i */
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.