النتائج 1 إلى 3 من 3

الموضوع: Examples of java.lang.Math Methods + source code

  1. #1
    elfr3on el3ashk الصورة الرمزية A7med Baraka
    تاريخ التسجيل
    Jun 2008
    الدولة
    Egypt - Cairo
    المشاركات
    4,695
    معدل تقييم المستوى
    10

    افتراضي Examples of java.lang.Math Methods + source code


    End Google Ads 201810 - BS.net 01 -->
    Here is an example program that 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 remember the exact meaning of logarithms or cosines. Just know that they're here in Java if you need 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());
    
      }
    
    }


  • #2
    elfr3on el3ashk الصورة الرمزية A7med Baraka
    تاريخ التسجيل
    Jun 2008
    الدولة
    Egypt - Cairo
    المشاركات
    4,695
    معدل تقييم المستوى
    10

    افتراضي رد: Examples of java.lang.Math Methods + source code

    Here's the output from the math library example


    i is 7
    j is -9
    x is 72.3
    y is 0.34
    |7| is 7
    |-9| is 9
    |72.3| is 72.3
    |0.34| is 0.34
    72.3 is approximately 72
    0.34 is approximately 0
    The ceiling of 7 is 7
    The ceiling of -9 is -9
    The ceiling of 72.3 is 73
    The ceiling of 0.34 is 1
    The floor of 7 is 7
    The floor of -9 is -9
    The floor of 72.3 is 72
    The floor of 0.34 is 0
    min(7,-9) is -9
    min(72.3,0.34) is 0.34
    min(7,72.3) is 7
    min(0.34,-9) is -9
    max(7,-9) is 7
    max(72.3,0.34) is 72.3
    max(7,72.3) is 72.3
    max(0.34,-9) is 0.34
    Pi is 3.14159
    e is 2.71828
    cos(0.785398) is 0.707107
    sin(0.785398) is 0.707107
    acos(0.707) is 0.785549
    asin(0.707) is 0.785247
    atan(0.707) is 0.615409
    exp(1.0) is 2.71828
    exp(10.0) is 22026.5
    exp(0.0) is 1
    log(1.0) is 0
    log(10.0) is 2.30259
    log(Math.E) is 1
    pow(2.0, 2.0) is 4
    pow(10.0, 3.5) is 3162.28
    pow(8, -1) is 0.125
    The square root of 0 is 0
    The square root of 1 is 1
    The square root of 2 is 1.41421
    The square root of 3 is 1.73205
    The square root of 4 is 2
    The square root of 5 is 2.23607
    The square root of 6 is 2.44949
    The square root of 7 is 2.64575
    The square root of 8 is 2.82843
    The square root of 9 is 3
    Here's one random number: 0.820582
    Here's another random number: 0.866157


  • #3
    تقنى جديد
    تاريخ التسجيل
    Aug 2009
    المشاركات
    6
    معدل تقييم المستوى
    0

    افتراضي رد: Examples of java.lang.Math Methods + source code


  • معلومات الموضوع

    الأعضاء الذين يشاهدون هذا الموضوع

    الذين يشاهدون الموضوع الآن: 1 (0 من الأعضاء و 1 زائر)

    المواضيع المتشابهه

    1. snake game in java source code
      بواسطة A7med Baraka في المنتدى Java - جافا
      مشاركات: 0
      آخر مشاركة: 05-08-2009, 03:57 AM
    2. Convert Binary to Hexadecimal source code in java
      بواسطة A7med Baraka في المنتدى Java - جافا
      مشاركات: 0
      آخر مشاركة: 04-16-2009, 03:32 AM
    3. Compiling and Running a Java Program Using the Command Line with examples
      بواسطة BarakaSoft في المنتدى Java - جافا
      مشاركات: 0
      آخر مشاركة: 03-30-2009, 12:17 AM
    4. Rotor: Shared Source CLI Provides Source Code for a FreeBSD Implementation of .NET
      بواسطة C++ Programming في المنتدى MSDN C++ Lessons
      مشاركات: 0
      آخر مشاركة: 03-29-2009, 02:42 AM
    5. Convert Hexadecimal into Binary and Long in java - source code
      بواسطة A7med Baraka في المنتدى Java - جافا
      مشاركات: 1
      آخر مشاركة: 03-25-2009, 06:47 PM

    الكلمات الدلالية لهذا الموضوع

    مواقع النشر (المفضلة)

    ضوابط المشاركة

    • لا تستطيع إضافة مواضيع جديدة
    • لا تستطيع الرد على المواضيع
    • لا تستطيع إرفاق ملفات
    • لا تستطيع تعديل مشاركاتك
    •  
    "وَقُل رَّبِّ زِدْنِي عِلْمًا"
    أعلانات نصية أستضافة , ريسيلر - Best Hosting | BarakaSoft Web Solutions

    BarakaSoft PageRank RSS RSS 2.0 XML MAP HTML 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 C/C++ | Java | C# | Network | Database | OS | Linux | Windows | Hacker & Security | Photoshop | Flash | Web Development | Free Programs | Mobile App | Free Java Course | Latest Technical News | Internet Programs | Antiviurse Programs | Graphics Programs | Network Programs | Portable Programs | vb Forums Development | Forums Development | CMS(Joomla-nuke-wordpress-mkportal...) | Photo | Anime |