![]() |
![]() |
|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
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 ) );
}
|
Btw,
why did I test setting for both true and false?
(If you only tested
Btw, just for reference, here's
/** 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.
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 “
static void setIsCopyrighted(>Song s, boolean _isCopyrighted ) {
s.iscopyRighted = _isCopyrighted;
}
static double diskSpaceRequired(>Song s) {
return s.length * 0.222;
}
|
void setIsCopyrighted( /* Song this, */ boolean _isCopyrighted ) {
this.iscopyRighted = _isCopyrighted;
}
double diskSpaceRequired( /* Song this */ ) {
return this.length * 0.222;
}
|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
| ©2012, Ian Barland, Radford University Last modified 2012.Oct.14 (Sun) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |