Java Reference
In-Depth Information
50 public static void giveIntro() {
51 System.out.println("This program computes the");
52 System.out.println("trajectory of a projectile given");
53 System.out.println("its initial velocity and its");
54 System.out.println("angle relative to the");
55 System.out.println("horizontal.");
56 System.out.println();
57 }
58
59 // returns the vertical displacement for a body given
60 // initial velocity v, elapsed time t, and acceleration a
61 public static double displacement( double v, double t,
62 double a) {
63 return v * t + 0.5 * a * t * t;
64 }
65
66 // rounds n to 2 digits after the decimal point
67 public static double round2( double n) {
68 return Math.round(n * 100.0) / 100.0;
69 }
70 }
This version executes the same way as the earlier version.
Chapter Summary
Methods may be written to accept parameters, which are
sets of characteristics that distinguish different members of
a family of tasks. Parameters allow data values to flow into
a method, which can change the way the method executes.
A method declared with a set of parameters can perform an
entire family of similar tasks instead of exactly one task.
Methods can be written to return values to the calling
code. This feature allows a method to perform a complex
computation and then provide its result back to the calling
code. The type of the return value must be declared in the
method's header and is called the method's return type.
Java has a class called Math that contains several useful
static methods that you can use in your programs, such as
powers, square roots, and logarithms.
When primitive values such as those of type int or
double are passed as parameters, their values are copied
into the method. Primitive parameters send values into a
method but not out of it; the method can use the data val-
ues but cannot affect the value of any variables outside it.
An object is an entity that combines data and operations.
Some objects in Java include String s, which are
sequences of text characters, and Scanner s, which read
user input.
Two methods can have the same name if they declare dif-
ferent parameters. This is called overloading.
 
 
Search WWH ::




Custom Search