Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing java/05-staticmethods/Boxed by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Write a class <code>Boxed</code> whose API has two public static methods:
<ul>
<li>Write a method <code>repeat()</code> that takes a string argument and an integer argument,
and returns the <code>String</code> obtained by repeating the string that many times.
For example <code>repeat("hots", 2)</code> should return <code>"hotshots"</code>
</li>
<li>Using <code>repeat()</code>, write a method <code>boxedPrint()</code> that takes a string argument, and prints
it out surrounded by a box of asterisks. 
For example, <code>boxedPrint("Hello, World!")</code> should cause the following
text to be sent to standard output:
<pre>
***************
*Hello, World!*
***************
</pre>
To get
the <b>length</b> of a String <code>stringVar</code>, use <code>stringVar.length()</code>.
</li>
</ul>
Public permissions remove
Engine
Template / Reference solution
 
public static\[String]\repeat(\[String S, int times]\) {
\[
   String result = "";
   for (int i=0; i<times; i++)
      result += S;
   return result;
]\
}
public static\[void]\boxedPrint(\[String S]\) {
\[
   StdOut.println(repeat("*", S.length()+2));
   StdOut.println("*"+S+"*");
   StdOut.println(repeat("*", S.length()+2));
]\
}
//basic tests
public static void main(String[] args) {
   StdOut.println(repeat("jar", 2));
   StdOut.println(repeat("he", 4));
   boxedPrint("Hello, World!");
}
Java test suite
See manual
 
testMain();
test("repeat", "meow", 4);
test("repeat", "nothing", 0);
test("boxedPrint", "|||stars and stripes|||");
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.