You’ve read about how Strings work in Java. Test your new skills with this challenge: solve this String-based puzzle to help you solidify your knowledge of the Java programming language and Android development. That's right, Android, too! You may need to refer to other Android tutorials here.
Setup
To prepare for this challenge, you’ll want to start with a basic Android application. Simply create an Android application within Eclipse and edit its default Activity, specifically the onCreate() method, to test the code you create for this challenge. Remember, you can use the Log.v() method to print output (including strings) to the debug log. This is a quick and convenient way to display testing output for exercises such as this one.
If what we've just asked of you is already too challenging, we would recommend taking a step back. Start with some of the Android tutorials, such as Introduction to Android Development or Beginning Android: Getting Started with Fortune Crunch. Once you’ve mastered setting up an Android project, return and try these progressive exercises.
A Progressive Challenge
This is a progressive challenge. If you follow each of the steps below correctly, you will solve a puzzle. Keep in mind, there are numerous ways to approach this problem and therefore numerous solutions.
You can print out your progress to the Android debug log called LogCat using the Log.v() method.
Bonus: Extra points (not that you’re being graded) for those who catch the reference to this puzzle, and where it comes from.
Step 1: Define Your String
For your first step, you’ll need to define a new text string for use in this challenge. You’ve got lots of options here. For the many ways in which you can define strings in Java, see our String tutorial. The simplest method, of course, would be to simply define a new variable of type String.
The contents of this string should be the following text:
Setec Astronomy
Step 2: Convert to Uppercase
For your next trick, you’ll want to convert the string to all uppercase letters.
Hint: If you’re stuck, check the methods available within the String class, as defined in the Android SDK documentation.
Sanity Check: The contents of your new string should be the following text: SETEC ASTRONOMY
Step 3: Rearrange The Letters
Now things will get a bit more challenging…
As you may recall, a string is simply a sequence of characters. In this step, you want to rearrange the characters in your uppercase string and add one extra character.
Think of your current string as a 0-based array of characters. Now rearrange the characters of your string into the following sequence, where each number represents the current character index, dashes are used to separate each index, and the asterisk (*) is used to represent a space character:
Hint: Remember that String variables cannot be changed. Seriously consider usingStringBuilder.
to build up your new string contents from the specific characters of your source string.
to build up your new string contents from the specific characters of your source string.
Sanity Check: If you print out the resulting string, it should be:
STERCES YNAM OOT.
STERCES YNAM OOT.
Step 4: Reverse Those Letters
You’re almost there! The only thing left to do is to reverse the characters in your string. Hopefully you used a StringBuilder to build up your string from individual characters; it’s got a helpful method that can this job for you.
Print the resulting string. It should be clear if you’ve solved the puzzle.
Step 5: Tweak Your Solution (Optional Challenge)
It’s always a good idea to review your solution and improve it, perhaps making it slightly more elegant or flexible. While this step is certainly not essential to solving the puzzle (especially as you’ve already done so), we suggest you continue if you’ve found the previous steps reasonably straightforward.
Assuming you haven’t already, consider how you might define a String variable to represent the index sequence discussed in Step 3.
Now, take the following steps:
- Parse the string above into an array of Strings, one for each number or asterisk.Hint: Check the String class method called split().
- Create a loop and iterate through this array of strings and build the resulting string using a StringBuilder. As part of this process, you will need to convert each String to the appropriate integer, or to a space character.
Conclusion
Android developers use Strings all the time to store textual data for various purposes. Strings often need to be manipulated in order to be used within your code. As always, there are many ways to solve these problems, so feel free to post your alternative answers (or any questions) in the comments section. We have supplied a couple of methods in the accompanying attachment.
Best of luck!
No comments:
Post a Comment