This is a lecture on Mathematical Method and Character Type. It covers both theoretical and practical aspects of the use of Repetition Statements (Loops). It covers also a wide variety of examples covering different points pertaining to this topic.
Introduction To PracticalComputer Science I
Lecture 09: Mathematical Method and Character Type
Herat University
Computer Science Faculty
Lecturer: Abdul Khaleq Herawi
Date: April 12, 2025
2.
2
Review
1. (true) ||(3 > 4)
2. !(x > 0) && (x > 0)
3. (x > 0) ^ (x < 0)
4. (x != 0) || (x == 0)
5. (x >= 0) && (x < 0)
6. (x != 1) == !(x == 1)
7. What is conditional or ternary operator?
8. What is syntax of switch?
3.
3
Outline:
Common Mathematicfunction
Character Data Types and Operator
Unicode and ASCII Code
Casting Between Char and NumericTypes
4.
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 4
Common Mathematics Functions:
Java provides many useful methods in the Math class for performing common
mathematical functions.
A method is a group of statements that performs a specific task.
Methods in Math class can be categorized as trigonometric methods,
exponent methods, and service methods
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 11
Cont…
11. Math.rint(-2.1) returns -2.0
12. Math.rint(2.5) returns 2.0
13. Math.rint(4.5) returns 4.0
14. Math.rint(-2.5) returns -2.0
15. Math.round(2.6f) returns 3 // Returns int
16. Math.round(2.0) returns 2 // Returns long
17. Math.round(-2.0f) returns -2 // Returns int
18. Math.round(-2.6) returns -3 // Returns long
19. Math.round(-2.4) returns -2 // Returns long
12.
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 12
The Min, Max,And Abs Methods
The min and max methods return the minimum and maximum numbers of two numbers (int, long, float, or
double).
The abs method returns the absolute value of the number (int, long, float, or double).
1. Math.max(2, 3) returns 3
2. Math.max(2.5, 3) returns 4.0
3. Math.min(2.5, 4.6) returns 2.5
4. Math.abs(-2) returns 2
5. Math.abs(-2.1) returns 2.1
13.
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 13
Character DataType And Operations
A character data type represents a single character.
The character data type, char, is used to represent a single character.A character
literal is enclosed in single quotation marks. For Example:
char letter = 'A’;
char numChar = '4';
14.
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 14
Unicode And ASCII Code
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. How characters are encoded is
defined by an encoding scheme.
Unicode And ASCII Code
15.
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 15
Unicode And ASCII Code
Unicode: Unicode takes two bytes, preceded by u, expressed in four hexadecimal
digits that run from u0000 to uFFFF
ASCII (American Standard Code for Information Interchange): an 8-bit encoding scheme
for representing all uppercase and lowercase letters, digits, punctuation marks, and
control characters.
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 17
Casting Between Char And NumericTypes
A char can be cast into any numeric type, and vice versa.
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.
char ch = (char)65.25; // Decimal 65 is assigned to ch
System.out.println(ch); // ch is character A
When a char is cast into a numeric type, the character’s Unicode is cast into the specified numeric type.
int i = (int)'A'; // The Unicode of character A is assigned to i
System.out.println(i); // i is 65
18.
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 18
Cont…
All numeric operators can be applied to char operands.
A char operand is automatically cast into a number if the other operand is a number or a character.
If the other operand is a string, the character is concatenated with the string. For example, the following
statements
INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 20
Example:
1. System.out.println("isDigit('a') is " +
Character.isDigit('a'));
2. System.out.println("isLetter('a') is " +
Character.isLetter('a’));
3. System.out.println("isLowerCase('a') is " +
Character.isLowerCase('a’));
4. System.out.println("isUpperCase('a') is " +
Character.isUpperCase('a’));
5. System.out.println("toLowerCase('T') is " +
Character.toLowerCase('T’));
6. System.out.println("toUpperCase('q') is " +
Character.toUpperCase('q'));
displays
1. isDigit('a') is false
2. isLetter('a') is true
3. isLowerCase('a') is true
4. isUpperCase('a') is false
5. toLowerCase('T') is t
6. toUpperCase('q') is Q