Object and subroutiens .
Program just by calling subroutines that have already been written for you. In Java, every subroutine is contained either in a class or in an object.
- Some classes that are standard parts of the Java language contain predefined subroutines that you can use.
- A value of type String, which is an object, contains subroutines that can be used to manipulate that string.
- These subroutines are "built into" the Java language. You can call all these subroutines without understanding how they were written or how they work.
- Indeed, that's the whole point of subroutines: A subroutine is a "black box" which can be used without knowing what goes on
Let consider
- Subroutiens are part of a class. One of the purposes of a class is to group together some variables and subroutines, which are contained in that class. These variables and subroutines are called static members of the class.
- You've seen one example: In a class that defines a program, the main() routine is a static member of the class.
- The parts of a class definition that define static members are marked with the reserved word "static", such as the word "static" in public static void main...
When a class contains.
- Static variable or subroutine, the name of the class is part of the full name of the variable or subroutine.
- For example, the standard class named System contains a subroutine named exit. To use that subroutine in your program, you must refer to it as System.exit.
- This full name consists of the name of the class that contains the subroutine, followed by a period, followed by the name of the subroutine. This subroutine requires an integer as its parameter, so you would actually use it with a subroutine call statement such as
System.exit()or
- Calling System.exit will terminate the program and shut down the Java Virtual Machine. Y
- could use it if you had some reason to terminate the program before the end of the main routine. (The parameter tells the computer why the program was terminated.
- A parameter value of 0 indicates that the program ended normally. Any other value indicates that the program was terminated because an error was detected, so you could call System.(
- exit1) to indicate that the program is ending because of an error.
- The parameter is sent back to the operating system; in practice, the value is usually ignored by the operating system.)
System is just one of many standard classes that come with Java. Another useful class is called Math.
This class give us
- an example of a class that contains static variables: It includes the variables Math.
- PIand Math.E whose values are the mathematical constants π and e. Math also contains a large number of mathematical "functions." Every subroutine performs some specific task.
- For some subroutines, that task is to compute or retrieve some data value. Subroutines of this type are called functions. We say that a function returns a value.
- Generally, the returned value is meant to be used somehow in the program that calls the function.
- You are familiar with the mathematical function that computes the square root of a number. T
- corresponding function in Java is called Math.sqrt. This function is a static member subroutine of the class named Math.
- If x is any numerical value, then Math.sqrt(x) computes and returns the square root of that value. Since Math.sqrt(x) represents a value, it doesn't make sense to put it on a line by itself in a subroutine call statement such as
Math.sqrt(x); // This doesn't make sense!
What, after all, would the computer do with the value computed by the function in this case?
You have to tell the computer to do something with the value. You might tell the computer to display it:
- System.out.print( Math.sqrt(x) ); // Display the square root of x.
- or you might use an assignment statement to tell the computer to store that value in a variable:
lengthOfSide = Math.sqrt(x);
The function call.
- Math.sqrt(x) represents a value of type double, and it can be used anyplace where a numeric literal of type double could be used.
- The x in this formula represents the parameter to the subroutine; it could be a variable named "x", or it could be replaced by any expression that represents a numerical value.
- For example, Math.sqrt(2) computes the square root of 2, and Math.sqrt(a*a+b*b) would be legal as long as a and b are numeric variables.
- The Math class contains many static member functions. Here is a list of some of the more important of them:
- Math.abs(x), which computes the absolute value of x.
- The usual trigonometric functions, Math.sin(x), Math.cos(x), and Math.tan(x). (For all the trigonometric functions, angles are measured in radians, not degrees.)
- The inverse trigonometric functions arcsin, arccos, and arctan, which are written as: Math.asin(x), Math.acos(x), and Math.atan(x). The return value is expressed in radians, not degrees.
- The exponential function Math.exp(x) for computing the number e raised to the power x, and the natural logarithm function Math.log(x) for computing the logarithm of x in the base e.
- Math.pow(x,y) for computing x raised to the power y.
- Math.floor(x), which rounds x down to the nearest integer value that is less than or equal to x. Even though 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(3.76) is 3.0, and Math.floor(-4.2) is -5. The function Math.round(x) returns the integer that is closest to x, and Math.ceil(x) rounds x up to an integer. ("Ceil" is short for "ceiling", the opposite of "floor.")
Math random( )
- whichreturns a randomly chosen double in the range 0.0 <= Math.random() < 1.0. (The computer actually calculates so-called "pseudorandom" numbers, which are not truly random but are effectively random enough for most purposes.) We will find a lot of uses for Math.
- randomin future examples.
- 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 by the function is of type double no matter what the type of the parameter.
- 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(9) is the double value 3.0, Math.abs(9) is the int value 9.
- Note that Math.random() does not