0
woodpecker

last time----JAVA SUCKS--programmers dig in

Recommended Posts

Last thing I have to do and stuck on 3 buttons. I'm building a calculator in JAVA and am having issues getting the +, -, and = keys to compile and work.

here is the coding so far:

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

public class Calculator implements ActionListener
{
private String number = "";
private JButton ButtonBS = new JButton("BS");
private JLabel numberArea = new JLabel(number, JLabel.CENTER);
private JButton ButtonClear = new JButton("Clear");

private JButton Button1 = new JButton("1");
private JButton Button2 = new JButton("2");
private JButton Button3 = new JButton("3");
private JButton Button4 = new JButton("4");
private JButton Button5 = new JButton("5");
private JButton Button6 = new JButton("6");
private JButton Button7 = new JButton("7");
private JButton Button8 = new JButton("8");
private JButton Button9 = new JButton("9");
private JButton Button0 = new JButton("0");

private JButton Button+ = new JButton("+");
private JButton Button- = new JButton("-");
private JButton Button= = new JButton("=");

private JFrame outputFrame = new JFrame("CALCULATOR");

public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();

Object source = event.getSource();

if (source == Button1)
{
number = number + "1";
numberArea.setText(number);
}
else if(source == Button2)
{
number = number + "2";
numberArea.setText(number);
}
else if(source == Button3)
{
number = number + "3";
numberArea.setText(number);
}
else if(source == Button4)
{
number = number + "4";
numberArea.setText(number);
}
else if(source == Button5)
{
number = number + "5";
numberArea.setText(number);
}
else if(source == Button6)
{
number = number + "6";
numberArea.setText(number);
}
else if(source == Button7)
{
number = number + "7";
numberArea.setText(number);
}
else if(source == Button8)
{
number = number + "8";
numberArea.setText(number);
}
else if(source == Button9)
{
number = number + "9";
numberArea.setText(number);
}
else if(source == Button0)
{
number = number + "0";
numberArea.setText(number);
}
else if(source == ButtonClear)
{
number = "";
numberArea.setText(number);
}
else if (source == ButtonBS)
{
number = number.substring(0, number.length() - 1);
numberArea.setText(number);
}

}
public Calculator()
{
number = "";
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputFrame.setSize(400, 400);
outputFrame.setLayout(new GridLayout(4, 4));

Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
Button4.addActionListener(this);
Button5.addActionListener(this);
Button6.addActionListener(this);
Button7.addActionListener(this);
Button8.addActionListener(this);
Button9.addActionListener(this);
Button0.addActionListener(this);
ButtonBS.addActionListener(this);
ButtonClear.addActionListener(this);
Button+.addActionListener(this);
Button-.addActionListener(this);
Button=.addActionListener(this);

outputFrame.add(ButtonBS);
outputFrame.add(numberArea);
outputFrame.add(ButtonClear);
outputFrame.add(Button1);
outputFrame.add(Button2);
outputFrame.add(Button3);
outputFrame.add(Button4);
outputFrame.add(Button5);
outputFrame.add(Button6);
outputFrame.add(Button7);
outputFrame.add(Button8);
outputFrame.add(Button9);
outputFrame.add(Button0);
outputFrame.add(Button+);
outputFrame.add(Button-);
outputFrame.add(Button=);
outputFrame.setVisible(true);
}
public static void main(String [] args)
{
Calculator gui = new Calculator();
}
}
SONIC WOODY #146

There is a fine line between cockiness and confidence -- which side of the line are you on?

Share this post


Link to post
Share on other sites
It won't compile because you're using illegal variable names in:

private JButton Button+ = new JButton("+");
private JButton Button- = new JButton("-");
private JButton Button= = new JButton("=");

Use some other name like:

private JButton buttonPlus;

also ...

you must be coming from the MS C++ environment since it's not common in Java to start you variable names with upper case characters. We use lower case in Java instead. But that is cosmetic. It will still compile with upper case characters.

Finally look into a different way to handle your events. Having big "if" / "else" statements in one method is not the best practice.


Try not to worry about the things you have no control over

Share this post


Link to post
Share on other sites
Java doesn't suck at all.

The compilation errors are due to illegal variable names. I don't know of many programming languages where "+", "-" make good variable names.

If you called an int "number-" - what would "number---" mean? What about number-++ That would be confusing in Java, C, C++, or C#. Don't use special characters in variable names.

Secondly, implementing actionListener is an easy way to handle functionality of a button, but when you start having multiple buttons it gets really confusing. Look at how you can add a new custom Listener to each button. For the easy way to do this, think of a way to add an Anonymous Inner class to each button. For extra points move the Listeners into their own class, maybe even in a different package like "busobj", or something. Think about ways to separate your business logic from your UI.

All these tricks help to make Java definately not suck.

_Am
__

You put the fun in "funnel" - craichead.

Share this post


Link to post
Share on other sites
OK, changed it and now the buttons show up....and thanks to all for the help up to now.

Any suggestions on how to get these new buttons to actually work. Been 16 straight weeks of JAVA and I am fried. Thanks again all.


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

public class Calculator implements ActionListener
{
private String number = "";
private JButton ButtonBS = new JButton("BS");
private JLabel numberArea = new JLabel(number, JLabel.CENTER);
private JButton ButtonClear = new JButton("Clear");

private JButton Button1 = new JButton("1");
private JButton Button2 = new JButton("2");
private JButton Button3 = new JButton("3");
private JButton Button4 = new JButton("4");
private JButton Button5 = new JButton("5");
private JButton Button6 = new JButton("6");
private JButton Button7 = new JButton("7");
private JButton Button8 = new JButton("8");
private JButton Button9 = new JButton("9");
private JButton Button0 = new JButton("0");

Button buttonPlus = new Button("+");;
Button buttonMinus = new Button("-");
Button buttonEquals = new Button("=");

private JFrame outputFrame = new JFrame("CALCULATOR");

public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();

Object source = event.getSource();

if (source == Button1)
{
number = number + "1";
numberArea.setText(number);
}
else if(source == Button2)
{
number = number + "2";
numberArea.setText(number);
}
else if(source == Button3)
{
number = number + "3";
numberArea.setText(number);
}
else if(source == Button4)
{
number = number + "4";
numberArea.setText(number);
}
else if(source == Button5)
{
number = number + "5";
numberArea.setText(number);
}
else if(source == Button6)
{
number = number + "6";
numberArea.setText(number);
}
else if(source == Button7)
{
number = number + "7";
numberArea.setText(number);
}
else if(source == Button8)
{
number = number + "8";
numberArea.setText(number);
}
else if(source == Button9)
{
number = number + "9";
numberArea.setText(number);
}
else if(source == Button0)
{
number = number + "0";
numberArea.setText(number);
}
else if(source == ButtonClear)
{
number = "";
numberArea.setText(number);
}
else if (source == ButtonBS)
{
number = number.substring(0, number.length() - 1);
numberArea.setText(number);
}

}
public Calculator()
{
number = "";
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputFrame.setSize(400, 400);
outputFrame.setLayout(new GridLayout(4, 4));

Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
Button4.addActionListener(this);
Button5.addActionListener(this);
Button6.addActionListener(this);
Button7.addActionListener(this);
Button8.addActionListener(this);
Button9.addActionListener(this);
Button0.addActionListener(this);
ButtonBS.addActionListener(this);
ButtonClear.addActionListener(this);
buttonPlus.addActionListener(this);
buttonMinus.addActionListener(this);
buttonEquals.addActionListener(this);

outputFrame.add(ButtonBS);
outputFrame.add(numberArea);
outputFrame.add(ButtonClear);
outputFrame.add(Button1);
outputFrame.add(Button2);
outputFrame.add(Button3);
outputFrame.add(Button4);
outputFrame.add(Button5);
outputFrame.add(Button6);
outputFrame.add(Button7);
outputFrame.add(Button8);
outputFrame.add(Button9);
outputFrame.add(Button0);
outputFrame.add(buttonPlus);
outputFrame.add(buttonMinus);
outputFrame.add(buttonEquals);
outputFrame.setVisible(true);
}


public static void main(String [] args)
{
Calculator gui = new Calculator();
}
}
SONIC WOODY #146

There is a fine line between cockiness and confidence -- which side of the line are you on?

Share this post


Link to post
Share on other sites
Quote

Any suggestions on how to get these new buttons to actually work.



Please elborate more as to what you're trying to do and what the program may or may not be doing. Are you not getting your events to fire? Or are you looking at some other issue? What do you mean by getting the buttons to work? I haven't bothered to copy the code into a compiler and run it. If push comes to sholve maybe I can do that later, but I can't do it now. Sorry ...

Note: I'm in and out of here all day long (I will actually be out of here for the next few hours). I'm sure others like AndyMan are in the same boat. If possible I will try and provide you with some additional help with Java, but it may be a delayed response.


Try not to worry about the things you have no control over

Share this post


Link to post
Share on other sites
After further communication with woodpecker, I quickly glaced at the code closer and noticed that he wasn't handing the "plus", "minus" and "equals" button events inside of his actionPerformed() method. Those events were just dropping through the method without being handled.

But AndyMan was dead on right when he said you need to seperate the business logic from the views as I believe woody's issue was that his actionPerformed() method was growing too large with too many "if/else" conditions. Annoymous event handlers or subclassing his buttons would make the code much easier to follow, modify and ultimately maintain. I also told woodpecker to google "Model View Controller" to help him understand some of these concepts better.

But people who want to get into programming should know that programming is not easy. It requires a lot of discipline to keep things as simple and as easy to follow while still being dynamic and getting the job done.

Back to my home renos ...


Try not to worry about the things you have no control over

Share this post


Link to post
Share on other sites
One other thought about woodpeckers issue and the advise that AndyMan and myself have given him. I have little knowledge about what woodpecker may know about software development. It's one thing for guys like AndyMan and myself to blurt out concepts like Model/View/Controller as we're both professionals in this field and know many programming concepts. But it's possible that woodpecker is still in the stage of learning how to crawl when it comes to programming and telling him how to run maybe a little too early in his development.

In fact it's not all that different from canopy flight in skydiving. I shouldn't be telling someone how to do a swooping freestyle trick when they're just learning how to fly their student canopy. LOL ... I actually need to be asking other people about freestyle tricks myself as I'm still very much a novice when it comes to freestyle tricks.


Try not to worry about the things you have no control over

Share this post


Link to post
Share on other sites
Anonymous inner class to handle the event is the way I'd go.

If I recall correctly, the Java Tutorial (available for free from Sun) has a nice discussion and explanation.

The best book I've ever read on Java was Core Java, but it's been updated several times and split into two volumes since I last looked at it (in 1997?).

Share this post


Link to post
Share on other sites
dude... that would be so much easier to read with switch/case statements. Probably faster too.

JAVA rocks, seriously. I realize you're not going into programming when you are done, but it really does a better job logically than lots of other langauages, and you'd realize that if you were coding for a living.

I miss JAVA some days [:/]
Arianna Frances

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0