Java has many built-in classes to work with numbers. The uses of three useful java classes have been shown in this tutorial to know the uses of numbers in Java. These classes are Math, Random, and GregorialCalendar.
Example-1: Use of Math class
Math class has many built-in methods to perform different types of arithmetic operations. Create a java file with the following code to know the uses of some commonly used methods of the Math class.
import java.util.*;
import java.lang.Math;
public class JavaNumber1 {
public static void main(String[] args) {
int number1, number2;
double number3, sqrResult, maxNum, roundVal, sqrtResult;
//Define scanner object
Scanner scanner = new Scanner(System.in);
//Get three input values
System.out.print("Enter the first number:");
number1 = scanner.nextInt();
System.out.print("Enter the second number:");
number2 = scanner.nextInt();
System.out.print("Enter the third number:");
number3 = scanner.nextDouble();
//Find the square value
sqrResult = Math.pow(number1, number2);
//Find the square root value
sqrtResult = Math.sqrt(number1);
//Find the maximum value
maxNum = Math.max(number1, number2);
//Find the round value
roundVal = Math.round(number3);
//Print the generated values
System.out.println(number1 + " to the power " + number2 + " is " + sqrResult);
System.out.println("The square root of " + number1 + " is " + sqrtResult);
System.out.println("The maximum value between " + number1 + " and " + number2 + " is " + maxNum);
System.out.print("The round value of " + number3 + " is " + roundVal);
}
}
Output:
Example-2: Use of Random class
Random class is used in Java to generate different types of random numbers. Create a Java file with the following code where the use of integer random number has been shown.
import java.util.*;
public class JavaNumber2 {
public static void main(String[] args) {
int minNum, maxNum, newNum;
//Create a scanner object
Scanner scan = new Scanner(System.in);
//Create a random object
Random random = new Random();
//Get two input values
System.out.print("Enter the minimum number: ");
minNum = scan.nextInt ();
System.out.print("Enter the maximum number: ");
maxNum = scan.nextInt();
//Generate a number using random method and the minimum number
newNum = random.nextInt(maxNum-minNum+1) + minNum;
//Print the generated number
System.out.println("\nThe generated number is " + newNum);
}
}
Output:
Example-3: Use of GregorianCalendar
GregorianCalendar class is used to perform different types of date and time related tasks. Create a Java file with the following code to know the basic uses of this class.
import java.util.*;
public class JavaNumber3 {
public static void main(String[] args) {
//Define necessary variables
int month, hr, min, sec;
//Create calendar object
GregorianCalendar today = new GregorianCalendar();
//Add 1 with the month value to start counting value from 1
month = today.get(Calendar.MONTH) + 1;
//Add 12 with the hour value to display hour value in 24 format
hr = today.get(Calendar.HOUR) + 12;
//Read minute value
min = today.get(Calendar.MINUTE);
//Read second value
sec = today.get(Calendar.SECOND);
//Print the current date and time
System.out.println("Current date and time i " + today.getTime());
//Print the current date
System.out.println("Today's date is : " + today.get(Calendar.DATE) + "-" + month + "-" + today.get(Calendar.YEAR));
//Print the current time
System.out.println("Current time is : " + hr + ":" + min + ":" + sec);
}
}
Output:
Exercise:
Create a Java class to generate a random number within 1 to 99. Next, concatenate the current date and time with the generated random number in the following format and print the concatenated value.
Format: dd-mm-yy-randomNumber