Publicidad

Java -lec-5

IT PROFESSIONAL en Self Employed
10 de Apr de 2019
Publicidad

Más contenido relacionado

Publicidad

Java -lec-5

  1. JAVA "WRITE ONCE, RUN ANYWHERE” Lec--5 By: Zubair Khalid
  2. Java £ Java Math Class £ Java Random Number Class
  3. Java math class contains the following useful methods Java Math Class £ Power £ Absolute £ Ceil £ Floor £ Maximum £ Minimum £ Square root
  4. Power This method is used for calculating power of a number Math.pow (a, b); Number Desired power of that number
  5. Power (Example) Import java.Util.Scanner; Class c1{ Public static void main(string[] args) { system.Out.Println("please enter A number"); Scanner input=new scanner(system.In); Int number,power; Number=input.Nextint(); System.Out.Println("please enter power"); Power=input.Nextint(); System.Out.Println("power of "+number+" is "+math.Pow(number, power)); } }
  6. Absolute £ it shows the absolute value of a number. £ If we enter -ve or +ve value it will show us absolute value. Math.abs (a); Number
  7. Absolute (Example) Import java.Util.*; Class c2{ Public static void main(string[] args) { int number; Scanner scan=new scanner(system.In); System.Out.Println("please enter A number:"); Number=scan.Nextint(); System.Out.Println(math.Abs(number)); } }
  8. Ceil It rounds up the number like if we enter (7.1-7.9) the output always will be 8.0. Math.ceil (a); Number
  9. Ceil (Example) Import java.Util.Scanner; Class c3{ Public static void main(string[] args) { double number; Scanner scan=new scanner(system.In); System.Out.Println("please enter A number:"); Number=scan.Nextdouble(); System.Out.Println(math.Ceil(number)); Scan.Close(); } }
  10. Floor it rounds down the number like if we enter (7.1-7.9) the output always will be 7.0. Math.floor (a); Number
  11. Floor (Example) import java.util.Scanner; class c4{ public static void main(String[] args) { double number; Scanner scan=new Scanner(System.in); System.out.println("Please Enter A Number:"); number=scan.nextDouble(); System.out.println(Math.floor(number)); scan.close(); } }
  12. Maximum it takes two numbers and output the variable which is maximum. Math.max (a, b); Number
  13. Maximum (Example) import java.util.Scanner; class c5{ public static void main(String[] args) { double number; Scanner scan=new Scanner(System.in); System.out.println("Please Enter A Number:"); number=scan.nextDouble(); System.out.println(Math.max(1.2,5.2)); scan.close(); } }
  14. Minimum it takes two numbers and output the variable which is minimum. Math.min (a, b); Number
  15. Minimum (Example) import java.util.Scanner; class c6{ public static void main(String[] args) { double number; Scanner scan=new Scanner(System.in); System.out.println("Please Enter A Number:"); number=scan.nextDouble(); System.out.println(Math.min(1.2,5.2)); scan.close(); } }
  16. Square root it takes a number as input and output the square root of that number. Math.sqrt (a); Number
  17. Square root (Example) import java.util.Scanner; class c7{ public static void main(String[] args) { double number; Scanner scan=new Scanner(System.in); System.out.println("Please Enter A Number:"); number=scan.nextDouble(); System.out.println(Math.sqrt(number)); scan.close(); } }
  18. import java.util.Random; class c8{ public static void main(String[] args) { Random dice=new Random(); int number; for(int counter=1;counter<=10;counter++) { number=dice.nextInt(6); System.out.println(number+""); } } } Java Random Number Class Example:
Publicidad