Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mayur Pande
Courses Plus Student 11,711 PointsDo I need to create a new class for extra credit Random Toasts on the Testing and Debugging section?
I am trying to attempt the extra credit section of the Testing & Debugging section. The problem is Random Toast and says;
With your newfound knowledge of random numbers and Toast messages, add another random number generator that will occasionally show a bonus Toast fact about yourself. For example, generate a random number from 0-19 and only show the Toast for one of the numbers.
I am getting confused as to whether or not I should add a new class and create the new methods in their and then call it from my main activity and then assign a toast to it. Or should I just add it to existing code. If so, where would I add it to?
Thanks
1 Answer
Harry James
14,780 PointsIt is completely up to you.
You can either create a new class with just that method in (Like RandomFact.java) or, you can create the method inside of the MainActivity.java file itself.
Either way, it won't affect your code, they are just two different ways of going about it, it's personal preference!
Spoilers on how you can go about it past this point! Look if you dare!
You're going to want to create a method though that will generate the random number and therefore a random fact (Perhaps an array of different strings? Hint hint?) You can then return the fact as a string from that method and just plug it in to your Toast! Pretty simple, eh?
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsThanks for the help. I have a better understanding now.
What I trying to comprehend now is, do I need to add a new button in order to display these new facts?
I have done what you have said and made a new class and have created a new random generator with an array that returns a string. But I am getting errors when I plug it into my Toast.
Harry James
14,780 PointsHarry James
14,780 PointsYou don't need to add a button, you can choose how you want to do it.
For example, the Bonus Credit says "occasionally show a bonus Toast fact about yourself". Now, as it is bonus credit, you don't need to follow this and can instead do something else, it won't break your code (Unless it errors of course)!
I wrote what the code could look like if you want to take a peek :) http://pastebin.com/MGW9vvh1
Hope it helps and, if you have any questions, give me a shout!
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsThank you so much this has been very helpful, I was getting the syntax wrong for the Toast. When I created the Toast in my main activity, I was not using the object created for the RandomFact class to call my method from the RandomFact class. After I correctly called it Android Studio cast it for me, and voila it works : ).
Now onto another challenge, as it only displays this at the beginning I will try to get it to display at different intervals.
Thank you again.
Harry James
14,780 PointsHarry James
14,780 PointsGlad to hear you got it working :)
As for the occasionally showing bit, this link has a few handy ways of going about this (Different options are available depending on how you're going to go about doing this (How long the delay is between each message)).
Hope it helps and good luck! :)