SlideShare 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 triangle’s 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 character’s 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

More Related Content

PPT
LiangChapter4 Unicode , ASCII Code .ppt
PDF
11-ch04-3-strings.pdf
PPTX
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
PPTX
ch 4 mathematical function character and string.pptx
PPT
Chapter 4 Mathematical Functions Character and string
PPTX
Lecture 3.pptx
PPTX
04slide Update.pptx
PPT
Java căn bản - Chapter9
LiangChapter4 Unicode , ASCII Code .ppt
11-ch04-3-strings.pdf
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
ch 4 mathematical function character and string.pptx
Chapter 4 Mathematical Functions Character and string
Lecture 3.pptx
04slide Update.pptx
Java căn bản - Chapter9

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

PPTX
Java string handling
PPTX
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
PPT
Chapter 9 - Characters and Strings
PPT
Chapter 2 java
PPTX
String Method.pptx
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
PDF
Week02
PDF
Java Programming 9th Edition Farrell Solutions Manual
PPS
String and string buffer
PPTX
Lecture-Java ProgrammingTheWorldii6.pptx
PPTX
Chapter i(introduction to java)
PPSX
Dr. Rajeshree Khande : Programming concept of basic java
PPSX
Dr. Rajeshree Khande : Java Basics
PPSX
Java String class
PPT
String classes and its methods.20
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
PPT
Cso gaddis java_chapter10
PPTX
Java Module 2 -Vikas.pptx About Java Programming
PPTX
Programming fundamental
PPT
JAVA PROGRAMMING : Data types
Java string handling
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
Chapter 9 - Characters and Strings
Chapter 2 java
String Method.pptx
Charcater and Strings.ppt Charcater and Strings.ppt
Week02
Java Programming 9th Edition Farrell Solutions Manual
String and string buffer
Lecture-Java ProgrammingTheWorldii6.pptx
Chapter i(introduction to java)
Dr. Rajeshree Khande : Programming concept of basic java
Dr. Rajeshree Khande : Java Basics
Java String class
String classes and its methods.20
Strings in javamnjn ijnjun oinoin oinoi .ppt
Cso gaddis java_chapter10
Java Module 2 -Vikas.pptx About Java Programming
Programming fundamental
JAVA PROGRAMMING : Data types
Ad

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Sports Quiz easy sports quiz sports quiz
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Pre independence Education in Inndia.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
Lesson notes of climatology university.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
master seminar digital applications in india
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPH.pptx obstetrics and gynecology in nursing
Sports Quiz easy sports quiz sports quiz
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Anesthesia in Laparoscopic Surgery in India
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Supply Chain Operations Speaking Notes -ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
Pharma ospi slides which help in ospi learning
Pre independence Education in Inndia.pdf
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
Institutional Correction lecture only . . .
Lesson notes of climatology university.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
master seminar digital applications in india
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 triangle’s 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 character’s 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