Java Reference
In-Depth Information
1. As an example of array list usefulness, create a JavaQuiz application
thatpresentsamultiple-choice-basedquizonJavafeatures.The JavaQuiz
class's main() method first populates the array list with the entries in
a QuizEntry array (e.g., new QuizEntry("what was java's
original name?", new String[] { "Oak", "Duke", "J",
"None of the above" },'a') ). Each entry consists of a ques-
tion,fourpossibleanswers,andtheletter(A,B,C,orD)ofthecorrectan-
swer. main() thenusesthearraylist's iterator() methodtoreturnan
Iterator instance, and this instance's hasNext() and next() meth-
ods to iterate over the list. Each of the iterations outputs the question and
fourpossibleanswers,andthenpromptstheusertoenterthecorrectchoice.
AftertheuserentersA,B,C,orD(via System.in.read() ), main()
outputs a message stating whether or not the user made the correct choice.
2. Createaword-countingapplication( WC )thatreadswordsfromthestandard
input (via System.in.read() ) and stores them in a map along with
theirfrequencycounts.Forthisexercise,awordconsistsoflettersonly;use
the java.lang.Character class's isLetter() methodtomakethis
determination. Also, use Map 's get() and put() methods and take ad-
vantage of autoboxing to record a new entry or update an existing entry's
count—the first time a word is seen, its count is set to 1. Use Map 's
entrySet() method to return a Set of entries, and iterate over these
entries, outputting each entry to the standard output.
3. Collections provides the static int fre-
quency(Collection<?> c, Object o) methodtoreturnthenum-
berofcollection c elementsthatareequalto o .Createa FrequencyDemo
applicationthatreadsitscommand-lineargumentsandstoresallarguments
except for the last argument in a list, and then calls frequency() with
thelistandlastcommand-lineargumentasthismethod'sarguments.Itthen
outputs this method's return value (the number of occurrences of the last
command-lineargumentinthepreviouscommand-linearguments).Forex-
ample, java FrequencyDemo should output Number of occur-
rences of null = 0 , and java FrequencyDemo how much
wood could a woodchuck chuck if a woodchuck could
chuck wood wood should output Number of occurrences of
wood = 2 .
Search WWH ::




Custom Search