Java built-in Subroutines and Functions
subroutine must be referred to as System.exit. Calling such subroutine will end the
program.
of the main routine.
shows that the program finished normally.
ignored.)
Java Functions:
or you might use some sort of assignment statement to instruct the PC to store that value in some variable:
length of side = Math.sqrt(x);
Call of Math.sqrt(x) represents some value of type double, so where there is a need for such it could be used there.
This class contains in itself many static member functions.
Here is a big list of some of the most frequent usage of them:
• Math.abs(x), which calculates the absolute value of x.
• Our usual trigonometric mathematical functions, Math tan(x), Math sin(x) and Math cos(x).
• The inverse trigonometric functions arcsin, arccos, and arctan: Math atan(x), Math asin(x), and Math acos(x). (also all in radians)
• And the Math.exp(x) for computing the number e raised to the power x, and Math log(x) for computing the logarithm of x in the base e (however I am not still sure where e is set there)
• Math.pow(x,y) for calculating x raised to the power of y.
• And Math.floor(x), which rounds x down to it’s the nearest integer value that is less than or equal to x.
Nevertheless, the return value is mathematically an integer, it is returned as a value of type double, rather than of type int as you might expect. For example, Math.floor(4.86) is 4.0. Math.round(x) returns the closest integer to x. So in that case 4.86 will be 5 and not 4.
• Math.random() – returns a randomly double in the range 0.0 and 1.0. (PC actually computes so-called “pseudorandom” numbers, which are not exactly random but are random enough for most cases.)
For these functions, the type of the parameter—the x or y inside the parentheses—can be
any value of any numeric type.
For most of the functions, the value returned will be double type. However, for Math.abs(x), the value returned will be the same type as x; if x is of type int, then so is Math.abs(x).
So, for example, while Math.sqrt(25) is the double value 5.0, Math.abs(25) is the int value 25.
Take note that Math.random() does not have any parameters. You still need that parenthesis, even if there is nothing between them.
The parentheses let the PC know that this is a subroutine and not a variable. Another example of a subroutine that has no parameters is the
function System.currentTimeMillis(), from the System class.
When this is performed by PC, it gets you the current moment of time, showed as the number of milliseconds that have passed since the somewhat set period of time (the start of the year 1970 in Greenwich Mean Time, if you care). One a millisecond is one-thousandth of a second.
The return value of System.currentTimeMillis() is of type long.
This Java function can be used to calculate the time that it takes the computer to perform a task. Difference between start and end in other words.
Sample Program:
Here is a sample program that doing a few mathematical tasks and reports the time
that it takes for the program to run. On some PC’s, this time might be zero or close to it,
because it is too small to measure.
/**
* This program performs some mathematical calculations and displays
* the results. It also reports the number of seconds that the
* PC spent on this task.
*/
public class TimedComputation {
public static void main(String[] args) {
long startTime; // Starting time of program, in milliseconds.
long endTime; //
double time; // Time difference, in seconds.
startTime = System.currentTimeMillis();
double width, height, h; // sides of a triangle
width = 35.0;
height = 10.0;
h = Math.sqrt( width*width + height*height );
System.out.print(“A triangle with cathetus 35 and 10 has hypotenuse “);
System.out.println(h);
System.out.println(“\nMathematically, sin(a)*sin(a) + “
+ “cos(a)*cos(a) minus one should be zero.”);
System.out.println(“Let’s check this for x = 2:”);
System.out.print(” sin(2)*sin(2) + cos(2)*cos(2) – 1 is “);
System.out.println( Math.sin(2)*Math.sin(2)
+ Math.cos(2)*Math.cos(2) – 1 );
System.out.println(“(There can be round-off errors when”
+ ” computing with real numbers!)”);
System.out.print(“\nHere is a random number: “);
System.out.println( Math.random() );
endTime = System.currentTimeMillis();
time = (endTime – startTime) / 1000.0;
System.out.print(“\nRun time in seconds was: “);
System.out.println(time);
} // end main()
} // end class TimedComputation
Comments are closed.