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:
Code :
{ /* 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!
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
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.
Re: Data hiding and maintainability
Quote:
Originally Posted by
humdinger
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
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.
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.
Code :
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
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:
Code :
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:
Code :
/**
* 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.
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
Code :
/**
* 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
Code :
/**
* 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.
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:
Code :
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
}
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!
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
Re: Data hiding and maintainability
Quote:
Originally Posted by
copeg
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:
Code :
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