Give an examples of java.lang.math methods, JAVA Programming

Assignment Help:

Give an Examples of java.lang.Math Methods

Here is an instance program in which exercises most of the routines in java.lang.Math. If your high school math is a little rusty, don't worry if you don't notice the exact meaning of logarithms or cosines. Just know in which they're here in Java if you required them.
public class MathLibraryExample {

public static void main(String[] args) {

int i = 7;
int j = -9;
double x = 72.3;
double y = 0.34;

System.out.println("i is " + i);
System.out.println("j is " + j);
System.out.println("x is " + x);
System.out.println("y is " + y);

// The absolute value of a number is equal to
// the number if the number is positive or
// zero and equal to the negative of the number
// if the number is negative.

System.out.println("|" + i + "| is " + Math.abs(i));
System.out.println("|" + j + "| is " + Math.abs(j));
System.out.println("|" + x + "| is " + Math.abs(x));
System.out.println("|" + y + "| is " + Math.abs(y));

// Truncating and Rounding functions

// You can round off a floating point number
// to the nearest integer with round()
System.out.println(x + " is approximately " + Math.round(x));
System.out.println(y + " is approximately " + Math.round(y));

// The "ceiling" of a number is the
// smallest integer greater than or equal to
// the number. Every integer is its own
// ceiling.
System.out.println("The ceiling of " + i + " is " + Math.ceil(i));
System.out.println("The ceiling of " + j + " is " + Math.ceil(j));
System.out.println("The ceiling of " + x + " is " + Math.ceil(x));
System.out.println("The ceiling of " + y + " is " + Math.ceil(y));

// The "floor" of a number is the largest
// integer less than or equal to the number.
// Every integer is its own floor.
System.out.println("The floor of " + i + " is " + Math.floor(i));
System.out.println("The floor of " + j + " is " + Math.floor(j));
System.out.println("The floor of " + x + " is " + Math.floor(x));
System.out.println("The floor of " + y + " is " + Math.floor(y));

// Comparison operators

// min() returns the smaller of the two arguments you pass it
System.out.println("min(" + i + "," + j + ") is " + Math.min(i,j));
System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y));
System.out.println("min(" + i + "," + x + ") is " + Math.min(i,x));
System.out.println("min(" + y + "," + j + ") is " + Math.min(y,j));

// There's a corresponding max() method
// that returns the larger of two numbers
System.out.println("max(" + i + "," + j + ") is " + Math.max(i,j));
System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y));
System.out.println("max(" + i + "," + x + ") is " + Math.max(i,x));
System.out.println("max(" + y + "," + j + ") is " + Math.max(y,j));

// The Math library defines a couple
// of useful constants:
System.out.println("Pi is " + Math.PI);
System.out.println("e is " + Math.E);
// Trigonometric methods
// All arguments are given in radians

// Convert a 45 degree angle to radians
double angle = 45.0 * 2.0 * Math.PI/360.0;
System.out.println("cos(" + angle + ") is " + Math.cos(angle));
System.out.println("sin(" + angle + ") is " + Math.sin(angle));

// Inverse Trigonometric methods
// All values are returned as radians

double value = 0.707;

System.out.println("acos(" + value + ") is " + Math.acos(value));
System.out.println("asin(" + value + ") is " + Math.asin(value));
System.out.println("atan(" + value + ") is " + Math.atan(value));

// Exponential and Logarithmic Methods

// exp(a) returns e (2.71828...) raised
// to the power of a.
System.out.println("exp(1.0) is " + Math.exp(1.0));
System.out.println("exp(10.0) is " + Math.exp(10.0));
System.out.println("exp(0.0) is " + Math.exp(0.0));

// log(a) returns the natural
// logarithm (base e) of a.
System.out.println("log(1.0) is " + Math.log(1.0));
System.out.println("log(10.0) is " + Math.log(10.0));
System.out.println("log(Math.E) is " + Math.log(Math.E));

// pow(x, y) returns the x raised
// to the yth power.
System.out.println("pow(2.0, 2.0) is " + Math.pow(2.0,2.0));
System.out.println("pow(10.0, 3.5) is " + Math.pow(10.0,3.5));
System.out.println("pow(8, -1) is " + Math.pow(8,-1));

// sqrt(x) returns the square root of x.
for (i=0; i < 10; i++) {
System.out.println(
"The square root of " + i + " is " + Math.sqrt(i));
}

// Finally there's one Random method
// that returns a pseudo-random number
// between 0.0 and 1.0;

System.out.println("Here's one random number: " + Math.random());
System.out.println("Here's another random number: " + Math.random());

}

}


Related Discussions:- Give an examples of java.lang.math methods

Create a new project in eclipse , Task 1 Create a new project in Eclips...

Task 1 Create a new project in Eclipse called Assignment 1. Within this project create a package called task01. 1/ Download the class Date (you must use this class - no

Loop, write a program for loop

write a program for loop

Authentication -security component, Authentication is basically an identifi...

Authentication is basically an identification step. Functionality used for J2EE security: Principal : An entity that may be authenticated and identified. Principal n

Java , Create an object model for the Solar System using the following link...

Create an object model for the Solar System using the following link: Provide">http://airandspace.si.edu/etp/ss/index.htm Provide an abstract class called Planet and a concrete s

What is methods in java explain with an example, What is Methods in java ex...

What is Methods in java explain with an example? Data types aren't much use unless you can do things along with them. For this reasons classes have techniques. Fields say what

Assignment , hi i wanted to know if your tutors provide solutions for progr...

hi i wanted to know if your tutors provide solutions for programming assignments

Convert psd to android, Convert PSD to Android (UI only, no database) Pr...

Convert PSD to Android (UI only, no database) Project Description: I want someone highly proficient in Photoshop and Android SDK to change series of PSD files into live Andro

Vigenere Cipher decryption unknown key, I need help writing this code eithe...

I need help writing this code either in java or in python. basically the steps are described here in this youtube video or">https://www.youtube.com/watch?v=LaWp_Kq0cKs or this is

Multi treading array program, You are to write a program name arrayScaling....

You are to write a program name arrayScaling.java that will randomly generate 5000 integer number raging from 1 - 49 and place them in an array. 1.  The program will scale thi

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd