Programming with Java

Similar documents
"Hello" " This " + "is String " + "concatenation"

Using Java Classes Fall 2018 Margaret Reid-Miller

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

Programming with Java

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

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

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

Introduction to Programming Using Java (98-388)

CS313D: ADVANCED PROGRAMMING LANGUAGE

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

More non-primitive types Lesson 06

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

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

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

USING LIBRARY CLASSES

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

Lab 14 & 15: String Handling

CSI33 Data Structures

CS251L REVIEW Derek Trumbo UNM

STUDENT LESSON A10 The String Class

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

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

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

Java Fall 2018 Margaret Reid-Miller

String related classes

AP Computer Science A

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

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

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

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

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

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Activity 9: Object-Oriented

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

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

Index COPYRIGHTED MATERIAL

Chapter 4 Mathematical Functions, Characters, and Strings

String. Other languages that implement strings as character arrays

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

Pace University. Fundamental Concepts of CS121 1

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

An Introduction to Processing

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

Review for Test 1 (Chapter 1-5)

PROGRAMMING FUNDAMENTALS

CS 1301 Ch 8, Part A

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

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

Eng. Mohammed Abdualal

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

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

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

More on variables and methods

Datatypes, Variables, and Operations

Fundamentals of Programming CS-110. Lecture 2

Computational Expression

Introduction to Programming EC-105. Lecture 2

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Full file at

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Object-Oriented Programming

More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

Creating Strings. String Length

Activity 9: Object-Oriented

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Chapter 8: Strings and Things

ECE 122. Engineering Problem Solving with Java

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Programming with Java

ECE 122 Engineering Problem Solving with Java

Ex: If you use a program to record sales, you will want to remember data:

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

Using System.out.println()

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

ENGR 101 Engineering Design Workshop

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

CISC 370: Introduction to Java

Java Identifiers, Data Types & Variables

Language Reference Manual

CIS 110: Introduction to Computer Programming

Control Structures in Java if-else and switch

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Program Elements -- Introduction

CS112 Lecture: Characters and Strings

Values, Variables, Types & Arithmetic Expressions. Agenda

Notes from the Boards Set BN19 Page

Intro to Computer Science II

CST242 Strings and Characters Page 1

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Computing with Numbers

Values and Variables 1 / 30

String Theory. Chapter Cut and Paste

Flow of Control. Chapter 3. Chapter 3 1

Full file at

Transcription:

Programming with Java String & Making Decision Lecture 05 First stage Software Engineering Dep. Saman M. Omer 2017-2018

Objectives By the end of this lecture you should be able to : Understand another data type of java, which is called String. Use a various String class methods. Explain the difference between sequence and selection. Use an if statement to make a single choice in a program. Use an if else statement to make a choice between two options in a program.

Outlines Introduction Creating String type Java String methods If statement If _ else Statement Examples

Introduction to String Generally, string is a sequence of characters. But In Java programming language, string is an object that represents a sequence of characters. Such as: a name, car registration number and any meaningless sequence of characters. java provides a String class that allows us to use and manipulate strings. String is actually a predefined class in the Java library just like the System class, and start with capital letter. The String type is not a primitive type. It is known as a reference type. String is an immutable object which means it is constant and can cannot be changed once it has been created.

Creating a String String: is a sequence of characters within double quotes. There are two ways to create a String in Java String literal String message = "Welcome to Java" ; Or String name ; name = " Welcome " ; Using new keyword String message = new String (" Welcome to Java ") ;

String type example

Java String Methods String class provides many useful methods, and listed some of them in the following. Method length() substring(int startindex) substring(int startindex,int endindex) charat(index) concat)another string) equals() equalsignorecase() touppercase() tolowercase() Description It returns count of total number of characters. it returns a part of the string. for given start index and end index it returns a character at that position in the string combines a specific string at the end of orignal string it compares the two given strings based on the content of the string It compares two string on the basis of content but it does not check the case like equals() It converts all the characters of the String to upper case. It converts all the characters of the String to lower case

Length of Strings length( ) method is used to return the number of characters in the string, including spaces. It will count from position zero, till \0 (excluded). Ex: int numchars; String street = Koya University / Koya ; numchars = street.length( ); Length is = 23

Length of Strings Example

Extracting Substrings We can extract substrings from larger ones. Using the following methods o o substring [start). substring [start, end). The index of the first character is zero, it append a \0 to all strings. The substring method take two parameters, they indicate the start and end of the substring. The start point is included, but the end point is excluded. If a position out of possible range is called, then an error will occur.

Extracting Substrings Example

Concatenating Strings Combines String with another string or other data types for printing. Other primitive data types like: int, double, char, float,... can easily concatenate with strings, and act as one string.. the + symbol is used for concatenation

Concatenating Strings method The String class includes a method for concatenating two strings.

Extracting Individual Characters of a String charat() method, it returns the character at the specified index in the string and store inside char type. Note that indexing start from zero. The first character in a string is at index 0.

String Equality The method equals( ) is used for determining the equality between two different strings. The method compares the two given strings on the basis of content of the string. The method will return true if strings are equal or all the characters are matched, and false if they are not equal.

String Equality equalsignorecase(): This method compares two string on the basis of content but it does not check the case like equals() method. In this method, if the characters match, it returns true else false.

touppercase & tolowercase methods tolowercase(): method converts all the characters of the String to lower case (Small Letter). touppercase() : method converts all the characters of the String to upper case (Capital Letter).

Making Choices Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Making Choices Java programming language provides following types of decision making statements. No. 1 2 3 4 Statement & Description if statement An if statement consists of a boolean expression followed by one or more statements. If.. else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. nested if statement You can use one if or else if statement inside another if or else if statement(s). switch statement A switch statement allows a variable to be tested for equality against a list of values.

The if statement The if statement, provides decision. An if statement consists of a Boolean expression followed by statement(s). Syntax: If (Boolean_expression) Statement(s) ; Note: Statements will execute if the Boolean expression is true. Otherwise, the statement is skipped.

Note

Example

The if else statement The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false. Syntax : if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Note : Only one of the branches will execute, true-case or false-case.

Example

Example Write a Java program to read a value from keyboard, and then determine the result of the following mathematical function:

Example : Write a Java program to read a value from keyboard, and then determine whether the number is an odd or even number.

Example : Write a Java program to read a character from position five of any string that entered and then determine if its digit or not? H e l l o 1 2 3 0 1 2 3 4 5 6 7