1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.

Slides:



Advertisements
Similar presentations
Java Programming Strings Chapter 7.
Advertisements

Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?
Building Java Programs
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-3: Strings and objects; printf reading: 3.3, self-check: Ch.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Topic 13 procedural design and Strings Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
1 Sierpinski Valentine. CS 112 Introduction to Programming Switch; Text Processing Yang (Richard) Yang Computer Science Department.
Objects and Classes; Strings. 2 Classes and objects class: A program entity that represents either 1.A program / module, or 2.A type of objects* –A class.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
Unit 3.
CSC Programming I Lecture 9 September 11, 2002.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers; Type boolean reading: , 5.6.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
CHAPTER 6 GC Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
CS 112 Introduction to Programming Boolean Type, String Processing Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, 4.3.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8: The String class
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
Chapter 6 GC 101 Strings.
Strings string: An object storing a sequence of text characters.
String Handling in JAVA
Topic 9 Using Objects, Interactive Programs and Loop Techniques
Topic 13 procedural design and Strings
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
CS 106A, Lecture 9 Problem-Solving with Strings
Chapter 7: Strings and Characters
Building Java Programs
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8: The String Class and Boolean Zen
Building Java Programs
Lecture 14: Strings and Recursion AP Computer Science Principles
Building Java Programs
Using Objects (continued)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Strings string: An object storing a sequence of text characters.
Building Java Programs
Building Java Programs Chapter 4
Optional Topic: User Input with Scanner
Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS

2 WHAT IS WRONG? Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); if (name == "Barney") { System.out.println("I love you, you love me,"); System.out.println("We're a happy family!"); }

3 THE EQUALS METHOD Strings and Objects are compared using a method named equals. Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); if (name.equals("Barney")) { System.out.println("I love you, you love me,"); System.out.println("We're a happy family!"); }

4 INDEXES Characters of a string are numbered with 0-based indexes: String name = "R. Kelly"; First character's index : 0 Last character's index : 1 less than the string's length The individual characters are values of type char (seen later) index character R. Kelly

5 STRING METHODS These methods are called using the dot notation: String gangsta = "Dr. Dre"; System.out.println(gangsta.length()); // 7 Method nameDescription str1.indexOf( str2 ) index where the start of str2 appears in str1 (-1 if not found) str1.length() number of characters in this string str1.substring( index1, index2 ) or str1.substring( index1 ) the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, grabs till end of string str1.toLowerCase() a new string with all lowercase letters str1.toUpperCase() a new string with all uppercase letters

6 STRING METHOD EXAMPLES // index String s1 = "Stuart Reges"; String s2 = "Marty Stepp"; System.out.println(s1.length()); // 12 System.out.println(s1.indexOf("e")); // 8 System.out.println(s1.substring(7, 10)); // "Reg" String s3 = s2.substring(1, 7); System.out.println(s3.toLowerCase()); // "arty s" Given the following string: // index String book = "Building Java Programs"; How would you extract the word "Java" ?

7 MODIFYING STRINGS Methods like substring and toLowerCase build and return a new string, rather than modifying the current string. String s = "lil bow wow"; s.toUpperCase(); System.out.println(s); // lil bow wow To modify a variable's value, you must reassign it: String s = "lil bow wow"; s = s.toUpperCase(); System.out.println(s); // LIL BOW WOW An object that can never be modified after creation is called an immutable object. Strings are immutable.

8 STRING TEST METHODS MethodDescription str1.equals( str2 ) whether two strings contain the same characters str1. equalsIgnoreCase( str2 ) whether two strings contain the same characters, ignoring upper vs. lower case str1.startsWith( str2 ) whether str1 contains str2's characters at start str1.endsWith( str2 ) whether str1 contains str2's characters at end str1.contains( str2 ) whether str1 contains str2’s characters anywhere

9 CHECKPOINT 1 Go into your student folder on the X drive Create a new folder named “.fraccalc.1” Example: “bieber.fraccalc.1” Copy your FracCalc.java file into this folder. Open Dropbox (on your desktop) Go into the Period 2 folder Copy your new.fraccalc.1 folder into the dropbox period 2 folder. Copy the entire.fraccalc.1 folder (not just your.java file).

10 LAB PracticeIt: BJP3 Self-Check 3.20 Remember to put “” around Strings in your answers Write a method that accepts a string parameter, and returns the string in reverse order. Example: reverse(“Good day”) should return “yad dooG“

11 SCANNER For most objects (including Scanner objects), we create a new instance with the new keyword: TypeName myInstance = new TypeName(any, parameters); For a Scanner, it looks like this: Scanner scanner = new Scanner(System.in);

12 A BIT MORE MAGIC: IMPORT There’s one other thing we have to do before we can start using our Scanner. We have to tell Java where it can find it! We do this with one more magic Java keyword, import, at the top of the Java source code file: import java.util.*; Eclipse will offer to do this for us if we mouse over the word “ Scanner ” when it has a red squiggly.

13 SCANNER We finally have enough to start using the methods on our Scanner object: import java.util.*; public class MyInteractiveProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Type something: "); String word = scanner.next(); System.out.print("The first word was: " + word); }

14 SCANNER What will this output? I don’t know! It depends on what you type at runtime! import java.util.*; public class MyInteractiveProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Type something: "); String word = scanner.next(); System.out.print("The first word was: " + word); }

15 LET’S TRY IT! What is the radius? 3 A circle with radius 3.0 has circumference A circle with radius 3.0 has area A sphere with radius 3.0 has volume

16 IN YOUR NOTEBOOK The following method signatures are missing their class. List the class to which each method belongs. double abs(double); double nextDouble(); char charAt(int); int ceil(double); int length(); String substring(int, int);