lesson 5a – methods – math class by john b. owen all rights reserved ©2011

53
Lesson 5A – Methods – Math class OWEN COMPUTER SCIENCE LESSONS By John B. Owen All rights reserved ©2011

Upload: noe-sage

Post on 28-Mar-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

Lesson 5A – Methods – Math class

OWEN COMPUTER SCIENCE LESSONS

By John B. Owen

All rights reserved

©2011

Page 2: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

2Lesson 5A - Methods – Math class

• Objectives

• Math class definition/API

• Math class program examples

• ceil and floor

• Static import statements

• Lesson Summary / Labs

• Acknowledgement of use

Topic List

Page 3: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

3Lesson 5A - Methods – Math class

• Students will understand more about the various Math class methods including sqrt, pow, abs, ceil, floor, the trig functions sine, cosine, and tangent, and the constant values PI and E.

• The Java documentation help file, or API, will be explored specifically in regards to the Math class.

• Also, the use of static import statements for the System and Math classes will be introduced.

Objectives

Page 4: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

4Lesson 5A - Methods – Math class

• Labs include work with classic math functions including number line distance, coordinate plane distance, area and volume formulas for various geometric 2D and 3D figures, problems involving the use of ceil and floor, the Pythagorean theorem, quadratic formula and various trig functions such as the law of sines and law of cosines.

Objectives

Page 5: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

5Lesson 5A - Methods – Math class

Previously, several Math class items were introduced, including: • pow – returns the power given a base and

exponent

• sqrt – returns the square root of a value

• PI – a fairly precise constant for PI, the ratio of the circumference of a circle to its diameter

• E – a fairly precise constant for E, the base of the natural logarithms

Math class definition

Page 6: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

6Lesson 5A - Methods – Math class

A complete definition of the Math class can be found in the Java help files, or external documentation, which is often referred to as the Java API (application programming interface).

On the next screen you can see the beginning of this definition.

Math class definition

Page 7: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

7Lesson 5A - Methods – Math class

Math class API

You can find this help file very easily by typing the word “Math” anywhere in your program, placing your cursor on the word, then doing the key sequence CTRL-F1.

Page 8: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

8Lesson 5A - Methods – Math class

Math class API

Let’s examine several features of this help file…

• The Math class belongs to the java.lang package

• It is a “descendant” of, or “extends” the java.lang.Object class, which means it has “inherited” all of its features, and added a few more of its own (more on inheritance later).

• A brief description of the class is provided.

Page 9: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

9Lesson 5A - Methods – Math class

Math class API

Here you see the beginning of the listing of Math class fields and methods, (over 40 in all) including the constants E and PI, as well as the first few methods.

Page 10: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

10Lesson 5A - Methods – Math class

Math class API

The abs method is “overloaded”, or defined in several ways to be able to handle four different data type parameters: double, float, int, and long.

Page 11: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

11Lesson 5A - Methods – Math class

Math class API

Notice that the return type is the same as the parameter type for this method.

These are parameter

s!

Page 12: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

12Lesson 5A - Methods – Math class

Math class API

And these are

return values! Notice that the return type is the

same as the parameter type for this method.

Page 13: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

13Lesson 5A - Methods – Math class

Math class API

Eureka! They

match!!!Notice that the return type is the same as the parameter type for this method.

Page 14: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

14Lesson 5A - Methods – Math class

Math class API

Not all match, however. Most of the return

types are doubles, and sometimes there are multiple parameters.

Page 15: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

15Lesson 5A - Methods – Math class

Math class API

Just look carefully for the method you want,

and see what parameters it requires,

and what the return type is.

Page 16: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

16Lesson 5A - Methods – Math class

Math class API In all of the API class listings, there are two main sections.

The column on the left indicates the return type, or the type of data for the answer returned by the method.

All of the Math class methods are “static”, which means they are “stand-alone” methods and work independently of any object (more explanation on that later).

The column on the right contains the name of the method, plus any parameter(s) it receives, along with a description of its purpose.

Page 17: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

17Lesson 5A - Methods – Math class

Math class APIOn the next few slides, the few methods that we will study are highlighted. Study them carefully and become familiar with them.

On this slide, check out the ceil and cos methods

Page 18: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

18Lesson 5A - Methods – Math class

Math class APIThis screen shows the floor, hypot, log, and log10 methods.

Page 19: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

19Lesson 5A - Methods – Math class

Math class APIThe max and min methods are very useful and are overloaded for several different situations.

Using Math class methods

Page 20: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

20Lesson 5A - Methods – Math class

Math class APIThe random, pow, round, sin, sqrt and tan methods are also very useful

Page 21: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

21Lesson 5A - Methods – Math class

Math class APIThe toDegrees and toRadians methods help to convert between the two types of angle measures, degrees and radians.

Using Math class methods

Page 22: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

22Lesson 5A - Methods – Math class

Math class program examples

• Now let’s look at how to use the Math class methods by examining several program examples.

Page 23: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

23Lesson 5A - Methods – Math class

Math class program examples

Here are examples of E, PI, abs, ceil, and floor.

Page 24: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

24Lesson 5A - Methods – Math class

Math class program examples

Here are examples of the trig functions cos, sin, tan, the toRadians and toDegrees functions, and the log and log10 functions.

Page 25: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

25Lesson 5A - Methods – Math class

Math class program examples

Here are examples of hypot, pow, max, min, random, round, and sqrt.

Page 26: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

26Lesson 5A - Methods – Math class

ceil and floor

• Most of the Math functions are familiar to you, or will be as a part of your math-based learning, but the ceil and floor functions are not part of the standard math curriculum and require some explanation.

Page 27: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

27Lesson 5A - Methods – Math class

ceil and floor

• In a similar way to the rounding function, ceil and floor convert any decimal value to the nearest whole number, but in a specific direction, NOT according to the rounding rule.

Page 28: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

28Lesson 5A - Methods – Math class

ceil and floor

• The ceil function ALWAYS converts upwards, towards the “ceiling”.

• The floor function ALWAYS converts downwards, towards the floor.

• 4.5 would convert to 5.0 with the ceil function, and 4.0 with the floor function.

Page 29: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

29Lesson 5A - Methods – Math class

ceil and floor

Here are several examples. Pay close attention to the negative values.

Page 30: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

30Lesson 5A - Methods – Math class

ceil and floor

They seem opposite, but if you think of the vertical number line, it all makes sense.

Page 31: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

31Lesson 5A - Methods – Math class

ceil and floor

Notice carefully that although the resulting value is always a whole number, it is still a double type.

Page 32: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

32Lesson 5A - Methods – Math class

ceil and floor situations

• These two interesting functions play a critical part of problem-solving, especially in situations that require “interpreting” the remainder, or fractional part of a division problem.

Page 33: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

33Lesson 5A - Methods – Math class

ceil and floor situations

• For example, if you need to purchase enough plywood to do a job, and your precise calculations come up to 4.5 sheets of plywood, you would “ceil” the value and purchase 5 sheets. A lumber company will NOT sell you 4.5 sheets of plywood, sorry!

Page 34: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

34Lesson 5A - Methods – Math class

ceil and floor situations

• Another situation would be if you have a certain amount of money and calculate that you can purchase exactly 4.5 items of something you need, clearly you would “floor” the value and only be able to purchase 4 items, with some change leftover.

Page 35: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

35Lesson 5A - Methods – Math class

Static imports

• The previous program examples used the Math class methods and the output statement System.out.println quite often, which brings up a really nifty feature of Java…the use of static imports.

Page 36: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

36Lesson 5A - Methods – Math class

Static imports

• Since the two most commonly used classes in many programs are the System and Math classes, it is quite useful to list two static import statements at the top of the program:

• import static java.lang.Math.*;

• import static java.lang.System.*;

Page 37: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

37Lesson 5A - Methods – Math class

Static imports• import static java.lang.Math.*;

• import static java.lang.System.*;

• These import statements enable the use of the System and Math class methods WITHOUT having to say System or Math each time, saving lots of typing and greatly simplifying your code!

Page 38: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

38Lesson 5A - Methods – Math class

Static import program examples

Notice that with the two static import statements, the words System and Math have not been included in the commands, but the program still works.

Page 39: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

39Lesson 5A - Methods – Math class

• In this lesson, you learned more about the Math class methods, specifically about the ceil and floor methods, how to examine the class in great detail using the Java API, and about how to use static imports for System and Math to greatly simplify and streamline your code.

Lesson Summary

Page 40: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

40Lesson 5A - Methods – Math class

• The following labs are all intended to help you practice using the Math methods. There may be more than one way to solve the program.

• Be sure that the output is precise, especially whether or not a value is an integer or a decimal, and how many decimal places are used.

Labs

Page 41: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

41Lesson 5A - Methods – Math class

Lab 5A-1WAP to•  find the distance between two input integers

representing values on the number line. The formula is: |a-b|

•  find the distance between two input decimals representing values on the number line. The formula is: |a-b|

Input: (data file “lab5A1.in”) 3 5 -3.3 5.865 -88 24.9 1678.4Output:The distance from 3 to 5 is 2The distance from -3.3 to 5.8 is 9.1The distance from 65 to -88 is 153The distance from 24.9 to 1678.4 is 1653.5

Page 42: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

42Lesson 5A - Methods – Math class

Lab 5A-2WAP to input an integer num from the keyboard and:

: find the area of a square whose side length is num.

: find the volume of a sphere whose radius is num.

: find the length of the side of a square whose area is num. Input Data File (“lab5A2.in”):5 82

Output: The area of a square whose side length is 5 is 25The volume of a sphere whose radius is 5 is 523.6The side length of a square whose area is 5 is 2.24The area of a square whose side length is 82 is 6724The volume of a sphere whose radius is 82 is 2309564.9The side length of a square whose area is 82 is 9.06

Page 43: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

43Lesson 5A - Methods – Math class

Lab 5A-3

WAP to find the minimum number of 4 X 8 sheets of plywood it would take to cover a roof when the total amount needed in square feet is input from a data file. Input Data File (“lab5A3.in”):200 988 Output:A 200 square-foot roof needs a minimum of 7 sheets of plywood.A 988 square-foot roof needs a minimum of 31 sheets of plywood.

Page 44: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

44Lesson 5A - Methods – Math class

Lab 5A-4WAP to find out the maximum number of DVDs Johnny can buy from WalMart if the price for each DVD (including tax) is $8.99 and the amount of money he has in his wallet is a value input from a data file. Input Data File (“lab5A4.in”):26.25 53.94 Output:With $26.25, Johnny can buy 2 DVDs.With $53.94, Johnny can buy 6 DVDs.

Page 45: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

45Lesson 5A - Methods – Math class

Lab 5A-5WAP to find the distance from two points in a coordinate plane, inputting the x and y coordinate for each. Output the distance formatted to two decimal places. Use the Math.hypot method to accomplish this task. Input coordinates in this order: x1 y1 x2 y2 Input Data File (“lab5A5.in”): 3 5 6 100 -8 -16 5Output:The distance between the points (3,5) and (6,10) is 5.83 units.The distance between the points (0,-8) and (-16,5) is 20.62 units. 

Page 46: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

46Lesson 5A - Methods – Math class

Lab 5A-6In the mathematics field of trigonometry, the Law of Cosines states that the third side of an oblique triangle (any triangle that is not a right triangle) can be found by knowing the length of any two sides and the measure of the included angle (SAS). Given the Law of Cosines formula shown below, WAP to input two sides and the included angle (in degrees) of a triangle and find the length of the third side.

Remember that the Math.cos method must have a radian measure as the parameter, which means you will have to convert the input angle from degree measure to radians using the Math.toRadians method.

Page 47: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

47Lesson 5A - Methods – Math class

Lab 5A-6given : sides a,b and angle C (in degrees) find side c

_____________________ c = √a2 + b2 - 2*a*b* cos(C in

radians) Input the data in the following order: side, side, angle Input Data File (“lab5A6.in”): 5.18 6.0 6045 67 3510.5 40.8 120

Output on next slide 

Page 48: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

48Lesson 5A - Methods – Math class

Lab 5A-6Output: A triangle with sides of length 5.18 and 6.00 and an included angle measuring 60 degrees has a third side of length 5.63.

A triangle with sides of length 45.00 and 67.00 and an included angle measuring 35 degrees has a third side of length 39.68. A triangle with sides of length 10.50 and 40.80 and an included angle measuring 120 degrees has a third side of length 46.94.

Page 49: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

49Lesson 5A - Methods – Math class

Lab 5A-7Another classic problem found in trigonometry deals with the right triangle. When the lengths of any two sides of a triangle are known, it is possible to find the measure of the two acute angles of the triangle, using the inverse of sin, cos, and tan functions (asin, acos, atan).

To summarize, when the opposite side (0) to an angle (A) and the hypotenuse (h) are known, you use the sin function in the following equation to solve for A: sin A = o/h

Use the cos function when the adjacent side (a) and hypotenuse (h) are known: cos A = a/h

oA

h

a A

h

Page 50: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

50Lesson 5A - Methods – Math class

Lab 5A-7Use the tan function when the opposite (o) and adjacent (a) sides are known: tan A = o/a

To solve for A in the above tangent situation, transform the tan A = o/a equation into A = atan(o/a).

Since the measure calculated will be in radians, you must convert it to degrees to find the final answer. The same process is used for cos and sin.

Given a data file with two sides given (always the two shorter sides), find and output the two acute angles of the right triangle.

oAa

Page 51: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

51Lesson 5A - Methods – Math class

Lab 5A-7Input Data File (“lab5A7.in”):3 45 1210 1050 87

Output:A right triangle with shorter sides of length 3 and 4has acute angle measures of 36.9 and 53.1 degrees.A right triangle with shorter sides of length 5 and 12has acute angle measures of 22.6 and 67.4 degrees.A right triangle with shorter sides of length 10 and 10has acute angle measures of 45.0 and 45.0 degrees.A right triangle with shorter sides of length 50 and 87has acute angle measures of 29.9 and 60.1 degrees.

Page 52: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

52Lesson 5A - Methods – Math class

• You now understand how to use many more of the Math class methods, as well as the Java API, and static import statements.

• The Lesson 5B will elaborate more about the String class methods.

CONGRATULATIONS!

Page 53: Lesson 5A – Methods – Math class By John B. Owen All rights reserved ©2011

53Lesson 5A - Methods – Math class

• Please copy, paste, and customize the brief acknowledgement shown below in an email to [email protected], indicating that you used this lesson.

• Feel free to include any comments you wish to offer, positive or negative.

• “I, (your name), from (educational institution) in (city, state, country) used Lesson 5A on (date), understand, respect and honor all of the rights claimed by the author, Mr. John Owen, and offer the following comments..._______________.”• “THANK YOU!”

Acknowledgement of use