Introduction To Practical Computer Science I
Lecture 09: Mathematical Method and Character Type
Herat University
Computer Science Faculty
Lecturer: Abdul Khaleq Herawi
Date: April 12, 2025
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
Outline:
 Common Mathematic function
 Character Data Types and Operator
 Unicode and ASCII Code
 Casting Between Char and NumericTypes
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 5
Trigonometric Methods InThe Math Class
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 6
Cont…
1. Math.toDegrees(Math.PI / 2) returns 90.0
2. Math.toRadians(30) returns 0.5236 (same as π/6)
3. Math.sin(0) returns 0.0
4. Math.sin(Math.toRadians(270)) returns -1.0
5. Math.sin(Math.PI / 6) returns 0.5
6. Math.sin(Math.PI / 2) returns 1.0
7. Math.cos(0) returns 1.0
8. Math.cos(Math.PI / 6) returns 0.866
9. Math.cos(Math.PI / 2) returns 0
10. Math.asin(0.5) returns 0.523598333 (same as π/6)
11. Math.acos(0.5) returns 1.0472 (same as π/3)
12. Math.atan(1.0) returns 0.785398 (same as π/4)
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 7
Exponent Methods
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 8
Cont…
1. Math.exp(1) returns 2.71828
2. Math.log(Math.E) returns 1.0
3. Math.log10(10) returns 1.0
4. Math.pow(2, 3) returns 8.0
5. Math.pow(3, 2) returns 9.0
6. Math.pow(4.5, 2.5) returns 22.91765
7. Math.sqrt(4) returns 2.0
8. Math.sqrt(10.5) returns 4.24
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 9
The Rounding Methods
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 10
Cont…
1. Math.ceil(2.1) returns 4.0
2. Math.ceil(2.0) returns 2.0
3. Math.ceil(-2.0) returns -2.0
4. Math.ceil(-2.1) returns -2.0
5. Math.floor(2.1) returns 2.0
6. Math.floor(2.0) returns 2.0
7. Math.floor(-2.0) returns –2.0
8. Math.floor(-2.1) returns -4.0
9. Math.rint(2.1) returns 2.0
10. Math.rint(-2.0) returns –2.0
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
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
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';
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
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 16
ASCII Code For Commonly Used 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
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 19
Methods in Character class
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
21
Any Question???

Introduction to Java Programming - lec 09.pptx

  • 1.
    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
  • 5.
    INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 5 Trigonometric Methods InThe Math Class
  • 6.
    INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 6 Cont… 1. Math.toDegrees(Math.PI / 2) returns 90.0 2. Math.toRadians(30) returns 0.5236 (same as π/6) 3. Math.sin(0) returns 0.0 4. Math.sin(Math.toRadians(270)) returns -1.0 5. Math.sin(Math.PI / 6) returns 0.5 6. Math.sin(Math.PI / 2) returns 1.0 7. Math.cos(0) returns 1.0 8. Math.cos(Math.PI / 6) returns 0.866 9. Math.cos(Math.PI / 2) returns 0 10. Math.asin(0.5) returns 0.523598333 (same as π/6) 11. Math.acos(0.5) returns 1.0472 (same as π/3) 12. Math.atan(1.0) returns 0.785398 (same as π/4)
  • 7.
    INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 7 Exponent Methods
  • 8.
    INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 8 Cont… 1. Math.exp(1) returns 2.71828 2. Math.log(Math.E) returns 1.0 3. Math.log10(10) returns 1.0 4. Math.pow(2, 3) returns 8.0 5. Math.pow(3, 2) returns 9.0 6. Math.pow(4.5, 2.5) returns 22.91765 7. Math.sqrt(4) returns 2.0 8. Math.sqrt(10.5) returns 4.24
  • 9.
    INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 9 The Rounding Methods
  • 10.
    INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 10 Cont… 1. Math.ceil(2.1) returns 4.0 2. Math.ceil(2.0) returns 2.0 3. Math.ceil(-2.0) returns -2.0 4. Math.ceil(-2.1) returns -2.0 5. Math.floor(2.1) returns 2.0 6. Math.floor(2.0) returns 2.0 7. Math.floor(-2.0) returns –2.0 8. Math.floor(-2.1) returns -4.0 9. Math.rint(2.1) returns 2.0 10. Math.rint(-2.0) returns –2.0
  • 11.
    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.
  • 16.
    INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 16 ASCII Code For Commonly Used Characters
  • 17.
    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
  • 19.
    INTRODUCTIONTO JAVA PROGRAMMING(10TH ED.)Y. DANIEL LIANG 19 Methods in Character class
  • 20.
    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
  • 21.