Java Reference
In-Depth Information
4.1 Introduction
The focus of this chapter is to introduce mathematical functions, characters, string
objects, and use them to develop programs.
Key
Point
The preceding chapters introduced fundamental programming techniques and taught you how
to write simple programs to solve basic problems using selection statements. This chapter intro-
duces methods for performing common mathematical operations. You will learn how to create
custom methods in Chapter 6.
Suppose you need to estimate the area enclosed by four cities, given the GPS locations (latitude
and longitude) of these cities, as shown in the following diagram. How would you write a program
to solve this problem? You will be able to write such a program after completing this chapter.
problem
Charlotte (35.2270869, -80.8431267)
Atlanta
(33.7489954, -84.3879824)
Savannah (32.0835407, -81.0998342)
Orlando (28.5383355, -81.3792365)
Because strings are frequently used in programming, it is beneficial to introduce strings
early so that you can begin to use them to develop useful programs. This chapter gives a brief
introduction to string objects; you will learn more on objects and strings in Chapters 9 and 10.
4.2 Common Mathematical Functions
Java provides many useful methods in the Math class for performing common mathe-
matical functions.
Key
Point
A method is a group of statements that performs a specific task. You have already used the
pow(a,  b) method to compute a b in Section  2.9.4, Exponent Operations and the random()
method for generating a random number in Section  3.7. This section introduces other useful
methods in the Math class. They can be categorized as trigonometric methods , exponent methods ,
and service methods . Service methods include the rounding, min, max, absolute, and random meth-
ods. In addition to methods, the Math class provides two useful double constants, PI and E (the
base of natural logarithms). You can use these constants as Math.PI and Math.E in any program.
4.2.1 Trigonometric Methods
The Math class contains the following methods as shown in Table  4.1 for performing
trigonometric functions:
VideoNote
Introduce math functions
T ABLE 4.1 Trigonometric Methods in the Math Class
Method
Description
Returns the trigonometric sine of an angle in radians.
sin(radians)
Returns the trigonometric cosine of an angle in radians.
cos(radians)
Returns the trigonometric tangent of an angle in radians.
tan(radians)
Returns the angle in radians for the angle in degree.
toRadians(degree)
Returns the angle in degrees for the angle in radians.
toDegree(radians)
asin(a)
Returns the angle in radians for the inverse of sine.
acos(a)
Returns the angle in radians for the inverse of cosine.
Returns the angle in radians for the inverse of tangent.
atan(a)
 
 
 
 
Search WWH ::




Custom Search