|
home—info—lects—labs—exams—hws
tutor/PIs—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 ) ); |
You can use
your own
We want to be able to update a
It will take several steps in order to write
Hint: To convert from a letter-grade to a gradepoint: I'll leave the full details to you, but note that Java does let you convert between a character and its ascii code.In Code Pad, type
(int)'A' or(char)((int)'A'+25) .(Recall that “
(int) ” means "casting", which is a very idiosyncratic way to call a conversion function. You are free to write your own (ordinary) functioncharToInt which hides the cast.)
Hint: How do you compute a grade point average?First you compute the total grade points weighted by the number of hours, and then divide by the total number of credit hours (the total weight).
For example, if you get a C in a one-hour class (like Univ100), you get 2*1 grade points. If you get a B in a four-hour class (like ITEC120), you get another 3*4 grade points. If those were your only two grades, your gpa would be (2*1+3*4)/(1+4) = 2.8.
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—Object120 + its docs—java.lang docs—java.util docs
©2010, Ian Barland, Radford University Last modified 2010.Oct.26 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |