Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Object-oriented mess

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Object-oriented mess

    Long story short:

    I am writing an object-oriented calculator that I hope will have some of the functionality of the Windows calculator. So far, I have written two GUI's; one displays in an undesirable way but is fully integrated with the logic module and therefore can perform basic arithmetic calculations; the other displays as I want it to display but is not integrated with the logic module and therefore does not perform any calculations.

    I have some idea why the latter GUI is not performing as I want it to, but I have less of a idea how to get it to do so.

    The code is rather long so I'm not sure exactly how much to post, but here is some of it:

    import javax.swing.*;
     
    class CalculatorScreen extends JPanel
    {
        JTextField screen = new JTextField("0", 15);
     
        CalculatorScreen()
        {
            add(screen);
        }
    }
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class NumberPad extends JPanel
    {
        JButton button;
        ActionListener ariLis;
     
        NumberPad()
        {
            setLayout(new GridLayout(4, 4));
     
            button = new JButton("7");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("8");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("9");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("+");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("4");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("5");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("6");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("-");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("1");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("2");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("3");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("*");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton(".");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("0");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("=");
            button.addActionListener(ariLis);
            add(button);
     
            button = new JButton("/");
            button.addActionListener(ariLis);
            add(button);
       }
    }
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Stack;
    import javax.swing.JTextField;
    import java.lang.*;
     
    public class ArithmeticListener implements ActionListener
    {
        boolean optPsh = false;
        boolean pntPsh = false;
        double fOpd;
        double sOpd;
        double res;
        String ariStr = "";
        String digStr = "";
        String pntStr = "";
        String opdStr = "";
        String curOpdStr = "";
        String prvOpdStr = "";
        String curOptStr = "";
        String prvOptStr = "";
        Stack<String> expStck = new Stack<String> ();
        Stack<String> opdStck = new Stack<String> ();
        Stack<String> optStck = new Stack<String> ();
        JTextField scrn;
     
        ArithmeticListener(CalculatorGUI calGUI)
        {
            scrn = calGUI.screen;
        }
     
        ArithmeticListener(GUI gui)
        {
            scrn = gui.calcScrn.screen;
        }
     
        public void actionPerformed(ActionEvent ae)
        {
            /*enter numbers and perform calculations*/
        }
    }

    The basic problem, as I understand it, is that when the main method in the GUI class calls the GUI the GUI construct ariLis isn't initialized, so when it is added to the buttons in the number pad it is added as an object reference without a referent object. (Why isn't that throw a NullPointerException?) What I am not so sure how to do is to add the actual ArithmeticListener object to the buttons in the number pad after the GUI has been initialized.

    Any suggestions?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Object-oriented mess

    (Why isn't that throw a NullPointerException?)
    It's not invalid to add a null ActionListener to a button. I think the behavior is to do nothing if it's null, but it's really undefined behavior.

    I would recommend creating AriLis before constructing NumPad, then pass that object to NumPad as a parameter. That way you can initialize AriLis to that value and not have to worry about trying to back-track the buttons contained inside of NumPad.

    If this can't be done, then what I would recommend is having all the buttons being reference-able from NumPad (without having to dig through the inheritance chain to get at them). Then, create a method which will take in an ActionListener and adds that listener to all the buttons. The NumPad constructor should not add a null ActionListern to the buttons.

  3. #3
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Object-oriented mess

    Quote Originally Posted by helloworld922 View Post
    It's not invalid to add a null ActionListener to a button. I think the behavior is to do nothing if it's null, but it's really undefined behavior.
    Yeah, that's what the Java 6 Documentation says.

    I find it strange that one can add a null ActionListener to a Component but not a null Component to a Component. In other word, I think it's strange that add() fails with a null Component but that addActionListner() doesn't.

    Quote Originally Posted by helloworld922 View Post
    I would recommend creating AriLis before constructing NumPad, then pass that object to NumPad as a parameter. That way you can initialize AriLis to that value and not have to worry about trying to back-track the buttons contained inside of NumPad.
    I thought about it more after posting and passing ariLis as a parameter was what I thought might work.

    Thanks for the conformation.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Object-oriented mess

    I find it strange that one can add a null ActionListener to a Component but not a null Component to a Component. In other word, I think it's strange that add() fails with a null Component but that addActionListner() doesn't.
    Wishy-washy programming is what I think of The more I delve into the finer details of Java, the more I find these funky nooks and crannies that for some reason or another they never fixed (usually because of backwards compatibility).

Similar Threads

  1. Object oriented programming
    By jonnitwo in forum Object Oriented Programming
    Replies: 8
    Last Post: September 2nd, 2011, 12:18 PM
  2. Object-oriented applet
    By mjpam in forum Object Oriented Programming
    Replies: 26
    Last Post: September 15th, 2010, 06:43 AM
  3. TCP Client Server: Object Oriented
    By nffc luke in forum Object Oriented Programming
    Replies: 2
    Last Post: April 28th, 2010, 06:39 PM
  4. Object Oriented Programming request please helpp...
    By dini-x in forum Object Oriented Programming
    Replies: 3
    Last Post: April 1st, 2010, 12:57 AM
  5. Object Oriented program, no output
    By boardbreaker in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 17th, 2009, 11:11 PM