Java Fundamentals Review -18 (Math class, arrays class and big data operations)

Source: Internet
Author: User

 Public classMathdemo { Public Static voidMain (string[] args) {Doublex=12.2; //take absolute valueSystem.out.println (Math.Abs (-99)); //Rounding upSystem.out.println (Math.ceil (x)); //Rounding downSystem.out.println (Math.floor (x)); //Take maximum valueSystem.out.println (Math.max (2,5)); //take the minimum valueSystem.out.println (Math.min (2,5)); //Take powerSystem.out.println (Math.pow (2,3)); //fetch random number [0,1]System.out.println (Math.random ()); //RoundingSystem.out.println (Math.Round (12.6)); System.out.println (Math.Round (12.5)); System.out.println (Math.Round (12.4)); //πSystem.out.println (Math.PI); }
Importjava.util.Arrays; Public classArraysdemo { Public Static voidMain (string[] args) {method2 (); }     Public Static voidmethod1 () {int[] arr={3,6,9,11,15}; //returns the index value for the specified value in an ordered array. //specified value if not present, index value = (-current index-1)        intI=arrays.binarysearch (arr, 6);    System.out.println (i); }         Public Static voidmethod2 () {//sort the array in ascending order        int[] arr={5,7,1,6,3,2,9};        Arrays.sort (arr);        System.out.println (arrays.tostring (arr));  for(intA:arr) {System.out.print (a+" "); }    }}
import Java.math.BigInteger;

Public classBigdemo { Public Static voidMain (String[]args) {BigInteger B1=NewBigInteger ("10000000000000000000000000000000000"); BigInteger B2=NewBigInteger ("20000000000000000000000000000000000"); //additionSystem.out.println (B1.add (B2)); //SubtractionSystem.out.println (B1.subtract (B2)); //multiplicationSystem.out.println (b1.multiply (B2)); //DivisionSystem.out.println (B1.divide (B2)); }}
Import Java.math.bigdecimal;public class Bigdecimaldemo {public static void main (string[] args) {method1 (); Method2 (); me Thod3 (); METHOD4 ();} public static void Method1 () {//For the purpose of avoiding loss accuracy with BigDecimal compute addition BigDecimal bd1=new BigDecimal ("0.09"); BigDecimal bd2=new BigDecimal ("0.01"); System.out.println (Bd1.add (BD2));} public static void Method2 () {//For the purpose of avoiding loss accuracy, use BigDecimal to calculate subtraction BigDecimal bd1=new BigDecimal ("1.0"); BigDecimal bd2=new BigDecimal ("0.32"); System.out.println (Bd1.subtract (BD2));} public static void Method3 () {//For the purpose of avoiding loss accuracy multiply BigDecimal bd1=new BigDecimal ("1.02") with BigDecimal; BigDecimal bd2=new BigDecimal ("0.32"); System.out.println (Bd1.multiply (BD2));} public static void Method4 () {//Calculates division BigDecimal bd1=new BigDecimal ("1.0") with BigDecimal to avoid loss accuracy; BigDecimal bd2=new BigDecimal ("0.3"); System.out.println (Bd1.divide (bd2,bigdecimal.round_floor,3));}}

Java Fundamentals Review -18 (Math class, arrays class and big data operations)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.