String Class Objects Methods ICS 111 Introduction to

String Class, Objects, Methods • ICS 111: Introduction to Computer Science I – William Albritton • Information and Computer Sciences Department at the University of Hawai‘i at Mānoa • “Tell me and I forget. Show me and I remember. Involve me and I understand. ” – Chinese proverb 2/11/2022 © 2007 William Albritton 1

Name Problem • Output your name in all lowercase letters and all uppercase letters 1. Understand the problem – 2. jeanny sue JEANNY SUE Write an algorithm a. b. 3. Print name in all lowercase Print name in all uppercase Write the computer program – 2/11/2022 For example: See Jeanny. Sue. java in “links” column from “Example Code” link © 2007 William Albritton 2

Next Problem • All of your friends are really impressed at you new program, so they all want you to write a program for each one of them 1. 2. Understand the problem Write an algorithm a. b. 3. Write the computer program – 2/11/2022 Print name in all lowercase Print name in all uppercase Retype the program for each friend, but takes a long time!!! © 2007 William Albritton 3

Next Problem Improved • Improve the algorithm, so we don’t have to type so much – Write an algorithm 1. Store the name in lowercase letters 2. Print the name in lowercase 3. Convert name to uppercase & print – Write the computer program – Only have to retype each name ONCE! – See Billy. Mac. java in “links” column from “Example Code” link 2/11/2022 © 2007 William Albritton 4

Terminology & Syntax • Variable initialization for an object – Has 3 steps 1. Declare a variable 2. Instantiate an object 3. Stores object’s address in the variable – Syntax • Class. Name variable. Name = new Class. Name(); – Example • String name = new String("Bubba"); 2/11/2022 © 2007 William Albritton 5

Terminology • Class – In simplest terms, a class is a blueprint for an object – It is a description of the data & methods for a particular set of objects • • 2/11/2022 String class describes the data & methods for Strings See the Java API (Application Programming Interface) for String class details (see “links” column on web page) © 2007 William Albritton 6

Terminology • Object – An instance (specific example) of a class – Stores data & allows methods to be called on that data – A variable contains the address (reference) that is used to access an object • String name = new String("Bubba"); //variable name has object's address • System. out. println(name. to. Upper. Case()); //method call to the object referred to //by variable name 2/11/2022 © 2007 William Albritton 7

Class vs. Object Examples • A class is like a blueprint for a house – The houses build from the blueprint are objects • Think of a class as a rubber stamp – Objects are created by using the rubber stamp to create different prints • Think of Hokusai’s woodblock carving as the class – Think of Hokusai’s “Great Wave off Kanagawa” woodblock print as the object 2/11/2022 © 2007 William Albritton 8

Class Exercise 1 • Think of your own Class vs. Object Examples – Class 1. 2. 3. 4. 5. 6. 2/11/2022 object Blueprint house 1, house 2, house 3 Rubber stamp red print, blue print, green print Woodblock “Great Wave off Kanagawa” Your example. . . © 2007 William Albritton 9

Terminology • Variable – A name for a place in memory where a value is stored – Example • String name = new String("Bubba"); //name is a variable – Variables can be used to store an object’s address • 2/11/2022 An object’s address (reference) is the place in memory where an object is stored © 2007 William Albritton 10

Terminology & Syntax • Step 1: declare a variable (variable declaration) – Creates a place in memory to store a value – Syntax • Class. Name variable. Name = new Class. Name(); – The value stored in the variable is an object’s address (reference) 2/11/2022 © 2007 William Albritton 11

Terminology & Syntax • Step 2: instantiate an object (object instantiation) – Creates space for an object in memory & initializes the values of that object – Syntax • Class. Name variable. Name = new Class. Name(); – You might want to visualize a variable as being a mailbox & the house as the object • • • 2/11/2022 The variable (mailbox) contains the address to the object (house) Variables (mailboxes) are all the same size Objects (houses) are of different sizes, colors, etc. © 2007 William Albritton 12

Terminology & Syntax • Step 3: store object’s address in the variable by using the assignment operator (=) – Variable on left gets the value of the expression on the right • Class. Name variable. Name = new Class. Name(); – You might want to visualize an variable as being a mailbox & the house as the object • 2/11/2022 The assignment statement gives the object’s (house’s) address to the variable (mailbox), so that the computer (mailperson) knows where the object (house) is located via the variable (mailbox) © 2007 William Albritton 13

Boxes & Balloons • Perhaps a simpler visualization is to think of a variable & object pair as a box & balloon pair – Variable • The box – Address of the object • The string to the balloon – Object • 2/11/2022 The balloon © 2007 William Albritton 14

Terminology & Syntax • Methods – Defines the behavior of a class – Used to get the object to do something • • • See the Java API for methods of the String class Note the for the String class, the String methods DO NOT change the original String Call (invoke) a method – Send a message (command) to an object – Tell the object to do something • Syntax: variable. Name. method. Name(); • System. out. println(name. to. Upper. Case()); 2/11/2022 © 2007 William Albritton 15

Terminology & Syntax • Constructor – A special method that initializes the values of an object – The name of a constructor is its class name – Constructor must be preceded by keyword “new” • “new” creates space in memory for the object – Syntax • Class. Name variable. Name = new Class. Name(); – Example • String name = new String("Jeanny"); 2/11/2022 © 2007 William Albritton 16

Terminology • Keywords – Special words with predefined meanings that cannot be used as the name of your program, variable, etc. – Example of bad code (does not compile!) • String class = new String("Jeanny"); • Since “class” is a keyword, it cannot be used as a variable name – Some keywords that we already have seen • public, class, static, void, new 2/11/2022 © 2007 William Albritton 17

Class Exercise 2 • What’s the output of the Java program? – See Exercise 2. java • Methods to. Upper. Case() & to. Lower. Case() return a string with all upper case letters, or all lower case letters • Note the for the String class, the value of a String DOES NOT change • String methods DO NOT change the original String, but instead return a new String object 2/11/2022 © 2007 William Albritton 18

Initials Problem • Your friends were so impressed with your program, that they are now asking you to output their initials as well – Algorithm 1. Store the first & last name separately 2. Put the following together & store it: the first letter of first name, a period, a space, the first letter of last name, and a period 3. Output the initials – Computer program – See Initials. java 2/11/2022 © 2007 William Albritton 19

Terminology & Syntax • Parameters (also called “arguments”) – The values between the method’s parentheses • Note that in public static void main(String[] args) “args” is short for “arguments” – Syntax • variable. Name. method. Name(parameter 1, parameter 2, parameter 3, . . . ); – Example • first. Name. substring(0, 1) 2/11/2022 © 2007 William Albritton 20

String Methods • The characters in a string are numbered, starting at zero (0) – "abcd" has corresponding positions 0123 • substring(start, end) – Returns a substring made up of the characters from start to end-1 in the original string, where start & end are whole numbers (integers) • String string = new String("abcd"); • System. out. println(string. substring (1, 3)); //output is "bc" 2/11/2022 © 2007 William Albritton 21

String Operators • Add (concatenate) two strings together – Can use the + operator rather than call a method • String first = new String("Hayao"); String last = new String("Miyazaki"); System. out. println(first + " " + last); //output is "Hayao Miyazaki" 2/11/2022 © 2007 William Albritton 22

Assignment Operator • The assignment operator (=) is used to assign the value on the right hand side to the object on the left hand side – For example • String first = new String("Hayao"); String last = new String("Miyazaki"); String full. Name = first + " " + last; System. out. println(full. Name); //output is "Hayao Miyazaki" 2/11/2022 © 2007 William Albritton 23

Class Exercise 3 • What’s the output of the Java program? – See Exercise 3. java 2/11/2022 © 2007 William Albritton 24
- Slides: 24