You are on page 1of 9

Object Oriented

Programming with
Java(CT405-N)
Prof. Vimal Bhatt , Assistant
Professor
Computer Science & Engineering
Vidush Somany Institute of Technology & Research,Kadi
Chapter 1
Introduction to Java
Agenda 01 Basic of Java Programming

Style 02 Data types ,Variables, operators

03 Control Structures

Looping, Methods,overloading,
04

05 Math class, Array in Java


Java Math class & Array
What is Math class? public class JavaMathExample1
 Java Math class provides several methods to work on {
math calculations like min(), max(), avg(), sin(), cos(), public static void main(String[] args).
tan(), round(), ceil(), floor(), abs() etc. {
double x = 28;
double y = 4;
// return the maximum of two numbers
System.out.println("Maximum number of x and y is: " +Math.max(x, y));
// return the square root of y
System.out.println("Square root of y is: " + Math.sqrt(y));
//returns 28 power of 4 i.e. 28*28*28*28
System.out.println("Power of x and y is: " + Math.pow(x, y));
// return the logarithm of given value
System.out.println("Logarithm of x is: " + Math.log(x));
System.out.println("Logarithm of y is: " + Math.log(y));
// return the logarithm of given value when base is 10
System.out.println("log10 of x is: " + Math.log10(x));
System.out.println("log10 of y is: " + Math.log10(y));
// return the log of x + 1
System.out.println("log1p of x is: " +Math.log1p(x));
// return a power of 2
System.out.println("exp of a is: " +Math.exp(x));
// return (a power of 2)-1
System.out.println("expm1 of a is: " +Math.expm1(x));
}
}
Java Math class & Array
What is Math class?
 Java Math class provides several methods to work on // return the trigonometric arc tangent value of a
math calculations like min(), max(), avg(), sin(), cos(), System.out.println("Tangent value. of a is: " +Math.atan(a));
tan(), round(), ceil(), floor(), abs() etc.
// return the hyperbolic sine of a
public class JavaMathExample2 System.out.println("Sine value of a is: " +Math.sinh(a));
{
public static void main(String[] args) // return the hyperbolic cosine value of a
{ System.out.println("Cosine value of a is: " +Math.cosh(a));
double a = 30;
// return the hyperbolic tangent value of a
// converting values to radian System.out.println("Tangent value of a is: " +Math.tanh(a));
double b = Math.toRadians(a); }
}
// return the trigonometric sine of a
System.out.println("Sine value of a is: " +Math.sin(a));

// return the trigonometric cosine value of a


System.out.println("Cosine value of a is: " +Math.cos(a));

// return the trigonometric tangent value of a


System.out.println("Tangent value of a is: " +Math.tan(a));

// return the trigonometric arc sine of a


System.out.println("Sine value of a is: " +Math.asin(a));

// return the trigonometric arc cosine value of a


System.out.println("Cosine value of a is: " +Math.acos(a));
Array in java
Array
 Java array is an object which contains elements of a similar data
type. Additionally, The elements of an array are stored in a
contiguous memory location. It is a data structure where we store
similar elements. We can store only a fixed set of elements in a
Java array.

Advantages
 Code Optimization: It makes the code optimized, we can retrieve or sort
the data efficiently.
 Random access: We can get any data located at an index position.
Disadvantages
 Size Limit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.
Array in java
Array Instantiation of an Array in Java
 Java array is an object which contains elements of a similar data  arrayRefVar=new datatype[size];
type. Additionally, The elements of an array are stored in a
contiguous memory location. It is a data structure where we store
similar elements. We can store only a fixed set of elements in a class Testarray{
Java array.
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
Advantages
a[0]=10;//initialization
 Code Optimization: It makes the code optimized, we can retrieve or sort a[1]=20;
the data efficiently. a[2]=70;
 Random access: We can get any data located at an index position. User-defined Method
Disadvantages a[3]=40;
 Size Limit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection
a[4]=50;
framework is used in Java which grows automatically. //traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Array in java
Array
class GFG
{
public static void main (String[] args) class
{ Element at index 0 : 10
// declares an Array of integers. Element at index 1 : 20
int[] arr;
Element at index 2 : 30
// allocating memory for 5 integers.
Advantages
arr = new int[5];
Element at index 3 : 40
 Code Optimization: It makes the code optimized, we can retrieve or sort
the//
data efficiently. Element at index 4 : 50
initialize the first elements of the array
 Random
arr[0]access:
= 10; We can get any data located at an index position.
Disadvantages
 Size
//Limit: We canthe
initialize store only the
second fixed size
elements of ofthe
elements
array in the array. It
doesn't grow
arr[1] its size at runtime. To solve this problem, collection
= 20;
framework is used in Java which grows automatically.
//so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;

// accessing the elements of the specified array


for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i + " : "+
arr[i]);
}
}
THANK YOU

You might also like