1 / 21

F21SF Software Engineering Foundations

5 More Strings and Selection. Dept of Computer Science. F21SF Software Engineering Foundations. Monica Farrow EM G30 email : M.Farrow@hw.ac.uk Material available on Vision. Topics. Testing for equality More than one constructor Selection using if/else Assumptions and exceptions.

Download Presentation

F21SF Software Engineering Foundations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SJF L3 5 More Strings and Selection Dept of Computer Science F21SFSoftware Engineering Foundations Monica Farrow EM G30 email : M.Farrow@hw.ac.uk Material available on Vision

  2. SJF L3 Topics • Testing for equality • More than one constructor • Selection using if/else • Assumptions and exceptions

  3. SJF L3 TESTING FOR EQUALITY

  4. SJF L3 Testing for Equality • To find out if 2 primitive types are equal, use the == operator, which results in a boolean (true/false) type • 5==5 (true) • 5==4 (false) • int n1 = 5; int n2 = 4; n1== n2 (false) • To find out if 2 primitive types are NOT equal, use !=

  5. SJF L3 Testing for object equality • We often want to test a string for equality to another string. • In this case, we ALWAYS use the String equals method, which compares the contents of strings • We NEVER use the double equals sign (which compares the memory locations where the String is stored) boolean same = entry.equals(password)) • To find out if 2 Strings are NOT equal, use ! boolean same = !entry.equals(password)) Why? 

  6. SJF L3 Testing for primitive type equality • With primitive types, a simple == comparison is all that is needed. • The variable name refers to a location in memory. • == compares the values at that location • Primitive types store their data in their location. • Objects don’t fit into a single memory location, so they just store a link to where the data is stored int num String firstName Barbara 42

  7. SJF L3 name2 b a r b a r a String equality Showing the difference between data values and locations String name1,name2,name3; name1=“barbara”; String bar = “bar”; name2 = bar + bar + “a”; name3=name1; name3 name1 name1.equals(name2) is true but name1==name2 is false name1.equals(name3) is true and in this particular case name1==name3 is true b a r b a r a

  8. SJF L3 String equality • The String equals method compares the 2 strings character by character. • in this way, upper case ‘A’ is different to lower case ‘a’ • There are other String methods which: • Compare ignoring case • Convert strings to upper or lower case

  9. SJF L3 QUIZ • Is name2==name3 true or false? • Is name.equals(“barbara”) true or false? For primitive types, you can use ==, >, <, != (not equal), >= (greater or equal to), <= Write expressions to test: • If the integer value in num1 is greater than 10 • If the String value in myString is equal to “5” • If the integer value in num1 is not equal to 4 • If a character value in ch is equal to ‘A’

  10. SJF L3 SELECTION and ALGORITHMS

  11. SJF L3 No middle name • Some people do not have a middle name • How should this be stored? • a null value • an empty String (“”) This is the approach taken in the example code

  12. SJF L3 Returning the full name • The name is stored as first name, middle name and last name • The middle one is the only one which we expect could be omitted • We’d like to return • If no middle name : first name then space then last name • If middle name exists : first name then space then middle name then space then last name • We need to • Test whether the middle name is there or not • Take different actions, depending on the result

  13. SJF L3 Algorithm to get the full name Start off with a String containing the first name and a space If there is a middle name (it’s not an empty String) Add middle name and a space Add last name [n== 0, n== 1] [middle name] Output middle name and space Output first name and space [no middle name] Output last name [counter <=n]

  14. SJF L3 If statement • The ‘if’ statement allows you to execute a block of code depending on a condition • If the condition is false, the block is not executed if (!middleName.equals("")) { result += middleName + " "; } ! Means NOT

  15. SJF L3 More about if • Always use curly brackets to enclose the blocks • You don’t NEED to use them if the block has only one statement • The chances are that you might change your code to add a second statement and forget to add the curly brackets – so start off with them! • The ‘if’ statement evaluates the condition (which is given within the rounded brackets) to a boolean value, True or False

  16. SJF L3 getFullName() public String getFullName() { String result = firstName + " "; if (!middleName.equals("")) { result += middleName + " "; //+= means ‘add RHS to LHS’ //same as result = result + middleName + " "; } result += lastName; return result; } OR you could use if (middleName.length() == 0)

  17. SJF L3 >1 Constructor • A class can have >1 constructor • If you don’t provide ANY constructors, java provides a default one which sets all the instance variables to suitable initial values, such as 0 for numbers and null for objects • We have a constructor to create a Name from 3 separate strings – first, middle and last names • We’d also like to create the Name from a single string • Then we have to split this into first, middle and last names. Use String methods to find substrings and index positions.

  18. SJF L3 String substring method • There is a String method with the signature String substring(int start,int next) • This one’s a bit confusing because it extracts a substring • Starting from the index given in the first parameter • And stopping before the index given in the second parameter • If you omit the second parameter, it goes up to the end • s = "Hello"; • s.substring(0,2) => "He" • s.substring(2) => "llo"

  19. SJF L3 Algorithm for fullname constructor Find position of first space First name is the substring of the fullname, from 0 to just before the first space Find position of last space If there is not a middle name (the position of the first space is the same as the position of the last space) middle name becomes an empty string Else middle name is substring from first space position + 1 up to just before last space Last name is substring from last space Pos + 1 to end Look at this code in your own time

  20. SJF L3 To Do Now • Read through lecture, do Quiz • Write and test a method in the Name class to return: • The initials. There may be 2 or 3, depending on whether there is a middle name or note.g. Mary Ann Smith -> MAS Tim Jones -> TJ You might need to refer to the slides in L3 Strings

  21. SJF L3 Followed by slides ERRORS AND EXCEPTIONS

More Related