Menggunakan labrary matematika pada Java - Java - Publisher - Royz Rambo
Ilham Penta
Site menu
Section categories
My articles [6]
Java [14]
Java Programming
Flash [6]
Flash
URL web dan Tutorial [29]
Petualangan [1]
Our poll
Rate my site
Total of answers: 19
Statistics

Total online: 2
Guests: 2
Users: 0
Login form
Main » Articles » Java

Menggunakan labrary matematika pada Java
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());

  }
}

OUTPUT

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.0
The ceiling of -9 is -9.0
The ceiling of 72.3 is 73.0
The ceiling of 0.34 is 1.0
The floor of 7 is 7.0
The floor of -9 is -9.0
The floor of 72.3 is 72.0
The floor of 0.34 is 0.0
min(7,-9) is -9
min(72.3,0.34) is 0.34
min(7,72.3) is 7.0
min(0.34,-9) is -9.0
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.141592653589793
e is 2.718281828459045
cos(0.7853981633974483) is 0.7071067811865476
sin(0.7853981633974483) is 0.7071067811865475
acos(0.707) is 0.7855491633997437
asin(0.707) is 0.785247163395153
atan(0.707) is 0.6154085176292563
exp(1.0) is 2.7182818284590455
exp(10.0) is 22026.465794806718
exp(0.0) is 1.0
log(1.0) is 0.0
log(10.0) is 2.302585092994046
log(Math.E) is 1.0
pow(2.0, 2.0) is 4.0
pow(10.0, 3.5) is 3162.2776601683795
pow(8, -1) is 0.125
The square root of 0 is 0.0
The square root of 1 is 1.0
The square root of 2 is 1.4142135623730951
The square root of 3 is 1.7320508075688772
The square root of 4 is 2.0
The square root of 5 is 2.23606797749979
The square root of 6 is 2.449489742783178
The square root of 7 is 2.6457513110645907
The square root of 8 is 2.8284271247461903
The square root of 9 is 3.0
Here's one random number: 0.7496372343000587
Here's another random number: 0.0943150671565458


//Rounding JDK 1.1

import java.math.*;

/**
 *
 * @author pentax
 */
public class TestRound11 {

    public static void main(String args[]) {
        double d = 3.1537;
        BigDecimal bd = new BigDecimal(d);
        bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        // output is 3.15
        System.out.println(d + " : " + round(d, 2));
        // output is 3.154
        System.out.println(d + " : " + round(d, 3));
    }

    public static double round(double d, int decimalPlace) {
        // see the Javadoc about why we use a String in the constructor
        // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }
}

OUTPUT
3.1537 : 3.15
3.1537 : 3.154

JDK 1.0.2
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author pentax
 */
public class TestRound102 {
    public static void main(String args[]){
      double d = 3.1537;
      // output is 3.2
      System.out.println(d + " : " + round(d, 1));
      // output is 3.15
      System.out.println(d + " : " + round(d, 2));
      // output is 3.154
      System.out.println(d + " : " + round(d, 3));
      d= 9.155;
      // output is 9.2
      System.out.println(d + " : " + round(d, 1));
      // output is 9.16
      System.out.println(d + " : " + round(d, 2));
      // output is 3.155
      System.out.println(d + " : " + round(d, 3));
      d= 1234.156789;
      // output is 1234.2
      System.out.println(d + " : " + round(d, 1));
      // output is 1234.16
      System.out.println(d + " : " + round(d, 2));
      // output is 1234.157
      System.out.println(d + " : " + round(d, 3));
      // output is 1234.1568
      System.out.println(d + " : " + round(d, 4));
      d= 1.5;
      // output is 2
      System.out.println(d + " : " + round(d, 0));
    }

    // positive value only.
    public static double round(double value, int decimalPlace)
    {
      double power_of_ten = 1;
      // floating point arithmetic can be very tricky.
      // that's why I introduce a "fudge factor"
      double fudge_factor = 0.05;
      while (decimalPlace-- > 0) {
         power_of_ten *= 10.0d;
         fudge_factor /= 10.0d;
      }
      return Math.round((value + fudge_factor)* power_of_ten)  / power_of_ten;
    }
}

OUTPUT

3.1537 : 3.2
3.1537 : 3.15
3.1537 : 3.154
9.155 : 9.2
9.155 : 9.16
9.155 : 9.155
1234.156789 : 1234.2
1234.156789 : 1234.16
1234.156789 : 1234.157
1234.156789 : 1234.1568
1.5 : 2.0





Source: http://www.cafeaulait.org/course/week4/40.html
Category: Java | Added by: penta (2010-10-26)
Views: 13190 | Comments: 27 | Tags: matematika, java, Labrary | Rating: 0.0/0
Total comments: 271 2 3 »
27  
web-site is not at all indexed in search engines like google
http://balongastricocolombia.com/node/24291/
http://www.telanganafb.com/index.php?do=/profile-92542/info/
http://iclarks.org/activity/p/55030/

26  
blog-is-the-new-black blog
http://www.tajblog.com/hs/profiles/110729
http://www.121swingers.com/LanoraC71
http://2009.desurasur.org/content/prompt-remedies-designed-parapharmacie-easy-help
http://moviesbyte.com/groups/just-as-important-practical-help-connected-with-parapharmacie/
http://vodaface.com/blog/view/150863/analyzed-is-plans-and-blueprints-involved-with-parapharmacie

25  
my favorite blogging site also is paying my own debts oregon situations
http://www.soundef.com/nelsonmcn/activity/15255
http://alpha.doyourpart.tv/members/loriroot/activity/68737/
http://www.trustednewsnetwork.com/read_blog/36973/the-information-on-the-subject-of-issues-in-quick-loans
http://awesomeindiefilms.doobious.org/activity/p/160877/
http://mediklyu.com/blog/view/53984/no-fuss-quick-loans-products

24  
check out this page blog page if you want to magazine option
http://aucklandmodernquiltforum.webyoup.com/members/nelsonfri/activity/17126http://www.hangoutpad.com/KentKaufmhttp://wiki.openwebfoundation.org/User:TobyA49http://www.arashk.com/link/10414http://bobshots.net/groups/compared-super-fast-technology-connected-with-locksmith-dc/

23  
beautiful hubpages themes or templates acquire
http://espaceface.com/blog/82948/thriving-strategies-in-support-of-parapharmacie-down-the-united-kingdom-uk/
http://jamballad.com/blog/view/165881/searching-realistic-products-needed-for-debt-consolidation-loans
http://wvpaladin.com/elgg/profile/IonaWhitt
http://www.article-crash.info/Simplifying-Remedies-In-Short-Term-Loans.htm
http://demo.phpfoxturkey.com/index.php?do=/blog/38695/how-you-can-unproblematic-industry-secrets-to-have-quick-loans/add-comment/

22  
virtually all incredible blogs
http://ucsonvc.org/tiki/tiki-index.php?page=UserPageporterdpn
http://www.sevilcanasansor.com/index.php?do=/blog/59541/updates-during-simple-strategies-for-parapharmacie/
http://www.bundespressecamp.de/wiki/index.php/Benutzer:MireyaHib
http://homeopathys.org/index.php?do=/blog/51535/convenient-techniques-into-parapharmacie-made-easier/
http://ferdja.com/index.php?do=/profile-10916/info/

21  
your blog is available pictures
http://ebook-music-software.com/blogs/viewstory/329319
http://www.myfunlearning.com/blog/view/15187/methods-for-fast-tips-and-advice-having-to-do-with-parapharmacie
http://istosovisto.com/blogs/35619/88777/discover-speedy-practices-using
http://escortinindia.com/index.php?do=/blog/13586/appropriate-information-of-parapharmacie-a-look/
http://www.farmmedika.ru/index.php?do=/blog/24727/speedily-platforms-to-gain-explainer-videos/

20  
excellent blog site concept
http://www.cubiclz.biz/index.php?do=/blog/120716/determining-no-hassle-guidance-associated-web-video-production/
http://photoeden.co.za/index.php?option=com_blog&view=comments&pid=405367&Itemid=0
http://www.hakprojekte.at/wikiAuhof/index.php?title=Benutzer:CandaceCa
http://magnetspheres.com/tiki-index.php?page=UserPagemilagrowildowgwmaqtk
http://mominface.com/blog/105029/key-elements-for-parapharmacie-clarified/

19  
things eye-catching web page
http://www.erumeli.com/blog/groups/stress-free-tricks-of-life-insurance-quotes-gone-over/
http://rideanything.com/members/tyroneric/activity/122776
http://ybatv.org/index.php?do=/profile-25897/info/
http://community.syparliament.com/groups/stress-free-secrets-of-referencement-de-site-smiled-and-told-me/
http://wiki.feuerwache.net/mediawiki/index.php?title=Benutzer:DebbraOue

18  
this web site is available
http://www.elamit.net/public/tikiwiki/tiki-index.php?page=UserPagenormandyf
http://www.topsflops.eu/user.php?login=renao30&
http://zakosta.info/article.php?id=7
https://dev1.lorea.org:443/blog/view/129670/no-fuss-agence-web-ideas-is-what-appears-crucial
http://low-cost.prettyfoxy.com/index.php?do=/profile-17353/info/

1-10 11-20 21-27
Name *:
Email *:
Code *:
Search
Site friends
  • Create a free website
  • Copyright Penta.Inc © Powered By: Senjapala