![]() |
![]() |
|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
Sometimes we want an object to change over time.
For example,
if a
Song s; s = new Song( "Happy Birthday", "Patty and Mildred Hill", 31, true ); // Returns true: Object120.equals(s, new Song( "Happy Birthday", "Patty and Mildred Hill", 31, true ) ) s.isCopyrighted = false; // Returns false: Object120.equals(s, new Song( "Happy Birthday", "Patty and Mildred Hill", 31, true ) ) |
We often use “setter functions” to change a field:
/** Set the isCopyright status of a Song.
* @param copyable The new copyright status for the Song to have.
*/
static void setCopyright( Song s, boolean copyable ) {
s.isCopyrighted = copyable;
}
// Test that it works (from the Code Pad):
setCopyright(s, true); // A *void* function -- must include semicolon in code pad
System.out.println( "Actual: " + s );
System.out.println1( "Expect: " + new Song( "Happy Birthday", "Patty and Mildred Hill", 31, false ) );
|
1We've already seen that rather than print out the actual/expected and check the printed results by hand, it's more convenient to have the computer check if they're equal:
setCopyright(s, false);
if (Object120.equals(s, new Song( "Happy Birthday", "Patty and Mildred Hill", 31, false ) )) {
System.out.println( "(test passed)" );
}
else {
System.out.println( "Actual: " + s );
( "Expect: " + new Song( "Happy Birthday", "Patty and Mildred Hill", 31, false ) );
}
|
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.10 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |