getFamilyName(%20)%20with%20one%20"name"%20field - PowerPoint PPT Presentation

About This Presentation
Title:

getFamilyName(%20)%20with%20one%20"name"%20field

Description:

getFamilyName( ) with one 'name' field. We will assume these specifications for the 'name' field: ... 3) zero or more given names. 4) maybe some trailing blanks ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 13
Provided by: depts175
Category:

less

Transcript and Presenter's Notes

Title: getFamilyName(%20)%20with%20one%20"name"%20field


1
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 gt 1. If
    part k is omitted, all
  • parts j must be omitted, with j gt k. Or ?)
  • Examples
  • name CLARKE JIM family name CLARKE
  • CLA R JIM CLA R
  • CLA CLA
  • CLAb CLA
  • bb "" (empty string)

2
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 lt name.length())
  • 2. if (name.charAt(pos) ' ')
  • 3. if (name.charAt(pos1) ' ') // 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"?

3
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 lt name.length())
  • if (name.charAt(pos) ' ')
  • if (pos 1 name.length()
  • name.charAt(pos1) ' ')
  • return result
  • result name.charAt(pos)
  • pos
  • return result

4
A common loop pattern
  • int pos 0
  • ...
  • while (pos lt 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

5
Our loop, rewritten
  • public String getFamilyName ()
  • String result "" // the family name
  • // that we're building up
  • for (int pos 0 pos lt name.length() pos)
  • if (name.charAt(pos) ' ')
  • ((pos 1 name.length()
  • name.charAt(pos1) ' '))
  • return result
  • result name.charAt(pos)
  • return result

6
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
    where1, we can say
  • name.substring(0, where)

7
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)

8
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?

9
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) ...

10
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)
  • ...

11
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)

12
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.
Write a Comment
User Comments (0)
About PowerShow.com