Lecture – 3.
5
Array, Type conversion and Math Class in Java
Afsara Tasneem Misha
Lecturer
Department of CSE
Daffodil International University
Today’s Topics
• Array in JAVA
• Type conversion
• Math Class
2
3
Array
• Java provides a data structure, the array, which stores a fixed-size sequential collection of
elements of the same type.
• An array is used to store a collection of data, but it is often more useful to think of an array as
a collection of variables of the same type.
• Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables.
4
Declaration of an Array
• datatype [ ] arrayName;
int [ ] arr;
• dataType[ ] arrayName = {value0, value1, ..., valuek};
int [ ] arr = {10, 20, 30, 40, 50};
• datatype [ ] arrayName = new datatype [arraySize];
int [ ] arr = new int [10];
5
Array Example:1 - Two ways
public static void main(String[] args) {
public static void main(String[] args) {
int [] arr = {10,20,30,40,50};
int[] arr = {10,20,30,40,50};
for(int i : arr)
for(int i = 0; i < [Link]; i++)
{
{
[Link]("The value of array : "+arr[i]); [Link]("The value of array : "+i);
}
} }
}
6
Array Example:2 : take input from user
import [Link];
for(int i = 0; i<size; i++)
public class NewClass4 { {
[Link]("The value of array ["+i+"] : "+arr[i]);
public static void main(String args[]) { }
}
Scanner input = new Scanner([Link]); }
[Link]("Enter the size of the array: ");
int size = [Link]();
int[] arr = new int [size];
[Link]("Enter the values of the array: ");
for(int i = 0; i<size; i++)
{
arr[i] = [Link]();
} 7
Exercises on Array
1. Declare an array with N size. N will be the input from user. Store N integers in the
Array and print the elements.
2. Find out the even numbers from the array.
8
9
Type conversion
Assigning a value of one type to a variable of another type
Variable1 = (type) variable2;
If we want to convert a float value to integer :
float a = 4.99;
int a1 = (int) a;
10
Example
public static void main(String[] args) {
double d = 100.04;
int i = (int)d;//explicit type casting required
[Link]("Double value "+d);
[Link]("Integer value "+i);
}
11
12
Math Class
The [Link] class contains methods for performing basic numeric
operations such as the elementary exponential, logarithm, square root, and
trigonometric functions.
import [Link];
13
Commonly Used Methods of the Math class
Basic Math Functions Trigonometric Math Functions
• [Link](x) • [Link]
• [Link](x) • Math.E
• [Link](x)
• [Link](x,y)
• [Link](x,y)
• [Link](x)
• [Link]() –
To get a random value between 0 and e.g. 100, multiply the value
returned by [Link]() with the maximum number (e.g. 100).
Exponential and Logarithmic Math Functions
• [Link](x,y)
• [Link](x)
• [Link](x)
• [Link](x)
• Math.log10(x)
Note: For more information please visit the link.
[Link]
14
Example
import [Link]; [Link](175)=175
[Link](-184)=184
public class MathDemo { [Link](-0)=0
public static void main(String[] args) { [Link](125.9)=126.0
[Link](175, 120)=175
int x = 175; int y = -184; int z = 120;
double x1 = 125.9;
[Link]("[Link](" + x + ")=" + [Link](x));
[Link]("[Link](" + y + ")=" + [Link](y));
[Link]("[Link](" + x1 + ")=" +[Link](x));
[Link]("[Link](-0)=" + [Link](-0));
[Link]("[Link](" + x + "," + z + ")=" + [Link](x, y));
}
15
}
Exercises on Math class
1. Find absolute, floor, ceil , round and square root values of a number.
2. Find the maximum and minimum values from three numbers using MATH Class.
3. Generate 5 random numbers between 100 and 200.
4. Calculate 2^0 to 2^n Using Math Class. ‘n’ will be input from user.
5. Calculate the area of a circle. pow() method and the value of PI should be used from
Math class
16
Thank you!
17