Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Similar documents
Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CST242 Strings and Characters Page 1

String. Other languages that implement strings as character arrays

Chapter 10: Text Processing and More about Wrapper Classes

Eng. Mohammed Abdualal

Class Library java.lang Package. Bok, Jong Soon


Lab 14 & 15: String Handling

Creating Strings. String Length

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

String related classes

STRINGS AND STRINGBUILDERS. Spring 2019

More on variables and methods

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

Introductory Mobile Application Development

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

"Hello" " This " + "is String " + "concatenation"

Index COPYRIGHTED MATERIAL

CS 1301 Ch 8, Part A

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

Eng. Mohammed Abdualal

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

MATHEMATICAL FUNCTIONS CHARACTERS, AND STRINGS. INTRODUCTION IB DP Computer science Standard Level ICS3U

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

A variable is a name for a location in memory A variable must be declared

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

STUDENT LESSON A10 The String Class

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

USING LIBRARY CLASSES

CSC Java Programming, Fall Java Data Types and Control Constructs

Java Foundations: Unit 3. Parts of a Java Program

APPENDIX A: SUMMARY OF IMPORTANT JAVA FEATURES

Introduction to Programming Using Java (98-388)

Programming with Java

Computational Expression

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Interaction with Android

Using Java Classes Fall 2018 Margaret Reid-Miller

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Full file at

CS 116x Winter 2015 Craig S. Kaplan. Module 09 Text Processing. Topics

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

5/23/2015. Core Java Syllabus. VikRam ShaRma

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

CMPT 125: Lecture 3 Data and Expressions

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

More non-primitive types Lesson 06

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Text. Text Actions. String Contains

CS251L REVIEW Derek Trumbo UNM

Full file at

String Processing CS 1111 Introduction to Programming Fall 2018

Strings, Strings and characters, String class methods. JAVA Standard Edition

Programming Techniques

We now start exploring some key elements of the Java programming language and ways of performing I/O

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

What did we talk about last time? Math methods boolean operations char operations

Chapter 8 Strings. Chapter 8 Strings

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Lesson:9 Working with Array and String

c) And last but not least, there are javadoc comments. See Weiss.

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming. Lecture 11: C Characters and Strings

Language Reference Manual

Basic Computation. Chapter 2

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Variables, Constants, and Data Types

Chapter 4 Mathematical Functions, Characters, and Strings

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Getting started with Java

Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables

A couple of decent C++ web resources you might want to bookmark:

Strings! Today! CSE String Methods!

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Strings are actually 'objects' Strings

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Chapter. Let's explore some other fundamental programming concepts

An Introduction to Processing

Topics. Chapter 5. Equality Operators

Transcription:

Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All

Advanced Java

This chapter discusses class String, class StringBuilder and class Character from the java.lang package. These classes provide the foundation for string and character manipulation in Java. (C) 2010 Pearson Education, Inc. All

A program may contain character literals. An integer value represented as a character in single quotes. The value of a character literal is the integer value of the character in the Unicode character set. String literals (stored in memory as String objects) are written as a sequence of characters in double quotation marks. (C) 2010 Pearson Education, Inc. All

Class String is used to represent strings in Java. The next several subsections cover many of class String s capabilities. (C) 2010 Pearson Education, Inc. All

No-argument constructor creates a String that contains no characters (i.e., the empty string, which can also be represented as "") and has a length of 0. Constructor that takes a String object copies the argument into the new String. Constructor that takes a char array creates a String containing a copy of the characters in the array. Constructor that takes a char array and two integers creates a String containing the specified portion of the array. (C) 2010 Pearson Education, Inc. All

String method length determines the number of characters in a string. String method charat returns the character at a specific position in the String. String method getchars copies the characters of a String into a character array. The first argument is the starting index in the String from which characters are to be copied. The second argument is the index that is one past the last character to be copied from the String. The third argument is the character array into which the characters are to be copied. The last argument is the starting index where the copied characters are placed in the target character array. (C) 2010 Pearson Education, Inc. All

Strings are compared using the numeric codes of the characters in the strings. Figure 16.3 demonstrates String methods equals, equalsignorecase, compareto and regionmatches and using the equality operator == to compare String objects (only compares objects). (C) 2010 Pearson Education, Inc. All

Method equals tests any two objects for equality The method returns true if the contents of the objects are equal, and false otherwise. Uses a lexicographical comparison. When primitive-type values are compared with ==, the result is true if both values are identical. When references are compared with ==, the result is true if both references refer to the same object in memory. Java treats all string literal objects with the same contents as one String object to which there can be many references. (C) 2010 Pearson Education, Inc. All

String methods startswith and endswith determine whether strings start with or end with a particular set of characters (C) 2010 Pearson Education, Inc. All

Figure 16.5 demonstrates the many versions of String methods indexof and lastindexof that search for a specified character or substring in a String. indexof(string str, int fromindex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. lastindexof (int ch, int fromindex) Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. (C) 2010 Pearson Education, Inc. All

Class String provides two substring methods to enable a new String object to be created by copying part of an existing String object. Each method returns a new String object. The version that takes one integer argument specifies the starting index in the original String from which characters are to be copied. The version that takes two integer arguments receives the starting index from which to copy characters in the original String and the index one beyond the last character to copy. (C) 2010 Pearson Education, Inc. All

String method concat concatenates two String objects and returns a new String object containing the characters from both original Strings. The original Strings to which s1 and s2 refer are not modified. (C) 2010 Pearson Education, Inc. All

Method replace return a new String object in which every occurrence of the first char argument is replaced with the second. An overloaded version enables you to replace substrings rather than individual characters. Method touppercase generates a new String with uppercase letters. Method tolowercase returns a new String object with lowercase letters. Method trim generates a new String object that removes all whitespace characters that appear at the beginning or end of the String on which trim operates. Method tochararray creates a new character array containing a copy of the characters in the String. (C) 2010 Pearson Education, Inc. All

Class String provides static valueof methods that take an argument of any type and convert it to a String object. Class StringBuilder is used to create and manipulate dynamic string information. Every StringBuilder is capable of storing a number of characters specified by its capacity. If the capacity of a StringBuilder is exceeded, the capacity expands to accommodate the additional characters. (C) 2010 Pearson Education, Inc. All

Eight type-wrapper classes that enable primitive-type values to be treated as objects: Boolean, Character, Double, Float, Byte, Short, Integer and Long Most Character methods are static methods designed for convenience in processing individual char values. (C) 2010 Pearson Education, Inc. All

Method isdefined determines whether a character is defined in the Unicode character set. Method isdigit determines whether a character is a defined Unicode digit. Method isjavaidentifierstart determines whether a character can be the first character of an identifier in Java that is, a letter, an underscore (_) or a dollar sign ($). Method isjavaidentifierpart determine whether a character can be used in an identifier in Java that is, a digit, a letter, an underscore (_) or a dollar sign ($). (C) 2010 Pearson Education, Inc. All

Method isletter determines whether a character is a letter. Method isletterordigit determines whether a character is a letter or a digit. Method islowercase determines whether a character is a lowercase letter. Method isuppercase determines whether a character is an uppercase letter. Method touppercase converts a character to its uppercase equivalent. Method tolowercase converts a character to its lowercase equivalent. (C) 2010 Pearson Education, Inc. All

Java automatically converts char literals into Character objects when they are assigned to Character variables Process known as autoboxing. Method charvalue returns the char value stored in the object. Method tostring returns the String representation of the char value stored in the object. Method equals determines if two Characters have the same contents. (C) 2010 Pearson Education, Inc. All

When you read a sentence, your mind breaks it into tokens individual words and punctuation marks that convey meaning. Compilers also perform tokenization. String method split breaks a String into its component tokens and returns an array of Strings. Tokens are separated by delimiters Typically white-space characters such as space, tab, newline and carriage return. Other characters can also be used as delimiters to separate tokens. (C) 2010 Pearson Education, Inc. All

Savings Account Class. Create class SavingsAccount. Use a static variable annualinterestrate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsbalance indicating the amount the saver currently has on deposit. Provide method calculatemonthlyinterest to calculate the monthly interest by multiplying the savingsbalance by annualinterestrate divided by 12 - this interest should be added to savings-balance. Provide a static method modifyinterestrate that sets the annualinterestrate to a new value. Write a program to test class SavingsAccount. Instantiate two savingsaccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualinterestrate to 4%, then calculate the monthly interest for each of 12 months and print the new balances for both savers. Next, set the annualinterestrate to 5%, calculate the next month s interest and print the new balances for both savers.

Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle s perimeter and area. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle. (C) 2010 Pearson Education, Inc. All

Readings part II Chapter 16 (C) 2010 Pearson Education, Inc. All