All Rights Reserved. You write Java programs by combining new methods and classes that you write with predefined available in the Java Application Programming Interface and in various other class libraries Related classes are typically grouped into packages so
that they can be imported into programs and reused
The Java API provides a rich collection of predefined
◦ using existing methods as building blocks to create new
programs Often, you can create programs from existing classes and methods rather than by building customized code Dividing a program into meaningful methods makes
All Rights Reserved. Class MaximumFinder (Fig. 5.3) has two methods— main (lines 8–25) and maximum (lines 28–41) The maximum method determines and returns the
largest of three double values
Most methods do not get called automatically
◦ You must call method maximum explicitly to tell it to perform
All Rights Reserved. Return type ◦ Specifies the type of data a method returns (that is, gives back to its caller) to the calling method after performing its task In some cases, you’ll define methods that perform a task but will not return any information ◦ Such methods use the return type void.
All Rights Reserved. The method name follows the return type Class names, method names and variable names are all identifiers and by convention all use the same camel case naming scheme we discussed in Chapter 2. Class names begin with an initial uppercase letter, and method names and variable names begin with an initial lowercase letter. For a method that requires additional information to perform its task, the method can specify one or more parameters that represent that additional information. ◦ Defined in a comma-separated parameter-list located in the parentheses that follow the method name ◦ Each parameter must specify a type and an identifier A method’s parameters are considered to be local variables of that method and can be used only in that method’s body
All Rights Reserved. Three ways to call a method: ◦ Using a method name by itself to call another method of the same class ◦ Using an object’s variable name followed by a dot (.) and the method name to call a non-static method of the object ◦ Using the class name and a dot (.) to call a static method of a class
All Rights Reserved. There are three ways to return control to the statement that calls a method If the method does not return a result, control returns
when the program flow reaches the method-ending
right brace or when the statement return; executes If the method returns a result, the statement return expression; evaluates the expression, then returns the result to the caller
All Rights Reserved. When a program calls a method, the called method must know how to return to its caller, so the return address of the calling method is pushed onto the method-call stack If a series of method calls occurs, the successive return addresses are pushed onto the stack in last-in, first-out order The method-call stack also contains the memory for the local variables (including the method parameters) used in each invocation of a method ◦ This data, stored as a portion of the method-call stack, is known as the stack frame (or activation record) of the method call ◦ When a method call is made, the stack frame for that method call is pushed onto the method call stack When a method returns to its caller, the stack frame for the method call is popped off the stack and those local variables are no longer known to the program
All Rights Reserved. Argument promotion—converting an argument’s value, if possible, to the type that the method expects to receive in its corresponding parameter Such conversions may lead to compilation errors if Java’s promotion rules are not satisfied These rules specify which conversions are allowed—that is, which ones can be performed without losing data The promotion rules apply to expressions containing values of two or more primitive types and to primitive-type values passed as arguments to methods Each value is promoted to the “highest” type in the expression Figure 5.4 lists the primitive types and the types to which each can be promoted
All Rights Reserved. If truly random, then every value in the range should have an equal chance (or probability) of being chosen each time nextInt is called Class Random provides another version of method nextInt that receives an int argument and returns a value from 0 up to, but not including, the argument’s value
All Rights Reserved. scaling factor—represents the number of unique values that nextInt should produce shift the range of numbers produced by adding a
All Rights Reserved. Rules for the dice game Craps: ◦ You roll two dice. Each die has six faces, which contain one, two, three, four, five and six spots, respectively. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, you win. If the sum is 2, 3 or 12 on the first throw (called “craps”), you lose (i.e., the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes your “point.” To win, you must continue rolling the dice until you “make your point” (i.e., roll that same point value). You lose by rolling a 7 before making your point. The application in Fig. 5.8 simulates the game of craps, using methods to implement the game’s logic
All Rights Reserved. Declarations introduce names that can be used to refer to classes, methods, variables and parameters The scope of a declaration is the portion of the program
that can refer to the declared entity by its name
Such an entity is said to be “in scope” for that portion
All Rights Reserved. Basic scope rules: ◦ The scope of a parameter declaration is the body of the method in which the declaration appears. ◦ The scope of a local-variable declaration is from the point at which the declaration appears to the end of that block. ◦ The scope of a local-variable declaration that appears in the initialization section of a for statement’s header is the body of the for statement and the other expressions in the header. ◦ A method or field’s scope is the entire body of the class.
All Rights Reserved. Compiler distinguishes overloaded methods by their signatures ◦ A combination of the method’s name and the number, types and order of its parameters, but not its return type. Internally, the compiler uses longer method names that include the original method name, the types of each parameter and the exact order of the parameters to determine whether the methods in a class are unique in that class. Method calls cannot be distinguished by return type ◦ Overloaded methods can have different return types if the methods have different parameter lists Overloaded methods need not have the same number of parameters
All Rights Reserved. Graphics methods fillRect and fillOval draw filled rectangles and ovals, respectively These have the same parameters as drawRect and
drawOval; the first two are the coordinates for the
upper-left corner of the shape, while the next two determine the width and height Graphics method setColor seta the current
Reclaiming Your Life from a Traumatic Experience A Prolonged Exposure Treatment Program Workbook 2nd Edition Barbara Olasov Rothbaum - The complete ebook is available for download with one click