BeanShellTutorial:2 Arithmetic

From jWork.ORG
Jump to: navigation, search
Limitted access. Reguest membership or login to this link first if you are already a member
Contents

Arithmetic

In BeanShell you also have the option of working with "loosely typed" variables. That is, you can simply omit the types of variables that you use (both primitives and objects). Now, let's try to use BeanShell as a calculator. We will calculate 3*8/6:


print(3*8/6);


Numbers and Math

You can also do some calculations using elementary functions. Math functions are imported by BeanShell automatically and you will need only to call the package name Math. Let's try to use BeanShell as a calculator. We will calculate $\sqrt(100))$:


print(Math.sqrt(100));

We have added the "Math" package name which is imported automatically by BeanShell. Let us consider a logarithmic function:


print(Math.log(100));

Default Imports

In the previous example, we called the "Math" package, which belongs to Java java.lang package. This package is imported automatically. By default, common Java core and extension packages are imported for you. They are, in the order in which they are imported:

  • javax.swing.event
  • javax.swing
  • java.awt.event
  • java.awt
  • java.net
  • java.util
  • java.io
  • java.lang

Exercises:

  1. Calculate logarithm of 10
  2. Calculate the absolute values of -100