0% found this document useful (0 votes)
5 views

5 - Math and Strings

Chapter 4 covers mathematical functions, character data types, and string manipulation in Java. It discusses the Math class, random number generation, string methods, and conversions between strings and numbers. The chapter also includes exercises for practical understanding of these concepts.

Uploaded by

3slm3al
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

5 - Math and Strings

Chapter 4 covers mathematical functions, character data types, and string manipulation in Java. It discusses the Math class, random number generation, string methods, and conversions between strings and numbers. The chapter also includes exercises for practical understanding of these concepts.

Uploaded by

3slm3al
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Chapter 4 Mathematical Functions,

Characters, and Strings

Modified from:
-W3Schools.com
-Introduction to Java Programming, Liang, 10 th Edition

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1
Introduction to the lecture

In this lecture will talk how deal with The


Mathematical Functions, The Math Class, The
Random Method, Character Data Type, Casting
between char and Numeric Types, The String Type,
Simple Methods for String Objects, Getting String
Length, Getting Characters from a String , Finding a
Character in a String, Converting Strings and String
Concatenation, Adding Numbers and Strings ,
Reading a String from the Console , Reading a
Character from the Console, Special Characters,
Conversion between Strings and Numbers in JAVA
programming.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Lecture Points

 The Mathematical Functions


 The Math Class
 The Random Method
 Character Data Type
 Casting between char and Numeric Types
 The String Type
 Simple Methods for String Objects
 Getting String Length
 Getting Characters from a String
 Finding a Character in a String
 Converting Strings and String Concatenation
 Adding Numbers and Strings
 Reading a String from the Console
 Reading a Character from the Console
 Special Characters
 Conversion between Strings and Numbers
 Some Exercise

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
3
Mathematical Functions
The Java Math class has many methods that
allows you to perform mathematical tasks on
numbers.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
4
The Math Class
 pow(double a, double b) Examples:
Returns a raised to the power of b.
Math.pow(2, 3) returns 8.0
 sqrt(double a) Math.pow(3, 2) returns 9.0
Math.pow(3.5, 2.5) returns
Returns the square root of a.
22.91765
Math.sqrt(4) returns 2.0
Math.sqrt(10.5) returns 3.24

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
min, max, and abs
1.The Math.max(x,y) method Examples:
can be used to find the highest
(maximum) value of x and y. Math.max(5,10) returns 10
Math.max(2.5, 3) returns
2.The Math.min(x,y) method 3.0
can be used to find the lowest Math.min(2.5, 3.6)
(minimum) value of x and y. returns 2.5
Math.abs(-2) returns 2
3.The Math.abs(x) method Math.abs(-2.1) returns
returns the absolute (positive) 2.1
value of x

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
The random Method
Generates a random double value greater than or equal to 0.0 and less than
1.0 (0 <= Math.random() < 1.0).
Math.random() returns a random number between 0.0 (inclusive), and 1.0
(exclusive)

Example:

Math.random();

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
The random Method
To get more control over the random number, e.g. you only want a random
number between 0 and 100, you can use the following formula:
int randomNum = (int)(Math.random() * 101);

Examples:

Returns a random integer


(int)(Math.random() * 10)
between 0 and 9.

50 + (int)(Math.random() * 50) Returns a random integer


between 50 and 99.

In general,

a + Math.random() * b Returns a random number between


a and a + b, excluding a + b.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Exercise (1)

Use the correct method to find the highest


value of x and y.
int x = 5;
int y = 10;
Math._______(x, y);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Exercise (2)

Use the correct method to find the square root of x.

int x = 16;
Math.________(x);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
Exercise (3)

Use the correct method to return a random number


between 0 (inclusive), and 1 (exclusive).

Math._______ ;

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
Character Data Type
char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)

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);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
12
ASCII Code for Commonly Used
Characters
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

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
Casting between char and
Numeric Types
int i = 'a'; // Same as int i = (int)'a';

char c = 97; // Same as char c = (char)97;

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
14
The String Type
 The char type only represents one character. To represent a
string of characters, use the data type called String.
 Strings are used for storing text.
 A String variable contains a collection of characters
surrounded by double quotes
 For example,
String message = "Welcome to Java";

String is actually a predefined class in the Java library just like the
System class and Scanner class.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15
The String Type
 Example:

Create a variable of type String and assign it a


value:

Ans:
String greeting = "Hello";

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
Simple Methods for String Objects
Method Description

length() Returns the number of characters in this string.


charAt(index) Returns the character at the specified index from this string.
concat(s1) Returns a new string that concatenates this string with string s1.
toUpperCase() Returns a new string with all letters in uppercase.
toLowerCase() Returns a new string with all letters in lowercase.
trim() Returns a new string with whitespace characters trimmed on both sides.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
17
Getting String Length
 A String in Java is actually an object, which contain methods
that can perform certain operations on strings. For example,
the length of a string can be found with the length() method:
 Example1:

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


System.out.println("The length of the txt string is: " + txt.length());

 Example2:
String message = "Welcome to Java";
System.out.println("The length of " + message + " is "
+ message.length());

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
18
Getting Characters from a String

String message = "Welcome to Java";


System.out.println("The first character in message is "
+ message.charAt(0));

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
Finding a Character in a String
 The indexOf() method returns the index (the
position) of the first occurrence of a specified text
in a string (including whitespace):
 Example
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
 Remember: Java counts positions from zero.
0 is the first position in a string, 1 is the second, 2
is the third ..

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
20
Converting Strings
"Welcome".toLowerCase() returns a new string, welcome.
"Welcome".toUpperCase() returns a new string,
WELCOME.

Example
String txt = "Hello World";
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
String Concatenation

 The + operator can be used between strings to combine them. This


is called concatenation.

 Example
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);

Note that we have added an empty text (" ") to create a space
between firstName and lastName on print.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved. 22
String Concatenation

 You can also use the concat() method to concatenate two strings:

 Example
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved. 23
String Concatenation Examples

String s3 = s1.concat(s2); or String s3 = s1 + s2;

// Three strings are concatenated


String message = "Welcome " + "to " + "Java";

// String Supplement is concatenated with character B


String s1 = "Supplement" + 'B'; // s1 becomes SupplementB

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved. 24
Adding Numbers and Strings
 WARNING!
– Java uses the + operator for both addition and concatenation.
– Numbers are added. Strings are concatenated.
 If you add two numbers, the result will be a number:

 Example
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer/number)

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
25
Adding Numbers and Strings
 If you add two strings, the result will be a
string concatenation:
 Example

String x = "10";
String y = "20";
String z = x + y; // z will be 1020 (a String)

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
26
Adding Numbers and Strings
 Ifyou add a number and a string, the result
will be a string concatenation:
 Example

String x = "10";
int y = 20;
String z = x + y; // z will be 1020 (a String)

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
27
Reading a String from the Console
Scanner input = new Scanner(System.in);
System.out.print("Enter three words separated by spaces: ");
String s1 = input.next();
String s2 = input.next();
String s3 = input.next();
System.out.println("s1 is " + s1);
System.out.println("s2 is " + s2);
System.out.println("s3 is " + s3);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
28
Reading a Character from the
Console
Scanner input = new Scanner(System.in);
System.out.print("Enter a character: ");
String s = input.nextLine();
char ch = s.charAt(0);
System.out.println("The character entered is " + ch);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
29
Special Characters
 Because strings must be written within quotes, Java will
misunderstand this string, and generate an error:
 Example:
String txt = "We are the so-called "Vikings" from the north.";
 The solution to avoid this problem, is to use the backslash
escape character.
 The backslash (\) escape character turns special characters
into string characters

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
30
Special Characters
 The sequence \" inserts a double quote in a string:
 Example
String txt = "We are the so-called \"Vikings\" from the north.";

 The sequence \' inserts a single quote in a string:


 Example
String txt = "It\'s alright.";

 The sequence \\ inserts a single backslash in a string:


 Example
String txt = "The character \\ is called backslash.";

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
31
Escape Sequences for Special Characters

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
32
Conversion between Strings and
Numbers
int intValue = Integer.parseInt(intString);
double doubleValue = Double.parseDouble(doubleString);

String s = number + "";

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
33
Exercise (1)

Fill in the missing part to create a greeting variable of


type String and assign it the value Hello.

___________ greeting = __________ ;

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
34
Exercise (2)

Use the correct method to print the length of the txt string.

String txt = "Hello";


System.out.println(______.________);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
35
Exercise (3)

Convert the value of txt to upper case.

String txt = "Hello";


System.out.println(____.____);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
36
Exercise (5)

Use the correct method to concatenate two strings:

String firstName = "John ";


String lastName = "Doe";
System.out.println(firstName.____________(lastName));

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
37
Exercise (6)

Use the correct operator to concatenate two strings:

String firstName = "John ";


String lastName = "Doe";
System.out.println(firstName ___lastName);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
38
Exercise (7)

Return the index (position) of the first occurrence of "e" in


the following string:

String txt = "Hello Everybody";


System.out.println(txt.____________(____));

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
39
Summary of lecture
In this lecture, learned about some elements in JAVA
programming that are included :
The Mathematical Functions
The Math Class
The Random Method
Character Data Type
Casting between char and Numeric Types
The String Type
Simple Methods for String Objects
Getting String Length
Getting Characters from a String
Finding a Character in a String
Converting Strings and String Concatenation
Adding Numbers and Strings
Reading a String from the Console
Reading a Character from the Console
Special Characters
Conversion between Strings and Numbers
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
References

 Y. Daniel Liang, 2019, Intro to Java Programming,


Comprehensive Version, Student Value Edition 12th Edition.
Pearson, ISBN-10 : 0136520154 ISBN-13: 978-0136520153.

 Introduction to Java,
https://www.w3schools.com/java/java_intro.asp, Last Updated
2024, Last Accessed March 2024.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.

You might also like