Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #15589 > unrolled thread

I need a different approach - suggestions please

Started bybilsch <bilsch01@gmail.com>
First post2012-06-25 15:31 -0700
Last post2012-06-29 13:07 -0700
Articles 20 on this page of 75 — 16 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-25 15:31 -0700
    Re: I need a different approach - suggestions please markspace <-@.> - 2012-06-25 16:15 -0700
      Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-25 16:47 -0700
        Re: I need a different approach - suggestions please markspace <-@.> - 2012-06-25 18:10 -0700
          Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-26 13:04 -0700
            Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-26 14:01 -0700
            Re: I need a different approach - suggestions please Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-26 17:21 -0400
              Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-27 14:44 -0700
                Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-27 14:56 -0700
                Re: I need a different approach - suggestions please Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-27 18:13 -0400
                  Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-27 18:48 -0700
                Re: I need a different approach - suggestions please Patricia Shanahan <pats@acm.org> - 2012-06-27 15:21 -0700
                  Re: I need a different approach - suggestions please Jail Bush Cronies <do.not@ask.me> - 2012-06-27 18:46 -0400
            Re: I need a different approach - suggestions please markspace <-@.> - 2012-06-26 14:25 -0700
              Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-26 14:32 -0700
                Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-26 17:49 -0400
                  Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-26 15:20 -0700
                    Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-27 17:30 -0700
                      Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-27 18:09 -0700
          Re: I need a different approach - suggestions please Wanja Gayk <brixomatic@yahoo.com> - 2012-07-02 11:21 +0200
        Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-25 18:36 -0700
          Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-26 13:24 -0700
            Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-26 13:49 -0700
            Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-26 14:04 -0700
              Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-26 14:34 -0700
                Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-26 16:26 -0700
                  Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-26 16:47 -0700
                    Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-26 20:04 -0700
                  Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-26 22:18 -0400
                    Re: I need a different approach - suggestions please Joshua Cranmer <Pidgeot18@verizon.invalid> - 2012-06-26 22:59 -0400
                      Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-27 21:30 -0400
                      Re: I need a different approach - suggestions please Wanja Gayk <brixomatic@yahoo.com> - 2012-07-02 11:32 +0200
                        Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-07-02 09:47 -0700
                    Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-26 20:06 -0700
                      Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-27 11:42 -0700
                        Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-27 14:09 -0700
                          Re: I need a different approach - suggestions please Patricia Shanahan <pats@acm.org> - 2012-06-27 15:11 -0700
                            Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-27 21:59 -0400
                              Re: I need a different approach - suggestions please Devil's Advocate <legal@hell.org> - 2012-06-27 22:17 -0400
                          Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-27 21:40 -0400
                            Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-27 19:12 -0700
                          Re: I need a different approach - suggestions please Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-06-27 20:47 -0500
                            Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-27 19:12 -0700
                      Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-27 21:28 -0400
                        Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-27 19:14 -0700
                          Re: I need a different approach - suggestions please Hydrangea <hydrangea393@foo.mail.quuzzle.edu> - 2012-06-27 22:28 -0400
                            Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-27 21:05 -0700
                          Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-27 22:32 -0400
                            Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-27 21:08 -0700
            Re: I need a different approach - suggestions please markspace <-@.> - 2012-06-26 14:10 -0700
              Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-26 14:35 -0700
                Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-26 17:51 -0400
                  Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-26 15:20 -0700
                    Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-26 19:35 -0400
                      Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-26 16:44 -0700
                        Re: I need a different approach - suggestions please Arne Vajhøj <arne@vajhoej.dk> - 2012-06-26 22:06 -0400
            Re: I need a different approach - suggestions please Patricia Shanahan <pats@acm.org> - 2012-06-27 08:15 -0700
          Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-27 18:36 -0700
            Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-27 18:54 -0700
              Re: I need a different approach - suggestions please B1ll G4tes <wm.gat3s@micro5oft.quux> - 2012-06-27 22:23 -0400
                Re: I need a different approach - suggestions please Lew <lewbloch@gmail.com> - 2012-06-28 09:55 -0700
              Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-28 04:02 -0700
                Re: I need a different approach - suggestions please Mike Winter <mlwinter@gmail.com> - 2012-06-28 04:55 -0700
                  Re: I need a different approach - suggestions please markspace <-@.> - 2012-06-28 07:33 -0700
                    [OT] Message IDs (Re: I need a different approach - suggestions please) Mike Winter <mlwinter@gmail.com> - 2012-06-28 08:45 -0700
                      Re: [OT] Message IDs (Re: I need a different approach - suggestions please) markspace <-@.> - 2012-06-28 09:39 -0700
                    Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-28 09:44 -0700
                    Re: I need a different approach - suggestions please Joshua Cranmer <Pidgeot18@verizon.invalid> - 2012-06-28 12:51 -0400
                      Re: I need a different approach - suggestions please markspace <-@.> - 2012-06-28 09:58 -0700
                Re: I need a different approach - suggestions please markspace <-@.> - 2012-06-28 07:32 -0700
    Re: I need a different approach - suggestions please Roedy Green <see_website@mindprod.com.invalid> - 2012-06-26 03:49 -0700
      Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-26 13:59 -0700
    Re: I need a different approach - suggestions please Patricia Shanahan <pats@acm.org> - 2012-06-29 08:12 -0700
      Re: I need a different approach - suggestions please Gene Wirchenko <genew@ocis.net> - 2012-06-29 10:02 -0700
      Re: I need a different approach - suggestions please bilsch <bilsch01@gmail.com> - 2012-06-29 13:07 -0700

Page 1 of 4  [1] 2 3 4  Next page →


#15589 — I need a different approach - suggestions please

Frombilsch <bilsch01@gmail.com>
Date2012-06-25 15:31 -0700
SubjectI need a different approach - suggestions please
Message-ID<jsaos3$bht$1@dont-email.me>
I'm trying to make a program that works like the calculator in Windows 
Accessories - all input is from button clicks.  Below I listed a 
stripped down version of the GUI and a little of the processing behind 
it.  It is in two small files. I had intended that the program would 
display a sequence of digits from button clicks, which I would then 
convert into a Double precision number.  The problem is I am constrained 
to initialize the string within the method that accumulates the sequence 
of digits.  Everytime the method is called the string is reinitialized 
with the result that my sequence of digits is only ever one digit long. 
My plan would work if I could initialize the string outside the method, 
however variable scope in Java doesn't allow it.

If you try the code below you will see my problem.  Alternative 
suggestions on how to make a simple calculator will be appreciated.

TIA  Bill S.

HERE'S THE GUI:

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

public class CalcGUIQ1 extends JFrame {
CrunchQ1 crunchNu = new CrunchQ1(this);

     // set up row 1
    JPanel row1 = new JPanel();
    JTextField number1 = new JTextField(10);
     // set up row 2
    JPanel row2 = new JPanel();


    JButton sev = new JButton("7");
    JButton ate = new JButton("8");
    JButton nin = new JButton("9");

    JButton fou = new JButton("4");
    JButton fiv = new JButton("5");
    JButton six = new JButton("6");

    JButton one = new JButton("1");
    JButton two = new JButton("2");
    JButton tre = new JButton("3");

    JButton zro = new JButton("0");
    JButton dot = new JButton(".");

     public CalcGUIQ1() {
         super();
         setTitle("Calculator");
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         GridLayout layout = new GridLayout(2, 1, 10, 10);
         setLayout(layout);

         //add listeners
         dot.addActionListener(crunchNu);
         zro.addActionListener(crunchNu);
         one.addActionListener(crunchNu);
         two.addActionListener(crunchNu);
         tre.addActionListener(crunchNu);
         fou.addActionListener(crunchNu);
         fiv.addActionListener(crunchNu);
         six.addActionListener(crunchNu);
         sev.addActionListener(crunchNu);
         ate.addActionListener(crunchNu);
         nin.addActionListener(crunchNu);


         FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
         row1.add(number1);
         row1.setLayout(layout1);
         add(row1);

         GridLayout layout2 = new GridLayout(4, 3, 10, 10);
         row2.setLayout(layout2);

    row2.add (sev);
    row2.add (ate);
    row2.add (nin);

    row2.add (fou);
    row2.add (fiv);
    row2.add (six);

    row2.add (one);
    row2.add (two);
    row2.add (tre);

    row2.add (zro);
    row2.add (dot);

    add(row2);
    pack();
    setVisible(true);
     }
     public static void main(String[] arguments) {
         CalcGUIQ1 frame = new CalcGUIQ1();
         }
}


HERE'S THE SECOND FILE:

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

public class CrunchQ1 implements ActionListener{
     CalcGUIQ1 gui;
     public CrunchQ1(CalcGUIQ1 in) {
     gui = in;
    }
public void actionPerformed(ActionEvent event){
     String strng1 = "";
     String btn = event.getActionCommand();

            if (btn == ".") {strng1 += btn;}
       else if (btn == "0") {strng1 += btn;}
       else if (btn == "1") {strng1 += btn;}
       else if (btn == "2") {strng1 += btn;}
       else if (btn == "3") {strng1 += btn;}
       else if (btn == "4") {strng1 += btn;}
       else if (btn == "5") {strng1 += btn;}
       else if (btn == "6") {strng1 += btn;}
       else if (btn == "7") {strng1 += btn;}
       else if (btn == "8") {strng1 += btn;}
       else if (btn == "9") {strng1 += btn;}

        gui.number1.setText(strng1);
            }

}

[toc] | [next] | [standalone]


#15590

Frommarkspace <-@.>
Date2012-06-25 16:15 -0700
Message-ID<jsarei$tfl$1@dont-email.me>
In reply to#15589
On 6/25/2012 3:31 PM, bilsch wrote:
> I'm trying to make a program that works like the calculator in Windows
> Accessories - all input is from button clicks.  ...
 > Everytime the method is called the string is reinitialized
> with the result that my sequence of digits is only ever one digit long.
> My plan would work if I could initialize the string outside the method,
> however variable scope in Java doesn't allow it.


First, good job on making a very reasonable SSCCE.

Second, the trick to Java's scoping rules is to change the rules!

Move the string strng1 from inside the actionPerformed to outside, right 
below the CalcGUIQ1 gui; line.  Now Java's scoping rules help you rather 
than hinder you.

BTW, this looks like a homework problem, and it looks like you've been 
getting help on it.  Some if it is a bit sophisticated for someone who 
doesn't understand scoping.  Please try to talk to your instructor or a 
TA, they need to understand when you're having problems with your lessons.

[toc] | [prev] | [next] | [standalone]


#15591

Frombilsch <bilsch01@gmail.com>
Date2012-06-25 16:47 -0700
Message-ID<jsatb2$6ki$1@dont-email.me>
In reply to#15590
On 6/25/2012 4:15 PM, markspace wrote:
> On 6/25/2012 3:31 PM, bilsch wrote:
>> I'm trying to make a program that works like the calculator in Windows
>> Accessories - all input is from button clicks. ...
>  > Everytime the method is called the string is reinitialized
>> with the result that my sequence of digits is only ever one digit long.
>> My plan would work if I could initialize the string outside the method,
>> however variable scope in Java doesn't allow it.
>
>
> First, good job on making a very reasonable SSCCE.
>
> Second, the trick to Java's scoping rules is to change the rules!
>
> Move the string strng1 from inside the actionPerformed to outside, right
> below the CalcGUIQ1 gui; line. Now Java's scoping rules help you rather
> than hinder you.
>
> BTW, this looks like a homework problem, and it looks like you've been
> getting help on it. Some if it is a bit sophisticated for someone who
> doesn't understand scoping. Please try to talk to your instructor or a
> TA, they need to understand when you're having problems with your lessons.
>
Thanks for the help.  I could swear I tried that first but got error 
messages about static and non-static conflict problem. i will be taking 
Java in fall quarter.  Right now I'm working from "Learn Java in 24 
Hours"  I thought up the calculator project myself.  Thanks again.


Bill S.

[toc] | [prev] | [next] | [standalone]


#15592

Frommarkspace <-@.>
Date2012-06-25 18:10 -0700
Message-ID<jsb265$squ$1@dont-email.me>
In reply to#15591
On 6/25/2012 4:47 PM, bilsch wrote:

> Thanks for the help.  I could swear I tried that first but got error
> messages about static and non-static conflict problem. i will be taking
> Java in fall quarter.  Right now I'm working from "Learn Java in 24
> Hours"  I thought up the calculator project myself.  Thanks again.


It was the Q1 parts of your code that really tipped me off.  I suppose 
you could be planning on a future Q1 class, but it did make it look like 
you where taking a course over the summer.

BTW, another way to improve you code is to watch the big cascades of 
if-else statements.  For example this:

       if (btn == ".") {strng1 += btn;}
       else if (btn == "0") {strng1 += btn;}
       else if (btn == "1") {strng1 += btn;}
       else if (btn == "2") {strng1 += btn;}
       else if (btn == "3") {strng1 += btn;}
       else if (btn == "4") {strng1 += btn;}
       else if (btn == "5") {strng1 += btn;}
       else if (btn == "6") {strng1 += btn;}
       else if (btn == "7") {strng1 += btn;}
       else if (btn == "8") {strng1 += btn;}
       else if (btn == "9") {strng1 += btn;}

could all just be

   strng1 += btn;

because you don't do anything different for the different cases. 
Copy-paste statement like this should be avoid, because it creates 
relatively low value redundancy.  Try to combine common cases into one 
block.  It's easier to go back and modify later.


[toc] | [prev] | [next] | [standalone]


#15606

Frombilsch <bilsch01@gmail.com>
Date2012-06-26 13:04 -0700
Message-ID<jsd4l2$i8s$1@dont-email.me>
In reply to#15592
On 6/25/2012 6:10 PM, markspace wrote:
> On 6/25/2012 4:47 PM, bilsch wrote:
>
>> Thanks for the help. I could swear I tried that first but got error
>> messages about static and non-static conflict problem. i will be taking
>> Java in fall quarter. Right now I'm working from "Learn Java in 24
>> Hours" I thought up the calculator project myself. Thanks again.
>
>
> It was the Q1 parts of your code that really tipped me off. I suppose
> you could be planning on a future Q1 class, but it did make it look like
> you where taking a course over the summer.
>
> BTW, another way to improve you code is to watch the big cascades of
> if-else statements. For example this:
>
> if (btn == ".") {strng1 += btn;}
> else if (btn == "0") {strng1 += btn;}
> else if (btn == "1") {strng1 += btn;}
> else if (btn == "2") {strng1 += btn;}
> else if (btn == "3") {strng1 += btn;}
> else if (btn == "4") {strng1 += btn;}
> else if (btn == "5") {strng1 += btn;}
> else if (btn == "6") {strng1 += btn;}
> else if (btn == "7") {strng1 += btn;}
> else if (btn == "8") {strng1 += btn;}
> else if (btn == "9") {strng1 += btn;}
>
> could all just be
>
> strng1 += btn;
>
> because you don't do anything different for the different cases.
> Copy-paste statement like this should be avoid, because it creates
> relatively low value redundancy. Try to combine common cases into one
> block. It's easier to go back and modify later.
>
>
>
Now that you mention it I see how that would work.  However the actual 
program has many non-numeric buttons I don't want in the string - I 
better leave that alone for the present.

Thanks.    Bill S.

[toc] | [prev] | [next] | [standalone]


#15612

FromGene Wirchenko <genew@ocis.net>
Date2012-06-26 14:01 -0700
Message-ID<ii8ku7tii3r8fov25og69fp5ur18fn0d57@4ax.com>
In reply to#15606
On Tue, 26 Jun 2012 13:04:50 -0700, bilsch <bilsch01@gmail.com> wrote:

>On 6/25/2012 6:10 PM, markspace wrote:

[snip]

>> BTW, another way to improve you code is to watch the big cascades of
>> if-else statements. For example this:

[snip]

>> because you don't do anything different for the different cases.
>> Copy-paste statement like this should be avoid, because it creates
>> relatively low value redundancy. Try to combine common cases into one
>> block. It's easier to go back and modify later.

>Now that you mention it I see how that would work.  However the actual 
>program has many non-numeric buttons I don't want in the string - I 
>better leave that alone for the present.

     Actually, grouping would help.  If you know that more than one
case will be handled the same way, then you just have to concern
yourself with code in one place.  Suppose you later find that you have
to change that code.  One place vs. many.  The former is much easier.
Your code will also be shorter.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#15615

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-06-26 17:21 -0400
Message-ID<jsd95b$e0m$1@dont-email.me>
In reply to#15606
On 6/26/2012 4:04 PM, bilsch wrote:
> On 6/25/2012 6:10 PM, markspace wrote:
>>[...]
>> BTW, another way to improve you code is to watch the big cascades of
>> if-else statements. For example this:
>>
>> if (btn == ".") {strng1 += btn;}
>> else if (btn == "0") {strng1 += btn;}
>> else if (btn == "1") {strng1 += btn;}
>> else if (btn == "2") {strng1 += btn;}
>> else if (btn == "3") {strng1 += btn;}
>> else if (btn == "4") {strng1 += btn;}
>> else if (btn == "5") {strng1 += btn;}
>> else if (btn == "6") {strng1 += btn;}
>> else if (btn == "7") {strng1 += btn;}
>> else if (btn == "8") {strng1 += btn;}
>> else if (btn == "9") {strng1 += btn;}
>>
>> could all just be
>>
>> strng1 += btn;
>>
>> because you don't do anything different for the different cases.
>> Copy-paste statement like this should be avoid, because it creates
>> relatively low value redundancy. Try to combine common cases into one
>> block. It's easier to go back and modify later.
>>
>>
>>
> Now that you mention it I see how that would work.  However the actual
> program has many non-numeric buttons I don't want in the string - I
> better leave that alone for the present.

     You needn't use the same listener for the 7 key as for
the Backspace key ...

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#15680

Frombilsch <bilsch01@gmail.com>
Date2012-06-27 14:44 -0700
Message-ID<jsfuro$h7j$1@dont-email.me>
In reply to#15615
On 6/26/2012 2:21 PM, Eric Sosman wrote:
> On 6/26/2012 4:04 PM, bilsch wrote:
>> On 6/25/2012 6:10 PM, markspace wrote:
>>> [...]
>>> BTW, another way to improve you code is to watch the big cascades of
>>> if-else statements. For example this:
>>>
>>> if (btn == ".") {strng1 += btn;}
>>> else if (btn == "0") {strng1 += btn;}
>>> else if (btn == "1") {strng1 += btn;}
>>> else if (btn == "2") {strng1 += btn;}
>>> else if (btn == "3") {strng1 += btn;}
>>> else if (btn == "4") {strng1 += btn;}
>>> else if (btn == "5") {strng1 += btn;}
>>> else if (btn == "6") {strng1 += btn;}
>>> else if (btn == "7") {strng1 += btn;}
>>> else if (btn == "8") {strng1 += btn;}
>>> else if (btn == "9") {strng1 += btn;}
>>>
>>> could all just be
>>>
>>> strng1 += btn;
>>>
>>> because you don't do anything different for the different cases.
>>> Copy-paste statement like this should be avoid, because it creates
>>> relatively low value redundancy. Try to combine common cases into one
>>> block. It's easier to go back and modify later.
>>>
>>>
>>>
>> Now that you mention it I see how that would work. However the actual
>> program has many non-numeric buttons I don't want in the string - I
>> better leave that alone for the present.
>
> You needn't use the same listener for the 7 key as for
> the Backspace key ...
>
The listeners all call a statement in the second file:

public void actionPerformed(ActionEvent event)

so I don't understand how a button listener can be different from 
another button listener.

Could you explain please?

TIA   Bill S.

[toc] | [prev] | [next] | [standalone]


#15681

FromLew <lewbloch@gmail.com>
Date2012-06-27 14:56 -0700
Message-ID<483152cf-36d3-4dfc-8cb3-8acd6337ff40@googlegroups.com>
In reply to#15680
bilsch wrote:
> Eric Sosman wrote:
>> bilsch wrote:
>>> markspace wrote:
>>>> [...]
>>>> BTW, another way to improve you code is to watch the big cascades of
>>>> if-else statements. For example this:
>>>>
>>>> if (btn == ".") {strng1 += btn;}
>>>> else if (btn == "0") {strng1 += btn;}
>>>> else if (btn == "1") {strng1 += btn;}
>>>> else if (btn == "2") {strng1 += btn;}
>>>> else if (btn == "3") {strng1 += btn;}
>>>> else if (btn == "4") {strng1 += btn;}
>>>> else if (btn == "5") {strng1 += btn;}
>>>> else if (btn == "6") {strng1 += btn;}
>>>> else if (btn == "7") {strng1 += btn;}
>>>> else if (btn == "8") {strng1 += btn;}
>>>> else if (btn == "9") {strng1 += btn;}
>>>>
>>>> could all just be
>>>>
>>>> strng1 += btn;
>>>>
>>>> because you don't do anything different for the different cases.
>>>> Copy-paste statement like this should be avoid, because it creates
>>>> relatively low value redundancy. Try to combine common cases into one
>>>> block. It's easier to go back and modify later.
>>>>
>>> Now that you mention it I see how that would work. However the actual
>>> program has many non-numeric buttons I don't want in the string - I
>>> better leave that alone for the present.
>>
>> You needn't use the same listener for the 7 key as for
>> the Backspace key ...
>>
> The listeners all call a statement in the second file:
> 
> public void actionPerformed(ActionEvent event)
> 
> so I don't understand how a button listener can be different from 
> another button listener.
> 
> Could you explain please?

Do the Backspace and "7" keys perform the same steps in response to the 
action?

If not, why are they calling the same method?

If so, huh?

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#15683

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-06-27 18:13 -0400
Message-ID<jsg0he$rab$1@dont-email.me>
In reply to#15680
On 6/27/2012 5:44 PM, bilsch wrote:
> On 6/26/2012 2:21 PM, Eric Sosman wrote:
>>[...]
>> You needn't use the same listener for the 7 key as for
>> the Backspace key ...
>>
> The listeners all call a statement in the second file:
>
> public void actionPerformed(ActionEvent event)
>
> so I don't understand how a button listener can be different from
> another button listener.
>
> Could you explain please?

     In your code there's a class called CrunchQ1, and because it
implements the ActionListener interface you can (and do) tell
your buttons to use an instance of that class as their action
listeners:

	CrunchQ1 crunchNu = new CrunchQ1(this);
	...
	dot.addActionListener(crunchNu);
	zro.addActionListener(crunchNu);
	one.addActionListener(crunchNu);
	...

     What I'm suggesting is that you could perfectly have another
class, maybe CrunchQ2, that also implements ActionListener but does
something different in its ActionPerformed method.  Then you could
make yourself an instance of the CrunchQ2 class, and use it instead
of the CrunchQ1 instance for some of your buttons:

	CrunchQ1 crunchNu = new CrunchQ1(this);
	CrunchQ2 dotty = new CrunchQ2(...whatever...);
	...
	dot.addActionListener(dotty);
	zro.addActionListener(crunchNu);
	one.addActionListener(crunchNu);
	...

     If you have other buttons with idiosyncratic behaviors, you
could give them their own ActionListener implementations, too.
There is no reason in the world why every button in your program
should share the same listener!

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#15695

Frombilsch <bilsch01@gmail.com>
Date2012-06-27 18:48 -0700
Message-ID<jsgd69$gj$1@dont-email.me>
In reply to#15683
On 6/27/2012 3:13 PM, Eric Sosman wrote:
> On 6/27/2012 5:44 PM, bilsch wrote:
>> On 6/26/2012 2:21 PM, Eric Sosman wrote:
>>> [...]
>>> You needn't use the same listener for the 7 key as for
>>> the Backspace key ...
>>>
>> The listeners all call a statement in the second file:
>>
>> public void actionPerformed(ActionEvent event)
>>
>> so I don't understand how a button listener can be different from
>> another button listener.
>>
>> Could you explain please?
>
> In your code there's a class called CrunchQ1, and because it
> implements the ActionListener interface you can (and do) tell
> your buttons to use an instance of that class as their action
> listeners:
>
> CrunchQ1 crunchNu = new CrunchQ1(this);
> ...
> dot.addActionListener(crunchNu);
> zro.addActionListener(crunchNu);
> one.addActionListener(crunchNu);
> ...
>
> What I'm suggesting is that you could perfectly have another
> class, maybe CrunchQ2, that also implements ActionListener but does
> something different in its ActionPerformed method. Then you could
> make yourself an instance of the CrunchQ2 class, and use it instead
> of the CrunchQ1 instance for some of your buttons:
>
> CrunchQ1 crunchNu = new CrunchQ1(this);
> CrunchQ2 dotty = new CrunchQ2(...whatever...);
> ...
> dot.addActionListener(dotty);
> zro.addActionListener(crunchNu);
> one.addActionListener(crunchNu);
> ...
>
> If you have other buttons with idiosyncratic behaviors, you
> could give them their own ActionListener implementations, too.
> There is no reason in the world why every button in your program
> should share the same listener!
>
Thanks for the info.

[toc] | [prev] | [next] | [standalone]


#15684

FromPatricia Shanahan <pats@acm.org>
Date2012-06-27 15:21 -0700
Message-ID<1u-dnQTm2NDQGnbSnZ2dnUVZ_rydnZ2d@earthlink.com>
In reply to#15680
On 6/27/2012 2:44 PM, bilsch wrote:
...
> The listeners all call a statement in the second file:
>
> public void actionPerformed(ActionEvent event)
>
> so I don't understand how a button listener can be different from
> another button listener.

You can write several different implementing classes for an interface
such as ActionListener. You can then create different instances of the
same implementing class.

For example, you could have a NumberKeyListener class that implements
ActionListener, and takes a constructor parameter that tells it the
value associated with its button. On the other hand, I would have a
different class for dealing with backspace.

Code like:

one.addActionListener(new NumberKeyListener(1));
two.addActionListener(new NumberKeyListener(2));

would get rid of a lot of checking which button was pressed.

Of course, as soon as you learn about arrays this will all get simpler
and shorter.

Patricia


[toc] | [prev] | [next] | [standalone]


#15685

FromJail Bush Cronies <do.not@ask.me>
Date2012-06-27 18:46 -0400
Message-ID<jsg2h5$4o0$1@speranza.aioe.org>
In reply to#15684
On 27/06/2012 6:21 PM, Patricia Shanahan wrote:
> On 6/27/2012 2:44 PM, bilsch wrote:
> ...
>> The listeners all call a statement in the second file:
>>
>> public void actionPerformed(ActionEvent event)
>>
>> so I don't understand how a button listener can be different from
>> another button listener.
>
> You can write several different implementing classes for an interface
> such as ActionListener. You can then create different instances of the
> same implementing class.
>
> For example, you could have a NumberKeyListener class that implements
> ActionListener, and takes a constructor parameter that tells it the
> value associated with its button. On the other hand, I would have a
> different class for dealing with backspace.
>
> Code like:
>
> one.addActionListener(new NumberKeyListener(1));
> two.addActionListener(new NumberKeyListener(2));
>
> would get rid of a lot of checking which button was pressed.

Another option uses the ActionEvent the listener gets passed, a 
parameter that's frequently ignored:

public void actionPerformed (ActionEvent e) {
     JButton btn = (JButton)(e.getSource());
     String btnTxt = btn.getText();
     // btnTxt should be "0", "1", ..., or "9".
     // Do something with it.
}

Of course, this action listener will blow up if used on something other 
than a JButton. You also might not like the results if you later 
localize the button labels using a ResourceBundle or something. :) In 
the event that that sort of thing is likely, the numeric-parameter 
approach Patricia suggested is more robust. Another possibility is:

private static Map<JButton,Integer> buttonMap;

...

for (int i = 0; i < 10; i++) {
     JButton btn = new JButton("" + i);
     btn.addActionListener(new NumberKeyListener());
     buttonMap.add(btn,i);
}

...

public void actionPerformed (ActionEvent e) {
     int btnNum = buttonMap.get(e.getSource());
}

Note: will throw NPE if attached to something other than a button that's 
in buttonMap. Also, for buttonMap to be visible in actionPerformed, 
NumberKeyListener needs to be a nested class of the class with the rest 
of this code.

[toc] | [prev] | [next] | [standalone]


#15616

Frommarkspace <-@.>
Date2012-06-26 14:25 -0700
Message-ID<jsd9cf$hha$1@dont-email.me>
In reply to#15606
On 6/26/2012 1:04 PM, bilsch wrote:

> Now that you mention it I see how that would work.  However the actual
> program has many non-numeric buttons I don't want in the string - I
> better leave that alone for the present.


A couple of things.  First, even if true, it's better to do something 
like this:

   if( btn == "1" || btn == "2" || btn == "3" ... )
   {
       // one single case here...
   }

Than it is to use many different if-blocks.  Same action for different 
inputs, you want to use one code block to implement that action.

One other important point I'd like to make is that Java strings don't 
normally compare with ==.  You have to use .equals() instead.  Your code 
works now because all of the strings are in a single file, but as your 
program grows == will no longer work for you.

This is the normal, and more correct, way to do it:

   if( btn.equals( "1" ) || btn.equals( "2" ) || ... )
   {
      strng1 += btn;
   }

Lastly, given your specific use case, there's a cheap quick way to cut 
down on verbosity.  It involves knowing the API well, but String and 
Math (and a few others) are two APIs that you should memorize eventually 
to be a good Java programmer.  (Other APIs it's OK to have to consult 
the documentation periodically.)

   String digits = "0123456789.";
   String opers = "+-/*";
   String clear = "Clear";

   if( digits.contains( btn ) ) {
      strng1 += btn;
   } else if( opers.contains( btn ) ) {
      // go do some math
   } else if( clear.equals( btn ) ) {
      strng1 = "0";
   }

Note the above block is untested. Caveat emptor.

Later you'll be able to do the same thing with objects that aren't 
strings with the Set class.

   Set stuff1 = ...

   if( stuff1.contains( potentialMember ) ) {
      // take an action...
   }






[toc] | [prev] | [next] | [standalone]


#15620

FromLew <lewbloch@gmail.com>
Date2012-06-26 14:32 -0700
Message-ID<05a0cbeb-c97e-436b-9daf-e158445b0ac7@googlegroups.com>
In reply to#15616
markspace wrote:
> bilsch wrote:
> 
> > Now that you mention it I see how that would work.  However the actual
> > program has many non-numeric buttons I don't want in the string - I
> > better leave that alone for the present.
> 
> 
> A couple of things.  First, even if true, it's better to do something 
> like this:
> 
>    if( btn == "1" || btn == "2" || btn == "3" ... )
>    {
>        // one single case here...
>    }
> 
> Than it is to use many different if-blocks.  Same action for different 
> inputs, you want to use one code block to implement that action.
> 
> One other important point I'd like to make is that Java strings don't 
> normally compare with ==.  You have to use .equals() instead.  Your code 
> works now because all of the strings are in a single file, but as your 
> program grows == will no longer work for you.
> 
> This is the normal, and more correct, way to do it:
> 
>    if( btn.equals( "1" ) || btn.equals( "2" ) || ... )
>    {
>       strng1 += btn;
>    }

  switch (btn) 
  {
    case "1":
    case "2": 
        doCaseOneAndTwo();
        break;

    case "3": 
        doCaseThree();
        break;

    default:
        doAllOtherCases();
        break;
  }

> Lastly, given your specific use case, there's a cheap quick way to cut 
> down on verbosity.  It involves knowing the API well, but String and 
> Math (and a few others) are two APIs that you should memorize eventually 
> to be a good Java programmer.  (Other APIs it's OK to have to consult 
> the documentation periodically.)

Also you should be very, very familiar with the collections classes 
(mostly in java.util.*).

>    String digits = "0123456789.";
>    String opers = "+-/*";
>    String clear = "Clear";
> 
>    if( digits.contains( btn ) ) {
>       strng1 += btn;
>    } else if( opers.contains( btn ) ) {
>       // go do some math
>    } else if( clear.equals( btn ) ) {
>       strng1 = "0";
>    }
> 
> Note the above block is untested. Caveat emptor.
> 
> Later you'll be able to do the same thing with objects that aren't 
> strings with the Set class.
> 
>    Set stuff1 = ...
> 
>    if( stuff1.contains( potentialMember ) ) {
>       // take an action...
>    }

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#15621

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-06-26 17:49 -0400
Message-ID<4fea2e75$0$292$14726298@news.sunsite.dk>
In reply to#15620
On 6/26/2012 5:32 PM, Lew wrote:
> markspace wrote:
>> bilsch wrote:
>>
>>> Now that you mention it I see how that would work.  However the actual
>>> program has many non-numeric buttons I don't want in the string - I
>>> better leave that alone for the present.
>>
>>
>> A couple of things.  First, even if true, it's better to do something
>> like this:
>>
>>     if( btn == "1" || btn == "2" || btn == "3" ... )
>>     {
>>         // one single case here...
>>     }
>>
>> Than it is to use many different if-blocks.  Same action for different
>> inputs, you want to use one code block to implement that action.
>>
>> One other important point I'd like to make is that Java strings don't
>> normally compare with ==.  You have to use .equals() instead.  Your code
>> works now because all of the strings are in a single file, but as your
>> program grows == will no longer work for you.
>>
>> This is the normal, and more correct, way to do it:
>>
>>     if( btn.equals( "1" ) || btn.equals( "2" ) || ... )
>>     {
>>        strng1 += btn;
>>     }
>
>    switch (btn)
>    {
>      case "1":
>      case "2":
>          doCaseOneAndTwo();
>          break;
>
>      case "3":
>          doCaseThree();
>          break;
>
>      default:
>          doAllOtherCases();
>          break;
>    }

If Java version >= 7.

Arne

[toc] | [prev] | [next] | [standalone]


#15624

FromLew <lewbloch@gmail.com>
Date2012-06-26 15:20 -0700
Message-ID<26dc29ad-d5a8-4d04-91d6-57311ff23c83@googlegroups.com>
In reply to#15621
Arne Vajhøj wrote:
> Lew wrote:
>> markspace wrote:
>>> bilsch wrote:
>>>
>>>> Now that you mention it I see how that would work.  However the actual
>>>> program has many non-numeric buttons I don't want in the string - I
>>>> better leave that alone for the present.
>>>
>>>
>>> A couple of things.  First, even if true, it's better to do something
>>> like this:
>>>
>>>     if( btn == "1" || btn == "2" || btn == "3" ... )
>>>     {
>>>         // one single case here...
>>>     }
>>>
>>> Than it is to use many different if-blocks.  Same action for different
>>> inputs, you want to use one code block to implement that action.
>>>
>>> One other important point I'd like to make is that Java strings don't
>>> normally compare with ==.  You have to use .equals() instead.  Your code
>>> works now because all of the strings are in a single file, but as your
>>> program grows == will no longer work for you.
>>>
>>> This is the normal, and more correct, way to do it:
>>>
>>>     if( btn.equals( "1" ) || btn.equals( "2" ) || ... )
>>>     {
>>>        strng1 += btn;
>>>     }
>>
>>    switch (btn)
>>    {
>>      case "1":
>>      case "2":
>>          doCaseOneAndTwo();
>>          break;
>>
>>      case "3":
>>          doCaseThree();
>>          break;
>>
>>      default:
>>          doAllOtherCases();
>>          break;
>>    }
> 
> If Java version >= 7.

Which the OP had better be using.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#15687

Frombilsch <bilsch01@gmail.com>
Date2012-06-27 17:30 -0700
Message-ID<jsg8i9$a07$1@dont-email.me>
In reply to#15624
On 6/26/2012 3:20 PM, Lew wrote:
> Arne Vajhøj wrote:
>> Lew wrote:
>>> markspace wrote:
>>>> bilsch wrote:
>>>>
>>>>> Now that you mention it I see how that would work.  However the actual
>>>>> program has many non-numeric buttons I don't want in the string - I
>>>>> better leave that alone for the present.
>>>>
>>>>
>>>> A couple of things.  First, even if true, it's better to do something
>>>> like this:
>>>>
>>>>      if( btn == "1" || btn == "2" || btn == "3" ... )
>>>>      {
>>>>          // one single case here...
>>>>      }
>>>>
>>>> Than it is to use many different if-blocks.  Same action for different
>>>> inputs, you want to use one code block to implement that action.
>>>>
>>>> One other important point I'd like to make is that Java strings don't
>>>> normally compare with ==.  You have to use .equals() instead.  Your code
>>>> works now because all of the strings are in a single file, but as your
>>>> program grows == will no longer work for you.
>>>>
>>>> This is the normal, and more correct, way to do it:
>>>>
>>>>      if( btn.equals( "1" ) || btn.equals( "2" ) || ... )
>>>>      {
>>>>         strng1 += btn;
>>>>      }
>>>
>>>     switch (btn)
>>>     {
>>>       case "1":
>>>       case "2":
>>>           doCaseOneAndTwo();
>>>           break;
>>>
>>>       case "3":
>>>           doCaseThree();
>>>           break;
>>>
>>>       default:
>>>           doAllOtherCases();
>>>           break;
>>>     }
>>
>> If Java version>= 7.
>
> Which the OP had better be using.
>
I am using 1.7.0_04

[toc] | [prev] | [next] | [standalone]


#15688

FromLew <lewbloch@gmail.com>
Date2012-06-27 18:09 -0700
Message-ID<440beafb-abc1-498c-a3ae-6ea4001dfabf@googlegroups.com>
In reply to#15687
bilsch wrote:
> Lew wrote:
>> Arne Vajhøj wrote:
>>> If Java version>= 7.
>>
>> Which the OP had better be using.
>>
> I am using 1.7.0_04

It is best for now to stick with the latest Java version, which is Java 7 
as you have (although you should upgrade to Java 7u5 - minor version 
numbers represent bug and security fixes, things you don't want to 
miss).

Out in the wild you'll be asked to use all kinds of old or funky (or both) 
Java versions. Many of us have had to work on Java 1.4 projects, even 
recently. But while you're learning, stay with the current version.

This is in line with mimicking good code even for areas you haven't 
studied yet, such as starting your GUI on the EDT.

As they said in Star Trek (the original series) (paraphrased): 
It is easier for a civilized person to pass as a barbarian 
than for a barbarian to pass as a civilized person.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#15783

FromWanja Gayk <brixomatic@yahoo.com>
Date2012-07-02 11:21 +0200
Message-ID<MPG.2a5b85d036f50cae989712@202.177.16.121>
In reply to#15592
In article <jsb265$squ$1@dont-email.me>, -@. says...

> BTW, another way to improve you code is to watch the big cascades of 
> if-else statements.  For example this:
> 
>        if (btn == ".") {strng1 += btn;}
>        else if (btn == "0") {strng1 += btn;}
>        else if (btn == "1") {strng1 += btn;}
>        else if (btn == "2") {strng1 += btn;}
>        else if (btn == "3") {strng1 += btn;}
>        else if (btn == "4") {strng1 += btn;}
>        else if (btn == "5") {strng1 += btn;}
>        else if (btn == "6") {strng1 += btn;}
>        else if (btn == "7") {strng1 += btn;}
>        else if (btn == "8") {strng1 += btn;}
>        else if (btn == "9") {strng1 += btn;}
> 
> could all just be
> 
>    strng1 += btn;
> 
> because you don't do anything different for the different cases. 

Actually I would prefer to write something like:
if(".0123456789".contains(btn)){
 strng1 += btn
}

Kind regards,
Wanja


-- 
..Alesi's problem was that the back of the car was jumping up and down 
dangerously - and I can assure you from having been teammate to 
Jean Alesi and knowing what kind of cars that he can pull up with, 
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


Page 1 of 4  [1] 2 3 4  Next page →

Back to top | Article view | comp.lang.java.programmer


csiph-web