Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Calculator
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SapphireBlue




PostPosted: Tue May 03, 2011 8:54 pm   Post subject: Calculator

Hello,

I am a beginner in the program Eclipse. I have to do an assignment that is due tommorrow on a Calculator program that can just add numbers. I am having trouble when I press a number adding another number and then pressing an equal sign, the answer is not given. Here is my code so far:

/*
* Created on Apr 29, 2011
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author 301687257
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/

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

public class Calculator extends JFrame implements ActionListener {
JTextField text = new JTextField (20);
Button b, b2, b3, b4, b5, b6, b7, b8, b9, c, c2, c3, c4;

private int numClicks = 0;
int num, num1, num2, num3 = 2;
int counter = 0;
String numbers = "";
String sum = "";
double temp = 0;
double mainNum;

//Constructor
public Calculator (String Title){
super (Title);
setSize(200, 300);
setLayout (new FlowLayout ());
b = new Button("Clear");
add (b);
add (text);
b.addActionListener(this);
b2 = new Button("1");
add (b2);
add (text);
b2.addActionListener(this);
b3 = new Button ("2");
add (b3);
add (text);
b3.addActionListener(this);
b4 = new Button ("3");
add (b4);
add (text);
b4.addActionListener(this);
b5 = new Button ("4");
add (b5);
add (text);
b5.addActionListener(this);
b6 = new Button ("5");
add (b6);
add (text);
b6.addActionListener(this);
b7 = new Button ("6");
add (b7);
add (text);
b7.addActionListener(this);
b8 = new Button ("7");
add (b8);
add (text);
b8.addActionListener(this);
b9 = new Button ("8");
add (b9);
add (text);
b9.addActionListener(this);
c = new Button ("9");
add (c);
add (text);
c.addActionListener(this);
c2 = new Button ("0");
add (c2);
add (text);
c2.addActionListener(this);
c3 = new Button ("+");
add (c3);
add (text);
c3.addActionListener(this);
c4 = new Button ("=");
add (c4);
add (text);
c4.addActionListener(this);

}


/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub


if (counter == 0)
num1 = num;
counter++;
if (counter == 1)
num2 = num1;
counter++;

if (e.getSource()==b)
text.setText("0.");
else if (e.getSource()==b2)
//System.out.println ("1");
text.setText("1");
else if (e.getSource()==b3)
text.setText("2");
else if (e.getSource()==b4)
text.setText("3");
else if (e.getSource()==b5)
text.setText("4");
else if (e.getSource()==b6)
text.setText("5");
else if (e.getSource()==b7)
text.setText("6");
else if (e.getSource()==b8)
text.setText("7");
else if (e.getSource()==b9)
text.setText("8");
else if (e.getSource()==c)
text.setText("9");
else if (e.getSource()==c2)
text.setText("0");
else if (e.getSource()==c3)
text.setText("+");
else if (e.getSource () == c4)

numbers = "";

//text.setText(sum);
counter++;
//System.out.println (num);
num1 = num;
num2 = num3;

}

public static void main(String[] args) {
Calculator myWindow = new Calculator("Calculator");
myWindow.setVisible(true);
}
}

For a better version, please see the attachment. Feel free to make any changes to it as much as possible. Thanks.



Calculator.java
 Description:

Download
 Filename:  Calculator.java
 Filesize:  3.25 KB
 Downloaded:  149 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
deltamanx




PostPosted: Thu May 05, 2011 3:20 pm   Post subject: Re: Calculator

Here's your problem:
code:

  if (counter == 0)
    num1 = num;
    counter++;
  if (counter == 1)
    num2 = num1;
    counter++;


The problem is, in Java, when you have an if structures that
has more than one line of code, you need brackets as follows:
code:

  if (counter == 0)
  {
    num1 = num;
    counter++;
  }
  if (counter == 1)
  {
    num2 = num1;
    counter++;
  }


Hope this helps!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: