8 Replies Latest reply: Jun 4, 2007 12:20 AM by 807600 RSS

    Calling a Constructor...?

    807600
      That probably wasn't the best subject but my problem is I am trying to call a constructor of a class from a different class. However, it is saying it cannot find symbol when I see the class right in front of my eyes! Here's the codes :

      This is the calling method inside CalendarFrame.java
          private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
              DateAD aDay = new DateAD();
              
              aDay.DateAD(); //This is where it says Cannot Find Symbol : method DateAD()
      This is the method that is being called inside DateAD.java
          public DateAD()
          {
              setCurrentDate();
              setDayOfYear();
              setDayOfWeek();
          }
      All help is appreciated! I feel like I am missing something easy...
        • 1. Re: Calling a Constructor...?
          807600
          In Soviet Russia constructor calls you!

          Sorry, just wanted to use that.

          A constructor is not a method. So this line
          aDay.DateAD();
          won't work. But that;s okay because you already called the constructor here.
          DateAD aDay = new DateAD();
          In other words just get rid of the bad line and it will work.
          • 2. Re: Calling a Constructor...?
            807600
            Oh, thanks haha. Simpler than I thought it would have been.

            I have another question if you will :

            I'll start with the code because it'll be easier to explain this way

            In CalendarFrame.java :
                
            private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
                    DateAD aDay = new DateAD();
                    aDay.year = Short.parseShort((String)(jComboBox2.getSelectedItem()));
                    
                    BasicCalendar basic = new BasicCalendar();
                    
                    jTextArea1.setText(basic.toString());
                }
            Here's the constructor I'm calling from DateAD.java
                public DateAD()
                {
                    setCurrentDate();
                    setDayOfYear();
                    setDayOfWeek();
                }
            I am printing calendarText into a textArea with the basic.toString() method onto the CalendarFrame.java GUI. An array successfully is printed, but the aDay.year variable that should've been set according to the selected item in the combobox up in CalendarFrame.java's method is stuck at 2007 and will not change whatever year I select from the combobox, which holds the items "2000" to "2010". What could possibly be wrong? Everything is so confusing to me! I honestly don't know how I'm supposed to get my head around this stuff.
            • 3. Re: Calling a Constructor...?
              807600
              I'm not too sure what you are trying to do or what the problem is but I can see that there is absolutely no magic happening
              between aDay and basic. Is there supposed to be? If yes then you will need to add code. If no, then why do you create
              aDay if you never do anything with it apart from set it's year?
              • 4. Re: Calling a Constructor...?
                807600
                Okay I'm going to abandon Matisse and use a standard TextEditor because Matisse is kind of annoying me. Sorry if this is a lot of information I'm just trying to explain as much as I can.

                Anyway, here's about all the information about the project I am working on right now. First of all, I have DateAD.java that holds information about everything about each year. This information includes the starting day of year, the starting day of the week, it's basically like a virtual calendar, there's a good way to think about it! Anyway, it has multiple constructors depending on how much information you have. If you have absolutely none, it sets all the variables such as month, year, and such to the current date on your computer. If it has a given month from the user as a parameter, it sets the current date but changes the month, and so on. This class is like a calendar that is waiting for information from the user and then it can look it up.

                NOW, BasicCalendar.java creates and sets an array that looks like a calendar. The array is 6x7 and it gets information from DateAD.java about dates and such and makes an array appropriately. So it makes a virtual visual component of DateAD.java sheesh this is really hard to explain! In addition to DateAD.java it also sets the starting day of the week of each month.

                OKAY, that's like all the information about this whole project haha. The reason why I'm stressing out so much over the past about 6 hours is because I need to make a GUI that looks exactly like this :

                http://www.edcc.edu/faculty/paul.bladek/images/151p4.jpg

                As you can see, the textfield and combo boxes above the array set the variables day, month, and year. Then, the textArea below holds the array that BasicCalendar.java creates. Then the label on the side just holds what date the user inputted. There's really nothing fancy to the GUI, you just tell it a date and it pulls up a correct calendar of the month of the year you are looking for. To me, it sounds like such a simple thing but it's not! It's like freakish hell for me and I can't do anything because I really suck at GUI's haha..

                I'm going to try to make a GUI again from a blank text editor again and see how far I can go. This is a lot harder than I imagined it to be ... eep.

                Message was edited by:
                ApRiX
                • 5. Re: Calling a Constructor...?
                  807600
                  The array is 6x7 and it gets information from DateAD.java about dates and such
                  This is what I was saying in reply #3. There is no connection between your two objects, so how is BasicCalender supposed to know about DateAD. Maybe you need something like this:
                  BasicCalendar basic = new BasicCalendar(aDate);
                  // or
                  BasicCalendar basic - new BasicCalendar();
                  basic.setDate(aDate);
                  • 6. Re: Calling a Constructor...?
                    807600
                    The DateAD class we used for our previous project. It was for a much simpler command-line based project but we need it for this project since it holds the definitions of months and years. The BasicCalendar class is like an add-on to the DateAD Class, but our teacher doesn't want us to group them together I guess it has something to do with what our teacher wants us to learn from doing this. Anyway, here's a sample constructor from the DateAD class
                        public DateAD(short monthIn)
                        {
                            setCurrentDate();
                            if(monthIn >= 0 || monthIn < 12)
                                month = monthIn;
                            setDayOfYear();
                            setDayOfWeek();
                        }
                            public BasicCalendar(int monthIn) 
                        {
                            aDay = new DateAD((short)monthIn);
                            startDayOfWeek = (short)((aDay.getDayOfWeek() - (aDay.getDayOfMonth() - 1) + 35) % 7);
                            numberOfDays = aDay.daysInMonth(aDay.getMonth());
                            
                            setArray();
                        }
                    As you can see, BasicCalendar just adds something onto what DateAD already has. I guess it makes DateAD easier to be translated onto a GUI. Err but right now I'm just trying to make a plan of how this project will work and I'll try to translate the plan into code a little later. This project is really giving me a headache, heh.
                    • 7. Re: Calling a Constructor...?
                      807600
                      private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
                              DateAD aDay = new DateAD();
                              aDay.year = Short.parseShort((String)(jComboBox2.getSelectedItem()));
                              
                              BasicCalendar basic = new BasicCalendar();
                              
                              jTextArea1.setText(basic.toString());
                          }
                      I think you still don't understand what I'm getting at. The DateAD object you created in the above snippet of code, has absolutely zero,nada,zilch to do with
                      the DataAD object created in your BasicCalendar class.
                      • 8. Re: Calling a Constructor...?
                        807600
                        Hm... yes I look at it now and I figure I didn't need to put in DateAD aDay because the constructor for BasicCalendar already had that line inside it. If I called BasicCalendar by itself it would have made an object for DateAD. I've started anew on the GUI part so that's not of our concern as of right now! Sorry but I really had some disorganized code and I had no clue what I was doing. I still have a feeling that this shouldn't be that hard... it is only our first ever GUI program after all.