• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculator Buttons  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a program that shows a calculator window. When
the calculator displays, there's no equal button. I just can't
find what's wrong. Can you help? Here's my code. Thanks
/*
Project 5:Using Frames
Programmer:Linda Weeks
Date:November 26, 2001
Program Name:Calculator
*/
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener
{
private Button keysArray[];
private Panel keyPad;
private TextField lcdField;
private double result;
private boolean first;
private boolean foundKey;
static boolean clearText;
private int prevOperator;
public Calculator()
{
lcdField = new TextField(20);
keyPad = new Panel();
keysArray = new Button[16];
result = 0.0;
prevOperator = 0;
first = true;
clearText = true;
//Set frame layout manager
setLayout(new BorderLayout());
lcdField.setEditable(false);
//Create buttons
for (int i = 0; i <=9; i++)
keysArray[i] = new Button(String.valueOf(i));
keysArray[10] = new Button("/");
keysArray[11] = new Button("*");
keysArray[12] = new Button("-");
keysArray[13] = new Button("+");
keysArray[14] = new Button("=");
keysArray[15] = new Button(".");
//Set panel layout manager
keyPad.setLayout(new GridLayout(4,4));
//Add buttons to keypad panel
for (int i = 7; i <= 10; i++)//adds buttons 7, 8, 9, and divide to Panel
keyPad.add(keysArray[i]);
for (int i = 4; i <= 6; i++)//adds buttons 4, 5, and 6 to Panel
keyPad.add(keysArray[i]);
keyPad.add(keysArray[11]);//adds multiply butoon to Panel
for (int i = 1; i <=3; i++)//adds buttons 1, 2, and 3 to Panel
keyPad.add(keysArray[i]);
keyPad.add(keysArray[12]);//adds subtract button to Panel

keyPad.add(keysArray[0]);//adds 0 key to Panel
for (int i = 15; 1>= 13; i--)
keyPad.add(keysArray[i]);//adds decimal point, equal, and addition to keys Panel
add(lcdField, BorderLayout.NORTH);//adds text field to top of Frame
add(keyPad, BorderLayout.CENTER);//adds Panel to center of Frame
for(int i = 0; i< keysArray.length; i++)
keysArray[i].addActionListener(this);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
foundKey = false;
//Search for the key pressed
for (int i = 0; i < keysArray.length && !foundKey; i++)
if(e.getSource() == keysArray[i])//key match found
{
foundKey = true;
switch(i)
{
case 0: case 1: case 2: case 3: case 4://number buttons
case 5: case 6: case 7: case 8: case 9://0 - 9
case 15://decimal point button
if(clearText)
{
lcdField.setText("");
clearText = false;
}
lcdField.setText(lcdField.getText() + keysArray[i].getLabel());
break;
case 10://divide button
case 11://multiply button
case 12://minus button
case 13://plus button
case 14://equal button
clearText = true;
if (first)//first operand
{
if(lcdField.getText().length()==0)
result = 0.0;
else
result = Double.valueOf(lcdField.getText()).doubleValue();
first = false;
prevOperator = i;//save previous operator
}
else//second operand already entered, so calculate total
{
switch(prevOperator)
{
case 10://divide button
result /= Double.valueOf(lcdField.getText()).doubleValue();
break;
case 11://multiply button
result *= Double.valueOf(lcdField.getText()).doubleValue();
break;
case 12://minus button
result -= Double.valueOf(lcdField.getText()).doubleValue();
break;
case 13://plus button
result += Double.valueOf(lcdField.getText()).doubleValue();
break;
}
lcdField.setText(Double.toString(result));
if(i==14)//equal button
first = true;
else
prevOperator = i;//save previous operator
}
break;
}
}
}
public static void main(String args[])
{
//Create a new instance of the Calculator object
Calculator calcFrame = new Calculator();
//Set frame attributes
calcFrame.setBounds(100,100,200,200);
calcFrame.setTitle("Calculator");
calcFrame.setVisible(true);
}
}

 
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't read through the whole thing, but if this isn't a typo, it's definitely a problem:

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shows up for me. After changing that 1>=13 to i>=13 like Michael pointed out.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Ernest:
I didn't read through the whole thing, but if this isn't a typo, it's definitely a problem:




here in the new code is


/*
* Created on 27/06/2006
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package kk;

/**
* @author ahmed khashila
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.awt.*;
import java.awt.event.*;

public class ncalc extends Frame implements ActionListener
{
private Button keysArray[];
private Panel keyPad;
private TextField lcdField;
private double result;
private boolean first;
private boolean foundKey;
static boolean clearText;
private int prevOperator;

public ncalc()
{
lcdField = new TextField(20);
keyPad = new Panel();
keysArray = new Button[16];
result = 0.0;
prevOperator = 0;
first = true;
clearText = true;

//Set frame layout manager
setLayout(new BorderLayout());

lcdField.setEditable(false);

//Create buttons
for (int i = 0; i <=9; i++)
keysArray[i] = new Button(String.valueOf(i));
keysArray[10] = new Button("/");
keysArray[11] = new Button("*");
keysArray[12] = new Button("-");
keysArray[13] = new Button(".");
keysArray[14] = new Button("=");
keysArray[15] = new Button("+");

//Set panel layout manager
keyPad.setLayout(new GridLayout(4,4));

//Add buttons to keypad panel
for (int i = 7; i <= 10; i++) //adds buttons 7, 8, 9, and divide to Panel
keyPad.add(keysArray[i]);

for (int i = 4; i <= 6; i++) //adds buttons 4, 5, and 6 to Panel
keyPad.add(keysArray[i]);

keyPad.add(keysArray[11]); //adds multiply butoon to Panel

for (int i = 1; i <=3; i++) //adds buttons 1, 2, and 3 to Panel
keyPad.add(keysArray[i]);

keyPad.add(keysArray[12]); //adds subtract button to Panel

keyPad.add(keysArray[0]); //adds 0 key to Panel

for (int i = 13; i <=15; i++)
keyPad.add(keysArray[i]);

//adds decimal point, equal, and addition to keys Panel

add(lcdField, BorderLayout.NORTH); //adds text field to top of Frame
add(keyPad, BorderLayout.CENTER); //adds Panel to center of Frame

for(int i = 0; i< keysArray.length; i++)
keysArray[i].addActionListener(this);

addWindowListener(
new WindowAdapter()
{

public void windowClosing(WindowEvent e)
{

System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
foundKey = false;

//Search for the key pressed
for (int i = 0; i < keysArray.length && !foundKey; i++)
if(e.getSource() == keysArray[i]) //key match found
{
foundKey = true;
switch(i)
{
case 0: case 1: case 2: case 3: case 4: //number buttons
case 5: case 6: case 7: case 8: case 9: //0 - 9
case 13: //decimal point button
if(clearText)
{
lcdField.setText("");
clearText = false;
}
lcdField.setText(lcdField.getText() + keysArray[i].getLabel());
break;

case 10: //divide button
case 11: //multiply button
case 12: //minus button
case 15: //plus button
case 14: //equal button
clearText = true;
if (first) //first operand
{
if(lcdField.getText().length()==0)
result = 0.0;
else
result = Double.valueOf(lcdField.getText()).doubleValue();
first = false;
prevOperator = i; //save previous operator
}
else //second operand already entered, so calculate total
{
switch(prevOperator)
{
case 10: //divide button
result /= Double.valueOf(lcdField.getText()).doubleValue();
break;
case 11: //multiply button
result *= Double.valueOf(lcdField.getText()).doubleValue();
break;
case 12: //minus button
result -= Double.valueOf(lcdField.getText()).doubleValue();
break;
case 15: //plus button
result += Double.valueOf(lcdField.getText()).doubleValue();
break;

}
lcdField.setText(Double.toString(result));
if(i==14) //equal button
first = true;
else
prevOperator = i; //save previous operator

}
break;
}
}
}
public static void main(String args[])
{
//Create a new instance of the Calculator object
ncalc calcFrame = new ncalc();
//calcFrame.show();
//Set frame attributes
calcFrame.setBounds(100,100,200,200);
calcFrame.setTitle("Calculator");
calcFrame.setVisible(true);
}
}

sorry for late (:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!