Thursday, March 31, 2016

Math Class Methods

Welcome back, after the loops we're heading to Math Class Methods...

OK, what is this Math Class Methods??They're Advanced mathematics functions(methods called in Java).

I don't wanna confuse you so let's go for the coding...

Absolute Value :-


public class main {

public static void main(String []args){
System.out.println(Math.abs(-18.75));
}
}
  • Give the Absolute value.

Round :-


public class main {

public static void main(String []args){
System.out.println(Math.ceil(5.6));
}
}
  • But guys this isn't accurate enough. "ceil" will round to the nearest greater number.
  • Use "floor" instead "ceil". And it'll round to nearest lower number.

Max & Min :-


public class main {

public static void main(String []args){
System.out.println(Math.max(5, 16, 24, 21));
}
}
  • Use "min" instead "max".

Power :-


public class main {

public static void main(String []args){
System.out.println(Math.pow(2, 5));
}
}
  • First number is the base & second one is the power.

Square :-


public class main {

public static void main(String []args){
System.out.println(Math.sqrt(9));
}
}






Using Math Class Methods you can easily calculate advanced mathematical operations.

No comments:

Post a Comment