RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

loops

Review: In lecture, we introduced See while loops. We ended with a (something like):

    static String padWithDots( String msg, int minLength ) {
        String paddedSoFar = msg;
        while (length(paddedSoFar) < minLength) {
            paddedSoFar = paddedSoFar + ".";
            System.out.println( "Debug: paddedSoFar is " + len(paddedSoFar) + " chars long: " + paddedSoFar;
        }
        return paddedSoFar;
    }
      

We can also use a while-loop to make a copy of a string, where every 'e' is replaced by '3':

    static String leet( String txt ) {
        String leetTxtSoFar = "";
        int nextIndexToCheck = 0;
        
        while (nextIndexToCheck < length(txt)) {
            char nextChar =charAt(txt,nextIndexToCheck); 
            if (nextChar == 'e') {
                leetTxtSoFar = leetTxtSoFar + '3';
            }
            else { 
                leetTxtSoFar = leetTxtSoFar + nextChar;
            }
            nextIndexToCheck = nextIndexToCheck + 1;
            System.out.println( "at loop-bottom: nextIndexToCheck is " + nextIndexToCheck + " and leetTxtSoFar is " + leetTxtSoFar );
        }

        return leetTxtSoFar;
    }
    


Your task

  1. Write a method which surrounds a string with < and > on each side, until it meets-or-exceeds a given length:
    class Lab04cWhile extends Object120 {
    
        static void testAngleize() {
            assertEquals( "", angleize("",0) );
            assertEquals( "<>", angleize("",2) );
            assertEquals( "<<<hi>>>", angleize("hi",8) );
            assertEquals( "<<<hi>>>", angleize("hi",7) );
            assertEquals( "Mississippi", angleize("Mississippi",3) );
            assertEquals( "bye", angleize("bye",3) );
            assertEquals( "<bye>", angleize("bye",4) );
        }
    
        static void testAll() {
            testAngleize();
        }
    
    }
          
  2. Write a function which returns a string like the input, except that each 's' is replaced with '$', and each 'A' with '4'.
        static void testLeetMore() {
            assertEquals( "", "" );
    
            assertEquals( "z", leetMore("z") );
            assertEquals( "4", leetMore("A") );
            assertEquals( "$", leetMore("s") );
    
            assertEquals( "hi", leetMore("hi") );
            assertEquals( "z$", leetMore("zs") );
            assertEquals( "$z", leetMore("sz") );
            assertEquals( "4a", leetMore("Aa") );
    
            assertEquals( "Hiy4", leetMore("HiyA" );
            assertEquals( "p$$$t -- 4bdulla got all 4's.", leetMore("pssst -- Abdulla got all A's.") );
        }
          

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.