際際滷

際際滷Share a Scribd company logo
Programming Language
Foundation
BY Dr. BABAOUSMAIL HASSEN
LECTURER AT BINJIANG COLLEGE OF NUIST
Chapter 4
Mathematical Functions,
Characters, and Strings
Continue
Case Study:
Computing Angles of a Triangle
Write a program that prompts the user to enter the x- and y-
coordinates of the three corner points in a triangle and then
displays the triangles angles.
3
A
B
C
a
b
c
A = acos((a * a - b * b - c * c) / (-2 * b * c))
B = acos((b * b - a * a - c * c) / (-2 * a * c))
C = acos((c * c - b * b - a * a) / (-2 * a * b))
x1, y1
x2, y2
x3, y3
Character Data Type
In addition to processing numeric values, you can process
characters in Java.
The character data type, char, is used to represent a single
character.
A character literal is enclosed in single quotation marks.
Consider the following code:
char letter = 'A';
char numChar = '4';
 The first statement assigns character A to the char variable letter.
 The second assigns digit character 4 to the char variable numChar.
4
Character Data Type,
Computers use binary numbers internally. A character is stored in a computer as a
sequence of 0s and 1s.
Mapping a character to its binary representation is called encoding. There are
different ways to encode a character.
 Unicode
 ASCII code
Java supports Unicode, which was originally designed as a 16-bit character
encoding. A 16-bit Unicode takes two bytes, preceded by u, expressed in four
hexadecimal digits that run from u0000 to uFFFF.
Most computers use ASCII (American Standard Code for Information Interchange),
an 8-bit encoding.
Unicode includes ASCII code, with u0000 to u007F corresponding to the 128
ASCII characters.
5
Character Data Type,
NOTE: The increment and decrement operators can also be
used on char variables to get the next or preceding Unicode
character.
For example, the following statements display character b.
 char ch = 'a;
 System.out.println(++ch);
6
char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)
char letter = 'u0041'; (Unicode)
char numChar = 'u0034'; (Unicode)
Four
hexadecimal
digits.
Unicode Format
Unicode takes two bytes, preceded by u, expressed in four
hexadecimal numbers that run from 'u0000' to 'uFFFF'.
Unicode can represent 65534 characters.
7
Unicode u03b1 u03b2 u03b3 for three Greek
letters
ASCII Character Set,
ASCII Character Set is a subset of the Unicode from
u0000 to u007F
8
ASCII Code for Commonly Used Characters
9
Characters Code Value in Decimal Unicode Value
'0' to '9' 48 to 57 u0030 to u0039
'A' to 'Z' 65 to 90 u0041 to u005A
'a' to 'z' 97 to 122 u0061 to u007A
Escape Sequences
for Special Characters
10
A char can be cast into any numeric type, and vice versa.
 When an integer is cast into a char, only its lower 16 bits of data are used; the other
part is ignored.
 When a floating-point value is cast into a char, the floating-point value is first cast
into an int, which is then cast into a char.
 When a char is cast into a numeric type, the characters Unicode is cast into the
specified numeric type.
11
Casting between char and
Numeric Types
Casting between char and Numeric
Types, cont.
Implicit casting can be used if the result of a casting fits into
the target variable. Otherwise, explicit casting must be used.
For example,
12
Comparing and Testing Characters
Two characters can be compared using the relational
operators just like comparing two numbers. This is done by
comparing the Unicodes of the two characters.
For example,
if (ch >= 'A'&& ch <= 'Z')
System.out.println(ch + " is an uppercase letter");
else if (ch >= 'a' && ch <= 'z')
System.out.println(ch + " is a lowercase letter");
else if (ch >= '0' && ch <= '9')
System.out.println(ch + " is a numeric character");
13
Methods in the Character Class
For convenience, Java provides the following methods in the
Character class for testing characters.
14
Method Description
isDigit(ch) Returns true if the specified character is a digit.
isLetter(ch) Returns true if the specified character is a letter.
isLetterOfDigit(ch) Returns true if the specified character is a letter or digit.
isLowerCase(ch) Returns true if the specified character is a lowercase letter.
isUpperCase(ch) Returns true if the specified character is an uppercase letter.
toLowerCase(ch) Returns the lowercase of the specified character.
toUpperCase(ch) Returns the uppercase of the specified character.
The String Type
The char type only represents one character. To represent a string of
characters, use the data type called String.
For example,
String message = "Welcome to Java ";
The String type is not a primitive type. It is known as a reference
type. Any Java class can be used as a reference type for a variable.
Reference data types will be thoroughly discussed in Chapter 9.
For the time being, you just need to know
 how to declare a String variable,
 how to assign a string to the variable,
 how to concatenate strings,
 to perform simple operations for strings.
15
Simple Methods for String Objects
16
Method Description
Returns the number of characters in this string.
Returns the character at the specified index from this string.
Returns a new string that concatenates this string with string s1.
Returns a new string with all letters in uppercase.
Returns a new string with all letters in lowercase.
Returns a new string with whitespace characters trimmed on both sides.
length()
charAt(index)
concat(s1)
toUpperCase()
toLowerCase()
trim()
Simple Methods for String Objects
Strings are objects in Java. The methods in the preceding table can only be invoked
from a specific string instance. For this reason, these methods are called instance
methods.
The syntax to invoke an instance method is
referenceVariable.methodName(arguments)
 A non-instance method is called a static method. A static method can be invoked
without using an object.
 The syntax to invoke a static method can be
ClassName.methodName(arguments)
 All the methods defined in the Math class are static methods. They are not tied to
a specific object instance. For example, the pow method in the Math class can be
invoked using.
Math.pow(2, 2.5)
17
Getting String Length
String message = "Welcome to Java";
System.out.println("The length of " + message +
" is  + message.length( ));
18
Getting Characters from a String
String message = "Welcome to Java";
System.out.println("The first character in
message is  + message.charAt(0));
19
Ad

Recommended

LiangChapter4 Unicode , ASCII Code .ppt
LiangChapter4 Unicode , ASCII Code .ppt
zainiiqbal761
11-ch04-3-strings.pdf
11-ch04-3-strings.pdf
AndreaBatholomeo
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
ssusere3b1a2
Chapter 4 Mathematical Functions Character and string
Chapter 4 Mathematical Functions Character and string
AhsirYu
Lecture 3.pptx
Lecture 3.pptx
ShehabEldinSaid
04slide Update.pptx
04slide Update.pptx
Adenomar11
Java cn b畉n - Chapter9
Java cn b畉n - Chapter9
Vince Vo
Java string handling
Java string handling
GaneshKumarKanthiah
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
Chapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
Chapter 2 java
Chapter 2 java
ahmed abugharsa
String Method.pptx
String Method.pptx
Dreime Estandarte
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
Week02
Week02
hccit
String and string buffer
String and string buffer
kamal kotecha
Lecture-Java ProgrammingTheWorldii6.pptx
Lecture-Java ProgrammingTheWorldii6.pptx
talhazc013
Chapter i(introduction to java)
Chapter i(introduction to java)
Chhom Karath
Dr. Rajeshree Khande : Programming concept of basic java
Dr. Rajeshree Khande : Programming concept of basic java
jalinder123
Dr. Rajeshree Khande : Java Basics
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
Java String class
Java String class
DrRajeshreeKhande
String classes and its methods.20
String classes and its methods.20
myrajendra
Cso gaddis java_chapter10
Cso gaddis java_chapter10
mlrbrown
Programming fundamental
Programming fundamental
Mukesh Thakur
JAVA PROGRAMMING : Data types
JAVA PROGRAMMING : Data types
Kongu Engineering College, Perundurai, Erode
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5e
Gina Bullock
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5e
Gina Bullock
String in java, string constructors and operations
String in java, string constructors and operations
manjeshbngowda
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
Atul Sehdev
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science

More Related Content

Similar to Java Object Orientend Programming 1.pptx (20)

21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
Chapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
Chapter 2 java
Chapter 2 java
ahmed abugharsa
String Method.pptx
String Method.pptx
Dreime Estandarte
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
Week02
Week02
hccit
String and string buffer
String and string buffer
kamal kotecha
Lecture-Java ProgrammingTheWorldii6.pptx
Lecture-Java ProgrammingTheWorldii6.pptx
talhazc013
Chapter i(introduction to java)
Chapter i(introduction to java)
Chhom Karath
Dr. Rajeshree Khande : Programming concept of basic java
Dr. Rajeshree Khande : Programming concept of basic java
jalinder123
Dr. Rajeshree Khande : Java Basics
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
Java String class
Java String class
DrRajeshreeKhande
String classes and its methods.20
String classes and its methods.20
myrajendra
Cso gaddis java_chapter10
Cso gaddis java_chapter10
mlrbrown
Programming fundamental
Programming fundamental
Mukesh Thakur
JAVA PROGRAMMING : Data types
JAVA PROGRAMMING : Data types
Kongu Engineering College, Perundurai, Erode
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5e
Gina Bullock
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5e
Gina Bullock
String in java, string constructors and operations
String in java, string constructors and operations
manjeshbngowda
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
Atul Sehdev
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
Chapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
Week02
Week02
hccit
String and string buffer
String and string buffer
kamal kotecha
Lecture-Java ProgrammingTheWorldii6.pptx
Lecture-Java ProgrammingTheWorldii6.pptx
talhazc013
Chapter i(introduction to java)
Chapter i(introduction to java)
Chhom Karath
Dr. Rajeshree Khande : Programming concept of basic java
Dr. Rajeshree Khande : Programming concept of basic java
jalinder123
Dr. Rajeshree Khande : Java Basics
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
String classes and its methods.20
String classes and its methods.20
myrajendra
Cso gaddis java_chapter10
Cso gaddis java_chapter10
mlrbrown
Programming fundamental
Programming fundamental
Mukesh Thakur
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5e
Gina Bullock
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5e
Gina Bullock
String in java, string constructors and operations
String in java, string constructors and operations
manjeshbngowda
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
Atul Sehdev

Recently uploaded (20)

BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
GlysdiEelesor1
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
Paper 109 | Archetypal Journeys in Interstellar: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in Interstellar: Exploring Universal Themes...
Rajdeep Bavaliya
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
Revista digital preescolar en transformaci坦n
Revista digital preescolar en transformaci坦n
guerragallardo26
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
Non-Communicable Diseases and National Health Programs Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
GlysdiEelesor1
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
Paper 109 | Archetypal Journeys in Interstellar: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in Interstellar: Exploring Universal Themes...
Rajdeep Bavaliya
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
Revista digital preescolar en transformaci坦n
Revista digital preescolar en transformaci坦n
guerragallardo26
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
Non-Communicable Diseases and National Health Programs Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
Ad

Java Object Orientend Programming 1.pptx

  • 1. Programming Language Foundation BY Dr. BABAOUSMAIL HASSEN LECTURER AT BINJIANG COLLEGE OF NUIST
  • 3. Case Study: Computing Angles of a Triangle Write a program that prompts the user to enter the x- and y- coordinates of the three corner points in a triangle and then displays the triangles angles. 3 A B C a b c A = acos((a * a - b * b - c * c) / (-2 * b * c)) B = acos((b * b - a * a - c * c) / (-2 * a * c)) C = acos((c * c - b * b - a * a) / (-2 * a * b)) x1, y1 x2, y2 x3, y3
  • 4. Character Data Type In addition to processing numeric values, you can process characters in Java. The character data type, char, is used to represent a single character. A character literal is enclosed in single quotation marks. Consider the following code: char letter = 'A'; char numChar = '4'; The first statement assigns character A to the char variable letter. The second assigns digit character 4 to the char variable numChar. 4
  • 5. Character Data Type, Computers use binary numbers internally. A character is stored in a computer as a sequence of 0s and 1s. Mapping a character to its binary representation is called encoding. There are different ways to encode a character. Unicode ASCII code Java supports Unicode, which was originally designed as a 16-bit character encoding. A 16-bit Unicode takes two bytes, preceded by u, expressed in four hexadecimal digits that run from u0000 to uFFFF. Most computers use ASCII (American Standard Code for Information Interchange), an 8-bit encoding. Unicode includes ASCII code, with u0000 to u007F corresponding to the 128 ASCII characters. 5
  • 6. Character Data Type, NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. char ch = 'a; System.out.println(++ch); 6 char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) char letter = 'u0041'; (Unicode) char numChar = 'u0034'; (Unicode) Four hexadecimal digits.
  • 7. Unicode Format Unicode takes two bytes, preceded by u, expressed in four hexadecimal numbers that run from 'u0000' to 'uFFFF'. Unicode can represent 65534 characters. 7 Unicode u03b1 u03b2 u03b3 for three Greek letters
  • 8. ASCII Character Set, ASCII Character Set is a subset of the Unicode from u0000 to u007F 8
  • 9. ASCII Code for Commonly Used Characters 9 Characters Code Value in Decimal Unicode Value '0' to '9' 48 to 57 u0030 to u0039 'A' to 'Z' 65 to 90 u0041 to u005A 'a' to 'z' 97 to 122 u0061 to u007A
  • 11. A char can be cast into any numeric type, and vice versa. When an integer is cast into a char, only its lower 16 bits of data are used; the other part is ignored. When a floating-point value is cast into a char, the floating-point value is first cast into an int, which is then cast into a char. When a char is cast into a numeric type, the characters Unicode is cast into the specified numeric type. 11 Casting between char and Numeric Types
  • 12. Casting between char and Numeric Types, cont. Implicit casting can be used if the result of a casting fits into the target variable. Otherwise, explicit casting must be used. For example, 12
  • 13. Comparing and Testing Characters Two characters can be compared using the relational operators just like comparing two numbers. This is done by comparing the Unicodes of the two characters. For example, if (ch >= 'A'&& ch <= 'Z') System.out.println(ch + " is an uppercase letter"); else if (ch >= 'a' && ch <= 'z') System.out.println(ch + " is a lowercase letter"); else if (ch >= '0' && ch <= '9') System.out.println(ch + " is a numeric character"); 13
  • 14. Methods in the Character Class For convenience, Java provides the following methods in the Character class for testing characters. 14 Method Description isDigit(ch) Returns true if the specified character is a digit. isLetter(ch) Returns true if the specified character is a letter. isLetterOfDigit(ch) Returns true if the specified character is a letter or digit. isLowerCase(ch) Returns true if the specified character is a lowercase letter. isUpperCase(ch) Returns true if the specified character is an uppercase letter. toLowerCase(ch) Returns the lowercase of the specified character. toUpperCase(ch) Returns the uppercase of the specified character.
  • 15. The String Type The char type only represents one character. To represent a string of characters, use the data type called String. For example, String message = "Welcome to Java "; The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable. Reference data types will be thoroughly discussed in Chapter 9. For the time being, you just need to know how to declare a String variable, how to assign a string to the variable, how to concatenate strings, to perform simple operations for strings. 15
  • 16. Simple Methods for String Objects 16 Method Description Returns the number of characters in this string. Returns the character at the specified index from this string. Returns a new string that concatenates this string with string s1. Returns a new string with all letters in uppercase. Returns a new string with all letters in lowercase. Returns a new string with whitespace characters trimmed on both sides. length() charAt(index) concat(s1) toUpperCase() toLowerCase() trim()
  • 17. Simple Methods for String Objects Strings are objects in Java. The methods in the preceding table can only be invoked from a specific string instance. For this reason, these methods are called instance methods. The syntax to invoke an instance method is referenceVariable.methodName(arguments) A non-instance method is called a static method. A static method can be invoked without using an object. The syntax to invoke a static method can be ClassName.methodName(arguments) All the methods defined in the Math class are static methods. They are not tied to a specific object instance. For example, the pow method in the Math class can be invoked using. Math.pow(2, 2.5) 17
  • 18. Getting String Length String message = "Welcome to Java"; System.out.println("The length of " + message + " is + message.length( )); 18
  • 19. Getting Characters from a String String message = "Welcome to Java"; System.out.println("The first character in message is + message.charAt(0)); 19