Java Homework: Exercise 5.14: Write a private class method in the Vic class that has a String parameter and an int parameter and returns the one-character substring at the given position. Then replace more complex coding by a call of that method in Listings 5.3, 5.4, and 5.5. I need help on how to do this. Book exerpt in comments
okay i see so u gotta make a indexing program
@Kainui
@ganeshie8
What you have to do is write this method: ``` private String getChar(String source, int pos) ``` You can use the `String.substring()` method, as it's already used in the `Vic` class. Now you have this new method, looking at the `Vic` class you have two properties: - `itsSequence` which is a String - `itsPos`which is an int (I don't know who named the variables this way, but if he was in my team he would have serious problems...) Whatever, you can use your new method to rewrite methods like `seesCD()`, `putCD()` etc... to make them simpler, or more readable. For example: ``` public boolean seesCD() { if ( ! seesSlot()) System.exit (0); return ! getChar(itsSequence, itsPos).equals (NONE); } ```
@maitre_kaio The author of the book named them. It is a bit of an odd book. You don't even deal with integers and basic types for the first 3 chapters...
I understand, but when I see students forced to use such books because teachers don't want to update their course, I'm feeling like making a rant: Besides the variable's naming, the whole question that @ElleCullen submitted seems rotted to me. The book asks to build a new method to make the code simpler, and this is a good thing ! But instead of using the instance's field, it asks to pass parameters to a method, thus getting against readability. To summarize my point, it seems that a lot a books used to teach programming were not written by skilled programmers. End of the rant.
Join our real-time social learning platform and learn together with your friends!