110 likes | 254 Views
Text. Pages 102-104. Syntax Introduced. c har, String Char is single character String is a string of characters How to define them? How to assign them? How to use them?. Character data type. c har letterGrade = ‘A’; c har poorGrade = ‘F’; Usage: if (answer == ‘Y’)
E N D
Text Pages 102-104
Syntax Introduced char, String Char is single character String is a string of characters How to define them? How to assign them? How to use them?
Character data type char letterGrade = ‘A’; char poorGrade = ‘F’; Usage: if (answer == ‘Y’) { println(“You answered Yes”); } char letter = ‘a’;// note single quote for characters for (inti = 0; i< 26; i++){ println(letter); letter = letter + 1; }
Words and sentences String myName; myName = “Bina”; String firstName = “Bagger”; String lastName = “Vance”; String fullName = firstName + lastName; // here + operator means concatenation
Type conversion Pages 105-106
Syntax Introduced Function for conversion: char(), int(), float(), str() Usage: inti = 65 ; // value of character A char ch1; ch1 = char(i); assigns ch1 = ‘A’ float fNum = 65.3; int j = (int)fNum; // convert floating point number to integer
Objects: this is a big deal Pages 107-109
What is an object? • Unlike simple/basic data types that have only name and value, objects have name, characteristics, and operations. • You define an object by • Its name or id or reference • Its characteristics /properties: it has ____, ____ • Its operations: It can do _____, _____ • Dot notation lets you access the operations of an object as well any publicly accessible characteristics/properties
Object String String has a number of chars assigned to it. What are the operations? length() startsWith(char) charAt(int) subString(int) subString(int, int) equals()
Examples for String Object operations String s1 = “Player Piano”; intlen = s1.length(); println(len); println(s1.startsWith(‘P’)); println(s1.startsWith(‘Q’)); char fifth; fifth = s1.charAt(5);
Examples (contd.) String s1 = “Mount Vernon”; String s2, s3; s2 = s1.substring(6); s3 = s1.substring(0,5); println(s1.equals(s1));