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

List of all available Websheets


Viewing java/03-arrays/NSwap 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
 
This is an extension of the <a href='javascript:websheets.load("java/00-intro/SquareSwap")'>SquareSwap</a> websheet.
Given an array <code>colors</code> of <code>n</code> strings,
move each string one position later in the array; and, you
must move the one at the end to the start.
Public permissions remove
Engine
Template / Reference solution
 
public static void main(String[] args) {
   // the grader will pre-define an array of color names
   String[] colors = \fake[...; // e.g. new String[] {"red", "blue", "green"};]\ \hide[args.clone();]\ 
   int n = colors.length; // for convenience
   System.out.println("Before n-swapping:");
   for (int i=0; i<n; i++)
      System.out.println("color " + i + " is " + colors[i]);
 
   // your swapping code here:
\[
   String tmp = colors[n-1];
   for (int i=n-1; i>=1; i--)
      colors[i] = colors[i-1];
   colors[0] = tmp;
]\
   System.out.println("After n-swapping:");
   for (int i=0; i<n; i++)
      System.out.println("color " + i + " is " + colors[i]);
}
Java test suite
See manual
 
title = "Running with <tt>colors[] = {\"red\", \"green\", \"blue\"}</tt>";
testMain("red", "green", "blue");
title = "Running with <tt>colors[] = {\"fuschia\", \"turquoise\", \"sienna\", \"vermilion\"}</tt>";
testMain("fuschia", "turquoise", "sienna", "vermilion");
title = "Running with <tt>colors[] = {\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"}</tt>"; 
testMain("a", "b", "c", "d", "e", "f", "g", "h");
title = "Running with <tt>colors[] = {\"monocolor\"}</tt>";
testMain("monocolor");
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'.