Programming 2 CS112- Lab 2 Java

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
1 Strings and String Operations Overview l Creating String Objects l Substring methods l The Concatenation Operator l Strings are Immutable l Other Methods.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Fundamental Programming Structures in Java: Strings.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Strings Reading for this Lecture, L&L, 3.2. Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
TA: Nouf Al-Harbi NoufNaief.net :::
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Strings.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
String & Composition. Agenda This keyword. String class. String operations. Composition.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 1.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
CHAPTER 8 File Input Output Part 1: String. The String Class  Constructing a String: String message = "Welcome to Java“; String message = new String("Welcome.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
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.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
17-Feb-16 String and StringBuilder Part I: String.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
String and StringBuffer classes
Chapter 4 Mathematical Functions, Characters, and Strings
Strings, Characters and Regular Expressions
Strings.
String class.
EKT 472: Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
String Handling in JAVA
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
Chapter 9 Strings and Text I/O
String and StringBuilder
Java – String Handling.
Lecture 07 String Jaeki Song.
String and StringBuilder
CS2011 Introduction to Programming I Strings
String methods 26-Apr-19.
Strings in Java.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Programming 2 CS112- Lab 2 Java TA: Nouf Al-Harbi www.acadox.com/class/14347 nouf200@hotmail.com

Lab Objectives The String Class Applications: String. Methods of the String Class. Exercises. Lab4_1, Programming 2

String String is a sequence of characters enclosed in quotes. E.g. “Hello” Once a String object is created it cannot be changed. Stings are Immutable. To get changeable strings use the class called StringBuffer. String and StringBuffer classes are declared final, so there cannot be subclasses of these classes. Lab4_1, Programming 2

How to Create a String String newString = new String(stringLiteral); String s1= new String("Welcome to Java"); Since strings are used frequently, Java provides a shorthand initializer for creating a string: String s1= "Welcome to Java"; Lab4_1, Programming 2

Strings Are Immutable A String object is immutable: its content cannot be changed. Does the following code changes the content of the string? String s = "Java"; s = "HTML"; Lab4_1, Programming 2

Trace Code String s = "Java"; s = "HTML"; Lab4_1, Programming 2

Another Example of Immutability String str1 = “Hello”; String str2 = “Hello”; Χ √ Lab4_1, Programming 2

Interned Strings Since strings are immutable and are frequently used, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. For example, the following statements: Lab4_1, Programming 2

s1 == s2 is false s1 == s3 is true Examples s1 == s2 is false s1 == s3 is true A new object is created if you use the new operator. If you use the string initializer, no new object is created if the interned object is already created. Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method char x=str.charAt(2); Returns the character at the specified index. char charAt(int index) Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method int comp=str.compareTo(str2); int comp2=str.compareToIngoreCase (str2); Compares this String to another Object or String. returns +ve number, 0, or -ve number if this String is greater than, equal to or less than s. int compareTo(String s) int compareToIngoreCase(s) Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method String str2=" Programm "; String res=str.concat(str2); Concatenates the specified string to the end of this string. String concat(String str) Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method boolean isEqual=str.equals(str); Compares this string to the specified object. returns true if s the same as this String. boolean equals(Object anObject) Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method int len=str.length(); Returns the length of this string. int length() Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method String res1=str.substring(2); Returns a new string that is a substring of this string. String substring(int beginIndex) Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method String res1=str.substring(2,6); Returns a new string that is a substring of this string. String substring(int beginIndex, int endIndex) Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method String upCase=str.toUpperCase(); String lowCase=str.toLowerCase(); returns a new String, equivalent to the Upper/lower case of this String String toUpperCase() String toLowerCase() Lab4_1, Programming 2

String/Character Methods String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Example Description Method boolean isLetter=Character.isLetter('r'); Ex2: boolean isLetter=Character.isLetter(str.charAt(5)); determines if the specified character is a letter. Character.isLetter(char ch) Lab4_1, Programming 2

Returns the character at the specified index. char charAt(int index) Description Method Returns the character at the specified index. char charAt(int index) Compares this String to another Object or String. returns +ve number, 0, or -ve number if this String is greater than, equal to or less than s. int compareTo(String s) int compareToIngoreCase(s) Concatenates the specified string to the end of this string. String concat(String str) Compares this string to the specified object. returns true if s the same as this String. boolean equals(Object anObject) Returns the length of this string. int length() Returns a new string that is a substring of this string. String substring(int beginIndex) String substring(int beginIndex, int endIndex) determines if the specified character is a letter. Character.isLetter(char ch) returns a new String, equivalent to the Upper/lower case of this String String toUpperCase() String toLowerCase() Lab4_1, Programming 2

Example of String Operations Using methods of the String Class. Design and implement a Java program that will do the following operations to this string s1=“Welcome to java” s2=“Welcome to Java” Print out the length of the string. Use concat method which concatenates s1 to s2. Use CharAt method which returns the character of s1 at index 1 Convert all s1 alphabets to capital letters and print out the result. Convert all s1 alphabets to lower-case letters and print out the result. Lab4_1, Programming 2

Example of String Operations Use equals method which compare s1 to s2. Use equalsIgnoreCase method which compare s1 to s2 ignoring case consideration. Use CompareTo method which compare s1 to s2. Use Substring method which extracting substring from s1. Lab4_1, Programming 2

Constructing two strings Lab4_1, Programming 2

Using Length method which returns the length of s1 Lab4_1, Programming 2

Using concat method which concatenates s1 to s2 Lab4_1, Programming 2

Using CharAt method which returns the character of s1 at index 1 Lab4_1, Programming 2

Using toUpperCase method which converts all the character of s1 to uppercase Lab4_1, Programming 2

Using toLowerCase method which converts all the character of s1 to lowercase Lab4_1, Programming 2

method which compare s1 to s2 Using equals method which compare s1 to s2 Lab4_1, Programming 2

Using equalsIgnoreCase method which compare s1 to s2 ignoring case consideration Lab4_1, Programming 2

method which compare s1 to s2 Using CompareTo method which compare s1 to s2 Lab4_1, Programming 2

method which extracting substring from s1 Using Substring method which extracting substring from s1 Lab4_1, Programming 2

The Output Lab4_1, Programming 2

Exercise1: Phone keypads Problem Description: The international standard letter/number mapping found on the telephone is shown below: Lab4_1, Programming 2

Exercise1: Phone keypads Write a method that returns a number, given an uppercase letter, as follows: public static int getNumber(char uppercaseLetter) Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (upper- or lowercase) to a digit and leaves all other characters intact. Lab4_1, Programming 2

Output Lab4_1, Programming 2

Any question Thank you Lab4_1, Programming 2