Transfer of Integer!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KRAZY
    New Member
    • Nov 2009
    • 2

    Transfer of Integer!

    Hello guys......I have a problem with my code, its more like I am stuck to continue further if I cannot find a way or an alternative.
    The problem is I have an integer X in a class called
    Langkawi, and I want the same integer to be present in another class
    called 'SA'.
    In this way, as int X changes in Langkawi class, changes will apply to
    int X in SA class as well. Just like the Paste special in MS
    Excel. I would be grateful to anyone who comes to the rescue because my due date is closing in very fast...:-)
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    It's hard to say without knowing what you are trying to do (as distinct from how you are trying to do it).

    a) The instance of Langkawi which has the variable x of interest could be given a reference to the instance of SA that should know about changes of value. Have all changes to x occur through a "mutator" method (setX()) which calls some method of SA to notify it of the change.

    a2) Make x a bound property of Langkawi.

    b) The instance of SA that is to use the value of the variable x could have a reference to the instance of Langkawi which has the value. The SA instance should use an "accessor" method (getX()) whenever it wants the value.

    c) The design could be changed so that SA no longer depends so strongly on Langkawi. Whatever behaviour it is that SA wants could be expressed as an interface that Langkawi implements. As in (b) the SA instance is given a reference to the Langkawi instance of interest (now as an instance of the interface). Whenever the SA instance wants the behaviour to occur it doesn't ask the Langkawi instance what its x value is, but tells it to perform the desired behaviour.

    Comment

    • KRAZY
      New Member
      • Nov 2009
      • 2

      #3
      Hey pbrockway2!
      Thanks for the knowledge; I appreciate your help on this one, atleast I got a headway on how to go about it. Let me apply the steps and see what I shall foster from it; in case of any problems, I ought to know what to do from here on.

      Once again, my sincere gratitude yooooo! Peace

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        You're welcome. Notice that the methods listed are alternatives not a sequence of steps to be performed...

        Anyway here's an example (of (b)):

        Code:
        class Sky {
                // Sky has an integer value as a field.  This value may change.
            private int color;
        
            public void setColor(int c) {
                color = c;
            }
        
            public int getColor() {
                return color;
            }
        }
        
            // Instances of Place want to be able to access the integer value
            // of a particular Sky instance.  And they always want the current
            // value
        class Place {
            private String name;
            private Sky sky;
        
                // In the Place constructor each instance is given a reference to
                // an instance of Sky.
            public Place(String name, Sky sky) {
                this.name = name;
                this.sky = sky;
            }
        
                // The Place instance will use an "accessor" method - getColor() -
                // every time it wants the sky colour
            public void report() {
                System.out.printf("At %s the sky is %h.%n", name, sky.getColor());
            }
        }
        
        public class SharedIntEg {
            public static void main(String[] args) {
                Sky sky = new Sky();
                sky.setColor(0x4545FF);
        
                Place place = new Place("Somewhere", sky);
                place.report();
                
                // Now let's change the sky's colour and see what the
                // place reports.
        
                sky.setColor(0x333333);
                place.report();
            }
        }

        Comment

        Working...