120 likes | 202 Views
getFamilyName( ) with one "name" field. We will assume these specifications for the "name" field: The name field consists of these parts, in this order: 1) the family name, possibly including some (single) blank characters
E N D
getFamilyName( ) with one "name" field We will assume these specifications for the "name" field: The name field consists of these parts, in this order: 1) the family name, possibly including some (single) blank characters 2) a double blank " " if necessary to separate the family name from the given names. 3) zero or more given names 4) maybe some trailing blanks At least part (1) is required, but the field may omit later parts. (Or: "Parts 1 to N are required, where N >= 1. If part k is omitted, all parts j must be omitted, with j >= k. Or …?) Examples: name: CLARKE JIM family name: CLARKE CLA R JIMCLA R CLA CLA CLAbCLA bb"" (empty string)
Keep it simple on the first try int pos = 0; // the current character position String result = ""; // the family name // that we're building up 1. while (pos < name.length()) { 2. if (name.charAt(pos) == ' ') { 3. if (name.charAt(pos+1) == ' ') // not "=" 4. return result; 5. } 6. else 7. result += name.charAt(pos); 8. pos++; // pos = pos + 1; 9. } • What about "CLAbR"? • What about "CLA"? • What about "CLAb"?
Fixed, we hope public String getFamilyName () { int pos = 0; // the current character position String result = ""; // the family name // that we're building up while (pos < name.length()) { if (name.charAt(pos) == ' ') { if (pos + 1 == name.length() || name.charAt(pos+1) == ' ') return result; } result += name.charAt(pos); pos++; } return result; }
A common loop pattern int pos = 0; ... while (pos < name.length()) { ... pos++; } This is an example of a very common pattern: INITIALIZE; while (TEST WHETHER TO DO ANOTHER CYCLE) { body; STEP TO PREPARE FOR ANOTHER CYCLE; } So we have a special statement, a "for" loop: for (INITIALIZE; TEST; STEP) { body; }
Our loop, rewritten public String getFamilyName () { String result = ""; // the family name // that we're building up for (int pos = 0; pos < name.length(); pos++) { if (name.charAt(pos) == ' ') && ((pos + 1 == name.length() || name.charAt(pos+1) == ' ')) return result; result += name.charAt(pos); } return result; }
Using String methods 1) indexOf: To find the first double blank, we can just say int where = name.indexOf(" "); Then, if there was no double blank, we can make the result be almost the same as name, and otherwise we just use the first part: if (where == -1) // return the same as name, except for a // possible final blank else // return the part of name from position // 0 to position where-1 2) substring: To find the part of the name from 0 to where–1, we can say: name.substring(0, where)
Rewritten again public String getFamilyName () { int where = name.indexOf(" "); if (where == -1) //return name; --what about final blank? return name.trim(); else return name.substring(0, where); }
How the Student class handles names class Student { private String name; public Student (String theName, ...) {...} public void setName (String newName) {...} public String getName () {return name;} public String getFamilyName () { ... // a LOT of stuff, and hard work too } } class TestStu { public static void main (...) { // Here we create many Students and carry out many operations on // names. How often do we separate a name into its parts? } }
Redesigning the Student class class Student { private String familyName; private String givenNames; public Student (String theName, ...) {...} public void setName (String newName) {...} public String getName () { return familyName + " " + givenNames; } public String getFamilyName () { return familyName; } private void breakUpName (String name) {...} }
A private method class Student { private String familyName; private String givenNames; private void breakUpName (String name) { int where = name.indexOf(" "); if (where == -1) { familyName = name.trim(); givenNames = ""; } else { familyName = name.substring(0, where); givenNames = name.substring(where + 2); } } ...
Using the new method class Student { private String familyName; private String givenNames; private void breakUpName (String name) {...} public Student (String theName, ...) { breakUpName(theName); ... } public void setName (String newName) { breakUpName(newName); } }
An exercise class Student { private int numCourses; // including failures private int numPassed; private double avgMark; // including failures public static final int PASS_MARK = 50; // includeNewMarkInAvg: update the records to // include a new course mark. public ??? includeNewMarkInAvg (??? mark) { ??? } } Exercise: replace the ???s with Java code. Write part of a main( ) method to use the new Student class.