my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Getter Setter Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getter Setter Question" Watch "Getter Setter Question" New topic
Author

Getter Setter Question

Charles Angemeyer
Greenhorn

Joined: Dec 18, 2012
Posts: 22
I'm working through one of those "CodingBat" exercises and I got it working.
(It is supposed to print every nth character).
My question is how do I set it up using Getters and Setters?
Did I do too much in my Setter?
Thanks.




Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

That's a matter of style really. By convention, setters take one parameter and assign that parameter to a member variable. They may also do validation ... range checking and so forth ... but they don't usually do anything else. You could follow that convention by calling the setter something else, like outputEditedString(String, int). In that case, you wouldn't need the member variables. There's no sense setting them unless you're going to use them later. On the other hand, you could have member variables for the original string and the n, and then have getters and setters for those, or just set them via a constructor. Your outputEditedString() in that case wouldn't need parameters, since it would use the member variables instead. In neither case do you need the String result in the member data. You aren't using it, and you use result locally in your method to mean something else, so that's confusing.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getter Setter Question
 
Similar Threads
Question from Jargon Mock Exam
Problem in iterating HashMap
cannot resolve symbol: Linux
how comparator works
LinkedList Problem