1
Math Class
• Java provides many useful methods in the Math class
for performing common mathematical
Tests functions.
• 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.
2
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)
3
Exponent Methods
Tests
4
Practice Problems
• Write a program which asks user to enter a number
calculates square root of this number. The square root
Tests
should be displayed at output.
• Write a program which asks user to enter a number
and also ask 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)
5
The Rounding Methods
Tests
6
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
7
Min, Max and Abs Methods
• The min and max methods return the minimum and
maximum numbers of two numbers (int, long, float, or
double). Tests
• For example, max(4.4, 5.0) returns 5.0, and min(3, 2)
returns 2.
• The abs method returns the absolute value of the
number (int, long, float, or double).
• For example,
Math.max(2, 3) returns 3
Math.max(2.5, 4) returns 4.0
Math.min(2.5, 4.6) returns 2.5
Math.abs(-2) returns 2
Math.abs(-2.1) returns 2.1
8
Practice Problem
• Write a program which asks user to enter two
numbers (one by one) and prints which number is
Tests class.
bigger out of the two using Math
9
The Random Method
• This method generates a random double value
greater than or equal to 0.0 and less than 1.0
Tests
• 0 <= Math.random() <1.0).
• You can use it to write a simple expression to
generate random numbers in any range.
• (int)(Math.random() * 10) Returns a random integer
between 0 and 9.
• 50 + (int)(Math.random() * 50) Returns a random
integer between 50 and 99.
10
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 Tests
the guess is correct it
should print that you have guessed right otherwise it
should print that your guess is wrong and should also
print both numbers.
11
The String Class
• The char type represents only one character. To
represent a string of characters, use the data type
called String. For example, Tests the following code
declares message to be a string with the value
"Welcome to Java".
• String message = "Welcome to Java";
• String is a predefined class in the Java library, just
like the classes System and Scanner.
• The String type is not a primitive type. It is known as
a reference type.
• Any Java class can be used as a reference type for a
variable.
• The variable declared by a reference type is known as
a reference variable that references an object. Here,
message is a reference variable that references a
12
Methods in the String Class
Tests
13
Practice Problem
Write a program which declares the following string
String message = "Welcome toTests
Java";
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
String message1 = “You are learning Strings”
Now combine message with message 1 using concat()
method and also using + sign.
14
Reading One Word as String from Console
To read one string from the console, invoke the next()
method on a Scanner object. A string finishes whenever
Tests
a space is pressed. For example, the following code
reads three strings from the keyboard:
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);
15
Reading Complete String from Console
• You can use the nextLine() method to read an entire
line of text.
Tests
• The nextLine() method reads a string that ends with
the Enter key pressed. For example, the following
statements read a line of text.
Scanner input = new Scanner(System.in);
System.out.println("Enter a line: ");
String s = input.nextLine();
System.out.println("The line entered is " + s);
16
Reading Character from Console
• To read a character from the console, use the
nextLine() method to read a string and then invoke
the charAt(0) method on Tests
the string to return a
character.
• For example, the following code reads a character
from the keyboard:
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);
17
Comparing Strings
Tests
18
Comparing Strings
Declare two strings
String s1 = "Welcome to Java";Tests
String s2 = “welcome to java";
Write a program which compares s1 and s2 and prints
results using first two methods given on previous slide.
19
Obtaining Substrings
Tests
String message = "Welcome to Java";
String message = message.substring(0, 11) + "HTML";
The string message now becomes Welcome to HTML.
20
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
Finding a Substring in a String
Tests
22
Example of Finding Substrings
"Welcome to Java".indexOf('W') returns 0.
"Welcome to Java".indexOf('o')Tests
returns 4.
"Welcome to Java".indexOf('o', 5) returns 9.
"Welcome to Java".indexOf("come") returns 3.
"Welcome to Java".indexOf("Java", 5) returns 11.
"Welcome to Java".indexOf("java", 5) returns -1.
23
Practice Problem
Write a program which asks user to enter First and
Surname and stores it in one string. Use substring
Tests
methods to divide the First and Surname.
Hint: Find the index of space (“ ”) and select
substrings before and after that index.
24
Conversion between Strings and Numbers
You can convert a numeric string into a number. To convert a
string into an int value, use the Integer.parseInt method, as
follows:
Tests
int intValue = Integer.parseInt(intString);
where intString is a numeric string such as "123".
To convert a string into a double value, use the
Double.parseDouble method, as follows:
double doubleValue = Double.parseDouble(doubleString);
where doubleString is a numeric string such as "123.45".
If the string is not a numeric string, the conversion would cause a
runtime error.
You can convert a number into a string, simply use the string
concatenating operator as follows:
String s = number + "";
Thank You