Maths And Scanner Class In Java

Maths Class In Java

Maths class in java is used to perform mathematical functions like finding the maximum value, calculating power of numbers, finding absolute values, square root, trigonometric functions(cos, sin, tan).

therefore, We can import java maths class by importing import java.math.* or 

java. lang.math.

Let’s code a simple example in order to calculate the power of two numbers.

so, Let us code a simple example to calculate the absolute value of numbers.

There are many methods in Maths Class In Java like:

  1. Math.toDegrees():This function converts the given value to degrees.
  2. Math. atan(): This method thus returns the trigonometric tangent of a double value.
  3. Math. acos(): This method returns the trigonometric cosine of a double value.
  4. Math. atanh(): This method therefore returns the hyperbolic tangent of an angle r.

Enough for now, Let’s do some coding now.

  • Math. max()-:Used for getting the maximum or the biggest of two numbers.
  • Math.min()-:Used for getting the minimum or the smallest of two numbers.
  • Math.PI()-:It holds the value of pi that can be used for calculating the area and volume of the circle and cylinders.
  • Math.decrementExact()-:Used for decrementing the value by 1.
  • Math.incrementExact()-:Used for incrementing the value by 1.
  • Math.log10:-Used for finding out the logarithmic of a number when the base is 10. This method returns the base 10 algorithms of a double value.
  • Math.floorMod:-This method returns the floor modulus of the integer arguments passed to it.Therefore,floor modulus is (a-floorDiv(a,b)*b)),has the same sign as the divisor b.
  • Math.nextUp()-: This method returns the floating-point value adjacent to the parameter provided by the user.

Why Maths Class In Java?

Java math class is a built-in function in java as we can perform certain functions and methods faster and in less time. Therefore java math class helps in reducing the code length and optimize our code.

Let’s scan with Scanner class

Scanner class

Java Scanner class is used to take input from the user and is found in java.util package. To use the scanner class, first, we have to create the object of the class(Scanner).

There are different types of methods that take input of different data types.For example if we want to take input of an integer from the user then we have to use .nextInt() method similarly for string we use .next() or .nextLine().

Let us write some code for better understanding.

  • nextInt()-:This method takes an integer as an input from the user.
  • nextboolean()-:This method takes boolean values(true or false) as input from the user.
  • next()-:This method takes String as an input from the user.
  • nextLine()-:This method also takes String as an input from the user.
  • nextLong()-:This method takes a long number as input from the user.
  • nextShort()-:This method takes a short number as an input from the user.
  • nextFloat()-:This method takes a floating number as an input from the user.
  • nextbyte()-:This method takes a byte as an input from the user.

Difference Between next() and nextLine()

next() can read the input till space but nextLine() can read input including the spaces between the words. For example, if we use next() and the user enters 

“Hello World”  as input then next() will read only Hello and it won’t read World as it is separated by space. On the other hand, if we use nextLine(), it will read the whole “Hello World” including the spaces.

Conclusion

Math class and Scanner class are important aspects of java language. Math class optimizes our code by providing in-built mathematical functions hence saving our time and reducing code length. On the other hand, the java scanner class is one of the basic concepts by which we can take input from the users and provide them the desired result.

written by: Aakarsh Vats

Reviewed By: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *