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 12 of 12

Thread: Data hiding and maintainability

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Data hiding and maintainability

    Hi, I have been working on a peice of code for a long time now and I really cant see how to solve the problem. I have gone over what have been tought and I really cant see how I can solve it!

    The code I am working on is:

    { /* Instance variables */ public int stockLevel; // The number of items of this product in stock which is updated // by the deliver() and acquire() methods. Objects of other // classes will need to find out its value.
     
    public String description; // The name of the product which will be set to a meaningful // string by the constructor, and then never changed. // Objects of other classes will need to find out its value.
     
    /** * Constructor for objects of class Product */ public Product(String aDescription) { super(); this.stockLevel = 0; this.description = aDescription; }
     
    /* Instance methods */
    /** * Reduce the stock level by the value of the argument. * Method presumes availability has already been checked. */ public void deliver(int anAmount) { this.stockLevel = this.stockLevel - anAmount; }


    What I need to do is Describe and explain the changes I would make to the code in order to improve its data hiding and aid maintainability.

    I have worked long and hard on this and I just cat crack it! For data hiding I was thinking/have been taught (not very far in the course yet) that I can declare the instance variables as private but I dont think this will work as it states within the explanation for the code that objects of other classes will need to find out the values of the instance variables!

    for maintainability I have been looking along the lines of reuse of code but im really stuck on this one. not sure if its because ive been racking my brains so hard on the data hiding part as I feel im very close on that.

    Please note that I am trying very hard with this and im not just looking for the easy way out. I jst feel that this is by far the best way for me to learn what I need to learn given that I cant answer the question after reading and re-reading the course books!


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Data hiding and maintainability

    Hello there,

    I just wanted to start off by saying you are thinking in the right way of changing the instance member to be private.

    If you are keen on how to figure out how to make it accessible by other classes have a look at the JavaBeans standard.

    Trail: JavaBeans(TM) (The Java™ Tutorials)

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    humdinger (November 19th, 2009)

  4. #3
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Data hiding and maintainability

    Hi, thankyou very much for the reply, however I cannot use anything I have not already been taught. I have been taught about encapsulation and i think that is what the question is asking me to use to solve the problem but the learning materials only show me how to hide data by setting declaring the variables as private.

    Thankyou again for the reply, I will be sure to click the thankyou button for you once the post is finished.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,356
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Data hiding and maintainability

    Quote Originally Posted by humdinger View Post
    Hi, thankyou very much for the reply, however I cannot use anything I have not already been taught. I have been taught about encapsulation and i think that is what the question is asking me to use to solve the problem but the learning materials only show me how to hide data by setting declaring the variables as private.
    The concepts of encapsulation and accessor modifiers are highly intertwined, so I'm surprised you learned about encapsulation without learning about accessor modifiers. Google "java encapsulation" and you will find a wealth of descriptions and examples

  6. The Following User Says Thank You to copeg For This Useful Post:

    humdinger (November 19th, 2009)

  7. #5
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Data hiding and maintainability

    Thankyou, you have put me in the right direction now I think. I will be working on this tomorow after work.

  8. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Data hiding and maintainability

    I dont think JavaBeans is something you havent been thought in that sense but have a look a the way a standard JavaBean is defined.

    public class MyBean {
     
        /** Creates a new instance of MyBean */
        public MyBean() {
        }
     
        /**
         * Holds value of property title.
         */
        private String title;
     
        /**
         * Getter for property title.
         * @return Value of property title.
         */
        public String getTitle() {
            return this.title;
        }
     
        /**
         * Setter for property title.
         * @param title New value of property title.
         */
        public void setTitle(String title) {
            this.title = title;
        }
    }

    In this code you can see how the instance member "title" is declared and how you expose it to the outside world.

    // Json

  9. The Following User Says Thank You to Json For This Useful Post:

    humdinger (November 20th, 2009)

  10. #7
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Data hiding and maintainability

    Hi I have been working hard on this and im startingto get somewhere now.

    Just to make you aware, i missed some of the code previously so the full code im working with is:

     
     public class Product 
     {
        /* Instance variables */ 
        public int stockLevel; 
        // The number of items of this product in stock which is updated 
        // by the deliver() and acquire() methods. Objects of other 
        // classes will need to find out its value.
     
     
    public String description; 
    // The name of the product which will be set to a meaningful 
    // string by the constructor, and then never changed. 
    // Objects of other classes will need to find out its value.
     
    /** Constructor for objects of class Product
     public Product(String aDescription) 
     { 
        super(); 
        this.stockLevel = 0; 
        this.description = aDescription; 
       }
     
     /* Instance methods */
     
     /** 
      * Reduce the stock level by the value of the argument. 
      * Method presumes availability has already been checked.
      */
     
       public void deliver(int anAmount) 
       {
          this.stockLevel = this.stockLevel - anAmount; 
       }
     
     /**
    * Increase the stock level by the value of the argument
    */
    public void acquire(int anAmount)
    {
    this.stockLevel = this.stockLevel + anAmount;
    }
     
    /**
    * Returns a string giving details of the product
    */
    public String productDetails()
    {
    return "Product " + this.description + ": " + this.stockLevel
             + " in stock.";
    } 
    }

    what I have come up with so far is I will declare the instance variables to private, this will improve data hiding. but to enable instances of other classes to find out their values I will need to create getter methods. The methods I will add to the end of the code are:

    /**
     * This will return the value of stockLevel
     */
     
    public int getStockLevel()
    {
        return this.stockLevel;
    }
     
    /**
     *  This will return the value of description
     */
     
    public String getDescription()
    {
        return this.description;
       }

    If this is correct, that the data hiding part out of the way and then I just need to work on the maintainability part.

    if someone could let me know if im on the right tracks it would be very appreciated.

    p.s this is sooo much clearer now, I feel really good about it now! cant beleive I forgot about the accessor modifiers! Just for the record, im doing this course after work each day so if iv had a hard day, some things dont sink in right away.

    Thankyou to everyone who has helped me so far.
    Last edited by humdinger; November 20th, 2009 at 01:35 PM.

  11. #8
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Data hiding and maintainability

    Hi, im also doing this course exactly the same module as yourself and im also struggling with this.

    My thought are the same as yours but im not sure you would use this.variable name

    /**
     * This will return the value of stockLevel
     */
     
    public int getStockLevel()
    {
        return this.stockLevel;
    }
     
    /**
     *  This will return the value of description
     */
     
    public String getDescription()
    {
        return this.description;
       }

    in this case would you not use

    /**
     * This will return the value of stockLevel
     */
     
    public int getStockLevel()
    {
        return Product.stockLevel;
    }
     
    /**
     *  This will return the value of description
     */
     
    public String getDescription()
    {
        return Product.description;
       }

    not sure my self just this is the way i was thinking as if you call the getDescription from another class wont that read this.variable thinking it is trying to get a variable from the class in which you are executing it from if you get what i mean.

  12. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,356
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Data hiding and maintainability

    Humdinger, looks as though you are on the right track.
    mds1256, in this instance (pun intended) I am guessing you want to access instance variables (the first code posting) and not class variables (static - the second code posting) See Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects) Sometimes the use of 'this' can seem a bit redundant, but the following example may illustrate a situation where it is important to use it:
    public void setDescription( String description ){
        //description = description;//wrong way, you are setting the parameter value to itself, do not do it this way
        this.description = description;//right way - you are setting the instance variable to the parameter value
    }

  13. #10
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Data hiding and maintainability

    Hi all,

    I think I have got the data hiding part done but I really cant see any way I could change the code aid maintainability! The only thing I know about maintainability is re-use of code and I cant see any code that I could re use in any of the methods in order to aid maintainability. If someone could point me in the right direction I would appreciate it very much as im really stuck on this and dont know what else to try!

  14. #11
    Junior Member
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Data hiding and maintainability

    humdinger - would you not need to change the existing methods to private also? are you changing the existing methods at all or leaving them like they are?

    Since I do not have all the code I'm not sure about this, anyone else??

    what course are you guys doing if you don't mind me asking? looks quite interesting. I am trying to teach myself from books thats all and would like to do a course I think.

    I'm not sure about improving maintainability sorry

    John

  15. #12
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Data hiding and maintainability

    Quote Originally Posted by copeg View Post
    Humdinger, looks as though you are on the right track.
    mds1256, in this instance (pun intended) I am guessing you want to access instance variables (the first code posting) and not class variables (static - the second code posting) See Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects) Sometimes the use of 'this' can seem a bit redundant, but the following example may illustrate a situation where it is important to use it:
    public void setDescription( String description ){
        //description = description;//wrong way, you are setting the parameter value to itself, do not do it this way
        this.description = description;//right way - you are setting the instance variable to the parameter value
    }
    This is called shadowing btw.

    // Json

  16. The Following User Says Thank You to Json For This Useful Post:

    kyuss (December 6th, 2009)

Similar Threads

  1. Importing excel data
    By supriya ramjee in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 20th, 2012, 10:39 AM
  2. Searching Data
    By kalees in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: October 2nd, 2009, 04:23 AM
  3. problem in retrieving data via RMS
    By solaleh in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: August 30th, 2009, 06:46 AM
  4. .xls data structure
    By helloworld922 in forum JDBC & Databases
    Replies: 3
    Last Post: August 20th, 2009, 08:12 PM
  5. I can't key in the data
    By muffin in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 11th, 2009, 12:03 AM