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

Lecture - 3.5-Array - Type Conversion - Math Class

The document discusses arrays, type conversion, and the Math class in Java. It defines arrays, shows how to declare and initialize them, and provides examples of using arrays. It also demonstrates type conversion in Java and provides examples. Finally, it discusses the Math class and commonly used methods for basic math, trigonometric, exponential, and logarithmic functions.

Uploaded by

d.vargas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture - 3.5-Array - Type Conversion - Math Class

The document discusses arrays, type conversion, and the Math class in Java. It defines arrays, shows how to declare and initialize them, and provides examples of using arrays. It also demonstrates type conversion in Java and provides examples. Finally, it discusses the Math class and commonly used methods for basic math, trigonometric, exponential, and logarithmic functions.

Uploaded by

d.vargas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture – 3.

5
Array, Type conversion and Math Class in Java
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 < arr.length; i++)
{
{
System.out.println("The value of array : "+i);
System.out.println("The value of array :
"+arr[i]);
} }
} }

6
Array Example:2 : take input from user
import java.util.Scanner;

for(int i = 0; i<size; i++)


public class NewClass4 { {
System.out.println("The value of array ["+i+"] : "+arr[i]);
public static void main(String args[]) { }
}
Scanner input = new Scanner(System.in);
}
System.out.println("Enter the size of the array: ");

int size = input.nextInt();

int[] arr = new int [size];

System.out.println("Enter the values of the array: ");


for(int i = 0; i<size; i++)
{
arr[i] = input.nextInt();
} 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

System.out.println("Double value "+d);


System.out.println("Integer value "+i);
}

11
12
Math Class

The java.lang.Math class contains methods for performing basic numeric


operations such as the elementary exponential, logarithm, square root, and
trigonometric functions.

import java.lang.Math;

13
Commonly Used Methods of the Math class
Basic Math Functions
• Math.abs(x) Trigonometric Math Functions
• Math.ceil(x) • Math.PI
• Math.floor(x) • Math.E
• Math.min(x,y)
• Math.max(x,y)
• Math.round(x)
• Math.random() –
To get a random value between 0 and e.g. 100, multiply the
value
returned by Math.random() with the maximum number (e.g.
100).
Exponential and Logarithmic Math Functions

• Math.pow(x,y)
• Math.sqrt(x)
• Math.exp(x)
• Math.log(x)
• Math.log10(x)

Note: For more information please visit the link.


http://tutorials.jenkov.com/java/math-operators-and-math-class.html
14
Example
import java.lang.Math; Math.abs(175)=175
Math.abs(-184)=184
public class MathDemo { Math.abs(-0)=0
public static void main(String[] args) { Math.ceil(125.9)=126.0
Math.max(175, 120)=175
int x = 175; int y = -184; int z = 120;
double x1 = 125.9;

System.out.println("Math.abs(" + x + ")=" + Math.abs(x));


System.out.println("Math.abs(" + y + ")=" + Math.abs(y));
System.out.println("Math.ceil(" + x1 + ")=" +Math.ceil(x));
System.out.println("Math.abs(-0)=" + Math.abs(-0));
System.out.println("Math.max(" + x + "," + z + ")=" + Math.max(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

You might also like