RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lect07c
non-static methods
and, testing void methods

How to test void methods?

  void testSong() {
        // ...
        
        // We can't write
        //    assertEquals( setIsCopyrighted(s1, false ), ??? )
        // because setIsCopyrighted doesn't return a value.
        // Instead:
        setIsCopyrighted(s1, false );
        assertEquals( s1.isCopyrighted, false );

        setIsCopyrighted(s1, true);
        assertEquals( s1.isCopyrighted, true );

        // OR: instead of just asserting the contents of one field:
        assertEquals( s1, new Song("Hit the floor", "Nickelback", 4*60+2, false ) );
    }
Since setter methods don't return a value, we can't what they (don't) return. Instead, we check afterwards that the affected fields now really do look like we want them to: Check that s1.isCopyrighted has the exact value of whatever we just tried setting it to.

Btw, why did I test setting for both true and false?
(If you only tested setIsCopyrighted with true, can you imagine a totally lame, wrong implementation that still happens to pass your test?)

Btw, just for reference, here's assertEquals again. It's interesting because it can take in any Objects — Strings or Songs or Integers … we won't talk more about what's happening until the last week of the semester, “inheritance”.

    /** Test whether the result from an actual function-call
     *  is what we expected/desired.
     *  Print a loud error message to stderr if not (and otherwise, 
     *  print some small quiet indication to stdout that the test case passed,
     *  so that we have some confidence that the test actually ran).
     * @param actual The actual result, from calling some function.
     * @param desired The result we desire, if the function is working correctly.
     */
    static void assertEquals( Object actual, Object desired ) {
        if (equals(actual,desired)) {  // Uses Object120.equals, for smart checking.
            System.out.print(".");
        }
        else {
            System.err.println( "***Test failed" );
            System.err.println( "Actual  result: " + toString(actual) );
            System.err.println( "Desired result: " + toString(desired) );
        }
    }


As of today, I'll start saying “method” instead of “function” — now that we're starting object-oriented (non-static) concepts.

Non-static methods

A mechanical translation: Instead of having the first argument (the object) be listed after the parentheses, pick it up and move it in front of the method-name (!):

          String msg = "let's call non-static methods, eh?";
          substring(msg,3,7)      ⇆   msg.substring(3,7)
          length(msg)              ⇆   msg.length()
          substring("radford",3,7)  ⇆ "radford".substring(3,7)
          substring(s,3,length(s)-1)    ⇆   s.substring(3,s.length()-1)
          
          Song lala = ...
          lala.fitsOnDisk()
          lala.setCopyright(true)

Correspondingly, in the signature of the function, you just don't include that first argument; Java includes it for you with the name “this”: Instead of

  static void setIsCopyrighted(>Song s, boolean _isCopyrighted ) {
     s.iscopyRighted = _isCopyrighted;
     }

  static double diskSpaceRequired(>Song s) {
    return s.length * 0.222;
    }
  
we'll write
  void setIsCopyrighted( /* Song this, */ boolean _isCopyrighted ) {
     this.iscopyRighted = _isCopyrighted;
     }

  double diskSpaceRequired( /* Song this */ ) {
    return this.length * 0.222;
    }
  

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2012, Ian Barland, Radford University
Last modified 2012.Oct.14 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme