Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

dogHears

Language/Type: Java input println Scanner Strings
Author: Marty Stepp (on 2010/12/28)

It is said that all dogs hear when people talk to them are their names (see cartoon by Gary Larson to the right). Write a static method named dogHears that prints lines of what a dog hears. The method accepts two parameters: a dog's name, as a string, and the number of lines, as an integer. Each line contains a randomly chosen number of words, between 2 - 10 words inclusive.

Each word on a given line is chosen randomly to be one of two choices: the dog's name, with 25% probability (1/4 likelihood); or the word "blah", with 75% probability (3/4 likelihood). This does not mean that the dogs name will print exactly every fourth time; just that a given word has a 1/4 random chance of being the dog's name. Multiple calls to dogHears with the same parameters will likely result in different output for each call. If 0 or less is passed for the number of lines, no output should be produced.

The following table lists some calls to dogHears and an example of their possible output:

Call
dogHears("Ginger", 7);
dogHears("Kona", 4);
Output
Ginger blah blah blah blah blah blah 
blah blah blah blah blah blah 
blah blah Ginger blah blah 
Ginger blah blah Ginger blah blah blah blah blah 
blah Ginger blah blah blah blah Ginger blah 
blah Ginger 
blah blah blah blah blah blah Ginger blah blah Ginger
blah blah blah 
Kona Kona blah blah blah Kona 
blah blah Kona blah 
blah Kona blah
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.