









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Methods
Spring 2009
I (^) Reading: Textbook, Chapter 5.1 โ 5.
I (^) Lab
I (^) Attendance
I (^) Youโve been working with methods ever since Lab 1 and the HelloWorld program.
I (^) The run() method in every program is just one example.
I (^) Other examples are println() and setColor().
together and given a name.
I (^) The name makes is possible to execute the statements more easily.
I (^) Instead of copying the entire list of statements, we can simply provide the method name.
method.
I (^) When a method completes its operation, it returns to the code which invoked it (i.e., the caller).
I (^) Important advantage of methods: they make it possible to ignore the inner workings of complex operations.
I (^) When a method is used, it is more important to know what the method does than to understand exactly how it works.
I (^) The underlying details of a method are of interest only to the programmer who implements and maintains it.
I (^) Code which calls a method only cares about the method interface, and whether the method is correct.
I (^) Programmers who use a method as a tool can usually ignore the implementation altogether.
I (^) The idea that callers should be insulated from the details of a
cornerstones of software engineering.
I (^) Syntactically, method calls in Java are part of the expression framework.
I (^) Methods that return a value can be used as terms in an expression, just like variables and constants.
I (^) The Math class in the java.lang package defines several methods that are useful in writing mathematical expressions.
I (^) You must include the name of the class, along with the method name, for example: Math.sqrt().
I (^) Suppose we need to compute the distance from the origin to the point (x, y), which is given by the formula: d =
x^2 + y 2
I (^) We can apply the square root function by calling the sqrt() methods in the Math class:
double distance = Math.sqrt(x * x + y * y);
Math.cos(theta) Returns the cosine of theta
Math.tan(theta) Returns the tangent of theta
Math.asin(x) Returns the angle whose sine is x
Math.acos(x) Returns the angle whose cosine is x
Math.atan(x) Returns the angle whose tangent is x
Math.toRadians(degrees) Converts an angle from degrees to radians
Math.toDegrees(radians) Converts an angle from radians to degrees
I (^) Create a table of values for โx, x, and x^2 running from x = 0 to x = 100 by 10s
I (^) Create a table of values for x, sin(x), and cos(x) as x runs from 0 to 2ฯ by ฯ 4 increments.
I (^) Solve for the nth^ Fibonacci number, defined by the sequence 0, 1, 1, 2, 3, 5, 8, 13... The first two terms are 0 and 1, and every subsequent term is the sum of the preceding two.
I (^) Modify the previous program to display all the terms in the Fibonacci sequence that are smaller than 1,000.