mat 2170
play

Mat 2170 Jargon Info Hiding Week 6 Math Lib Lab 6 Exercises - PowerPoint PPT Presentation

Mat 2170 Week 6 Methods Week 6 Mat 2170 Jargon Info Hiding Week 6 Math Lib Lab 6 Exercises Methods Spring 2014 Student Responsibilities Mat 2170 Week 6 Methods Week 6 Jargon Reading: Textbook, Chapter 5.1 5.3 Info Hiding


  1. Mat 2170 Week 6 Methods Week 6 Mat 2170 Jargon Info Hiding Week 6 Math Lib Lab 6 Exercises Methods Spring 2014

  2. Student Responsibilities Mat 2170 Week 6 Methods Week 6 Jargon Reading: Textbook, Chapter 5.1 – 5.3 Info Hiding Math Lib Lab 6 Lab Exercises Attendance

  3. Chapter Five Overview: 5.1 – 5.3 Mat 2170 Week 6 Methods Methods Week 6 Jargon Info Hiding Math Lib Lab 6 5.1 Quick overview of methods Exercises 5.2 Writing our own methods 5.3 Mechanics of the method–calling process

  4. Quick Overview of Methods Mat 2170 Week 6 Methods Week 6 You’ve been working with methods ever since Lab 1 and the Jargon HelloWorld program. Info Hiding Math Lib Lab 6 The run() method in every program is one example. Exercises Other examples are println() , setColor() , and getHeight() .

  5. Methods Mat 2170 Week 6 Methods Basically a method is a Week 6 sequence of statements Jargon Info Hiding collected together and given a name . Math Lib Lab 6 Exercises The name makes is possible to execute the statements more easily. Instead of copying the entire list of statements, we can simply provide the method name.

  6. Useful Terms Mat 2170 Week 6 Invoking a method (using its name) is known as calling Methods that method. Week 6 Jargon The part of a program or code that invokes a method is Info Hiding named the caller or calling program . Math Lib Lab 6 Exercises The caller can pass information to a method by using arguments (the java expressions in the parentheses), e.g.: R.setFilled(true) R.setFilled((row + col) % 2 == 0) Color myColor = Color.green; R.setColor(myColor)

  7. Mat 2170 Week 6 Methods Week 6 Jargon When a method completes its operation, it returns to the Info Hiding code which invoked it (i.e., the caller). Math Lib Lab 6 Exercises A method can pass information back to the caller by returning a single result .

  8. Method Calls as Messages Mat 2170 Week 6 Methods Week 6 The act of calling a method is often described in terms of Jargon sending a message to an object . Info Hiding Math Lib Lab 6 Exercises The method call: R.setColor(Color.RED); is regarded metaphorically as sending a message to the GRect object R telling it to change its color to red.

  9. Receivers Mat 2170 Week 6 Methods Week 6 The object to which a message is sent is called the receiver . Jargon Info Hiding Math Lib Lab 6 The general pattern for sending a message to an object is: Exercises receiver.methodName(argument list)

  10. Information Hiding Mat 2170 Week 6 One important advantage of methods : Methods They allow us to ignore the inner workings of complex Week 6 operations. Jargon Info Hiding Math Lib When a method is used, it is more important to know Lab 6 Exercises what the method does than to understand exactly how it does it. The underlying details of a method are of interest only to the programmer who implements and maintains it.

  11. Method Interface Mat 2170 Programmers who utilize a method are concerned about: Week 6 Methods 1. The method interface Week 6 1.1 return type Jargon 1.2 method name Info Hiding 1.3 order, number and type of arguments Math Lib 2. Whether the method is correct . Lab 6 Exercises They can usually ignore the implementation altogether. 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 .

  12. Methods as Tools for Programmers Mat 2170 Week 6 Methods A method provides a service to a programmer , who is typically creating some kind of application. Week 6 Jargon Info Hiding Math Lib A programmer utilizes methods to reduce the amount of Lab 6 work he or she must do, and to organize the software they Exercises are writing. Methods like readInt() and println() are used to communicate with and obtain information from the user .

  13. Method Calls as Expressions Mat 2170 Week 6 Syntactically, method calls in Java are part of the expression Methods framework. Week 6 Jargon Info Hiding Methods that return a value can be used as terms in an Math Lib expression, just like variables and constants. Lab 6 Exercises The Math class in the java.lang package defines several methods that are useful in writing mathematical expressions. Methods that belong to the entire class are called static methods.

  14. Math Library Method Calls Mat 2170 Week 6 You must include the name of the class , along with the Methods method name, for example: Math.sqrt() . Week 6 Jargon Info Hiding Suppose we need to compute the distance from the origin to Math Lib the point (x, y), which is given by the formula: Lab 6 x 2 + y 2 � d = Exercises We can apply the square root function by calling the sqrt() method from the Math class: double distance = Math.sqrt(x * x + y * y);

  15. Useful Methods in the Math Class Mat 2170 Week 6 Methods Week 6 Returns the absolute value of x Math.abs(x) Jargon Returns the smaller of x and y Info Hiding Math.min(x, y) Math Lib Math.max(x, y) Returns the larger of x and y Lab 6 Returns √ x , the square root of x Exercises Math.sqrt(x) Returns log e x , the natural logarithm of x Math.log(x) Returns e x , the inverse logarithm of x Math.exp(x) Returns x y , the value of x raised to the y power Math.pow(x, y)

  16. The Math Class — Trig Functions Mat 2170 Week 6 Methods Math.sin( theta ) Returns the sine of theta , Week 6 measured in radians Jargon Info Hiding Math.cos( theta ) Returns the cosine of theta Math Lib Lab 6 Returns the tangent of theta Math.tan( theta ) Exercises Math.asin(x) Returns the angle whose sine is x Returns the angle whose cosine is x Math.acos(x) Math.atan(x) Returns the angle whose tangent is x

  17. The Math Class — Conversion Functions Mat 2170 Week 6 Methods Week 6 Jargon Info Hiding Converts an angle from Math.toRadians( degrees ) Math Lib degrees to radians Lab 6 Exercises Converts an angle from Math.toDegrees( radians ) radians to degrees

  18. Solving Quadratic Equations Mat 2170 The standard quadratic equation is: Week 6 Methods ax 2 + bx + c = 0 Week 6 The quadratic formula , to solve for the roots, is: Jargon √ Info Hiding b 2 − 4 ac x = − b ± Math Lib Lab 6 2 a Exercises Of interest to us is the radicand , since it determines the number of solutions: b 2 − 4 ac How many real solutions are there when the radicand is . . . 1. positive? 2. zero? 3. negative?

  19. Also of Use in Lab 6 Mat 2170 Week 6 More messages available for GRect and GOval objects Methods Week 6 Jargon getX() returns the x component of the object’s Info Hiding position Math Lib getY() returns the y component of the object’s Lab 6 position Exercises returns the width of the object getWidth() getHeight() returns the height of the object move(dx, dy) moves the object using the displacements dx and dy

  20. Examples Mat 2170 Week 6 Methods Assume R is a GRect and C is a GOval : Week 6 R.getX() C.getX() Jargon Info Hiding R.getY() C.getY() Math Lib Lab 6 R.getWidth() C.getWidth() Exercises R.getHeight() C.getHeight() R.move(1.0, − 1.5) C.move(1.0, − 1.5) R.move(dx, dy) C.move(dx, dy)

  21. Problem Solving — in Class Mat 2170 Design algorithms, then programs, to: Week 6 Methods Create a table of values for x, √ x , and x 2 running from Week 6 x = 0 to x = 100 by 10 s Jargon Info Hiding Math Lib Create a table of values for x, sin( x ), and cos( x ) as x runs Lab 6 from 0 to 2 π by π 4 increments. Exercises Solve for the n th 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. Modify the previous program to display all the terms in the Fibonacci sequence that are smaller than 1,000.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend