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

Lecture 5 Math and String Class in Java

Math and String Class in Java

Uploaded by

mwaslam2303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 5 Math and String Class in Java

Math and String Class in Java

Uploaded by

mwaslam2303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

7/5/2020

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)

The Rounding Methods

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

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

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

The Random Method


• This method generates a random double value greater than or equal to 0.0
and less than 1.0
Tests
• 0.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

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

The String Class


• The char type represents only one character. To represent a string of
characters, use the data type called String. For example, the following code
Tests "Welcome to Java".
declares message to be a string with the value
• 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
string object with contents Welcome to Java.

12

6
7/5/2020

13

Methods in the String Class

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

String message1 = “You are learning Strings”

Now combine message with message 1 using concat() method and also using +
sign.

14

7
7/5/2020

15

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 a space is pressed. For example, the following
code reads three strings from the keyboard:Tests

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

16

Reading Complete String from Console


• You can use the nextLine() method to read an entire line of text.
• The nextLine() method reads a string thatTests
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

8
7/5/2020

17

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 the string to return a
character. Tests

• 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

18

Comparing Strings

Tests

18

9
7/5/2020

19

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

20

Obtaining Substrings

Tests

String message = "Welcome to Java";


String message = message.substring(0, 11) + "HTML";
The string message now becomes Welcome to HTML.

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

Finding a Substring in a String

Tests

22

11
7/5/2020

23

Example of Finding Substrings


"Welcome to Java".indexOf('W') returns 0.
"Welcome to Java".indexOf('o') returns 4. Tests
"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

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

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:

int intValue = Integer.parseInt(intString); Tests

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 + "";

25

Thank You

26

13

You might also like