Docsity

Assignment 6: Methods - Computer Science I - Spring 2009 | MAT 2170, Lab Reports of Computer Science

Partial preview of the text

Download Assignment 6: Methods - Computer Science I - Spring 2009 | MAT 2170 and more Lab Reports Computer Science in PDF only on Docsity!

Mat 2170

Week 6

Methods

Spring 2009

Student Responsibilities

I (^) Reading: Textbook, Chapter 5.1 โ€“ 5.

I (^) Lab

I (^) Attendance

Quick Overview of Methods

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().

I Basically, a method is a sequence of statements collected

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.

Useful Terms

I Invoking a method (using its name) is known as calling that

method.

I The code that invokes a method, known as the caller, can pass

information to a method by using arguments.

I (^) When a method completes its operation, it returns to the code which invoked it (i.e., the caller).

I A method can pass information to the caller by returning a

result.

Methods and Information Hiding

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.

Methods and Information Hiding, Cont.

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

method is the principle of information hiding, one of the

cornerstones of software engineering.

Method Calls as Expressions

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().

Methods that belong to a class are called static

methods.

Method Calls as Expressions

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);

Useful Methods in the Math Class โ€” Trig Functions

Math.sin(theta) Returns the sine of theta, measured in

radians

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

Useful Methods in the Math Class โ€” Conversion Functions

Math.toRadians(degrees) Converts an angle from degrees to radians

Math.toDegrees(radians) Converts an angle from radians to degrees

Problem Solving โ€” in class โ€” write programs to:

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.