Given the class below, design a method in the \"Strings\" class which generates
ID: 3656812 • Letter: G
Question
Given the class below, design a method in the "Strings" class which generates an array of random strings using digits and alphabets. public class Strings { private String [ ] x; private int capacity; private int size; : : }Explanation / Answer
using dollar: Iterable chars = $('a', 'z'); // 'a', 'b', c, d .. z given chars you can build a "shuffled" range of characters: Iterable shuffledChars = $('a', 'z').shuffle(); then taking the first n chars, you get a random string of length n. The final code is simply: public String randomString(int n) { return $('a', 'z').shuffle().slice(n).toString(); } NB: the condition n > 0 is cheched by slice EDIT as Steve correctly pointed out, randomString uses at most once each letter. As workaround you can repeat the alphabet m times before call shuffle: public String randomStringWithRepetitions(int n) { return $('a', 'z').repeat(10).shuffle().slice(n).toString(); } or just provide your alphabet as String: public String randomStringFromAlphabet(String alphabet, int n) { return $(alphabet).shuffle().slice(n).toString(); } String s = randomStringFromAlphabet("00001111", 4);Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.