5 - Math and Strings
5 - Math 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
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Lecture Points
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:
In general,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Exercise (1)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Exercise (2)
int x = 16;
Math.________(x);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
Exercise (3)
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)
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
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';
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:
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
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:
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
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
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
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.";
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);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
33
Exercise (1)
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
35
Exercise (3)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
36
Exercise (5)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
37
Exercise (6)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
38
Exercise (7)
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
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.