Lecture 5 Math and String Class in Java
Lecture 5 Math and String Class in Java
IN2203
Fundamentals of
Programming (Java)
Instructor
Dr. Muhammad Waqar
[email protected]
Math Class
• Java provides many useful methods in the Math class for performing common
mathematical functions. Tests
• A method is a group of statements that performs a specific task.
• Java provides many mathematical functions in Match class e.g. pow(a, b)
method to compute a^b, the random() method for generating a random
numbers.
• This lecture introduces useful methods in the Math class which can mainly be
categorized as trigonometric methods, exponent methods, and service
methods.
• Math class is automatically imported into Java and you do not need to
explicitly import it.
1
7/5/2020
Trigonometric Methods
Tests
• All methods of Math class can be used by appending Math. before method
name e.g. Math. sin(radians), Math.toRadians(degree)
Exponent Methods
Tests
2
7/5/2020
Practice Problems
• Write a program which asks user to enter a number and calculates square root
of this number. The square root should be displayed at output.
Tests
• Write a program which asks user to enter a number and also asks user which
power of this number he/she wants to calculate. The required power of
number should be calculated and displayed at output. (Hint: use
Math.pow(a,b)
Tests
3
7/5/2020
Practice Problems
• Write a program which asks user to enter radius of a circle and calculates its
area. The area must be rounded to nearest integer.
Tests
4
7/5/2020
Practice Problem
• Write a program which asks user to enter two numbers (one by one) and
prints which number is bigger out of the two using Math class.
Tests
10
10
5
7/5/2020
11
Practice Problem
• Write a program which generates output of a dice randomly (a number
between 1 and 6). Asks user to guess that number and if the guess is correct it
should print that you have guessed rightTests
otherwise it should print that your
guess is wrong and should also print both numbers.
11
12
12
6
7/5/2020
13
Tests
13
14
Practice Problem
Write a program which declares the following string
String message = "Welcome to Java"; Tests
Use length() method to print the length of string. Then print the character at
index 11 using charAt() method. Convert this string to upper and lower case
using toUpperCase() and to lowercase() methods. Declare another string
Now combine message with message 1 using concat() method and also using +
sign.
14
7
7/5/2020
15
15
16
16
8
7/5/2020
17
• For example, the following code reads a character from the keyboard:
17
18
Comparing Strings
Tests
18
9
7/5/2020
19
Comparing Strings
Declare two strings
Write a program which compares s1 and s2 and prints results using first two
methods given on previous slide.
19
20
Obtaining Substrings
Tests
20
10
7/5/2020
21
Practice Problem
Write a Java program which print the following statement.
Tests
“The quick brown fox jumps over the lazy dog.”
Ask the user to specify two integer indexes. Tell the user that the second index
cannot be greater than the length of the string and then extract substring for
those indexes and print that substring.
21
22
Tests
22
11
7/5/2020
23
23
24
Practice Problem
Write a program which asks user to enter First and Surname and stores it in
one string. Use substring methods to divide the First and Surname.
Tests
Hint: Find the index of space (“ ”) and select substrings before and after that
index.
24
12
7/5/2020
25
25
Thank You
26
13