Java Reference
In-Depth Information
Sample Run:
Line 1: Uppercase a is A
Line 4: 4.2 to the power of 3.0 = 74.09
Line 5: 5 to the power of 4 = 625.00
Line 7: u = 31.20
Line 9: The absolute value of -15 = 15
This program works as follows: The statement in Line 1 outputs the uppercase letter that
corresponds to 'a' ,whichis 'A' .InthestatementinLine4,themethod pow (of the class
Math )isusedtooutput u v . In Java terminology, it is said that the method pow is called with the
(actual) parameters u and v . In this case, the values of u and v are passed to the method pow .The
statement in Line 5 uses the method pow to output 5 4 . The statement in Line 6 uses the method
pow to determine 3 3 ,addsthisvaluetothevalueof u , and then stores the new value into u .
Notice that in this statement, the method pow is called using the name of the class ,whichis
Math , and the dot operator. The statement in Line 7 outputs the value of u .Thestatementin
Line 8 stores -15 into x , and the statement in Line 9 outputs the absolute value of x .
7
Additional programming examples that show how to use some of the other methods of the
class es Math and Character can be found with the Additional Student Files at
www.cengagebrain.com.
User-Defined Methods
Because Java does not provide every method that you will ever need, and designers
cannot possibly know a user's specific needs, you must learn to write your own methods.
User-defined methods in Java are classified into two categories:
￿ Value-returning methods—methods that have a return data type.
These methods return a value of a specific data type using the return
statement, which we will explain shortly.
￿ Void methods—methods that do not have a return data type. These
methods do not use a return statement to return a value.
The next section discusses value-returning methods. Many concepts regarding value-
returningmethods also apply to voidmethods. Voidmethods are discussed later in this chapter.
Value-Returning Methods
The previous section introduced some predefined Java methods, such as pow , sqrt ,
isLowerCase , and toUpperCase . These are examples of value-returning methods, that
is, methods that calculate and return a value. To use these methods in your programs, you
must know the following properties:
 
Search WWH ::




Custom Search