|
|
In addition, in this lecture, we will be considering defining our own constructors and the use of constants (the PI constant from the Math class in this case). In the giant letters example we used the default constructor to create an instance of the class GiantLetters. In the circle calculation example given here (the Circle class) we will be defining our own constructor. The Circle class example will also illustrate: (1) the use of the Double wrapper class, and (2) the use of a programmer defined toString method (introduced when discussing the Integer wrapper class). Note also that the Circle class will form the "super class" when we consider inheritance in a later lecture.
1. OVERVIEW OF ARITHMETIC OPERATORS |
Arithmetic operations are among the most fundamental instructions that can be included in a computer program. Java supports all the standard mathematics operations (see Table 1). Note that:
Table 1: Arithmetic operators
More complex arithmetic operations can be found in the Java Math class. A fragment of the Math class is given in Figure 1. The class is contained in the java.lang package which is always included in all Java programs. The class includes constants such as PI and methods such as: |
Figure 1: Math class diagram showing the fields and methods referenced here
These methods are all class (static) methods and thus we do not need to create an instance of the class Maths to use them --- we simply link them to the class name. For example: Math.abs(number); where number is a data item of type integer. 1.1. Note on e-notationValues for variables are sometimes expressed using what is known as or scientific notation/format. For example: 0.2e004 or: 0.2e-06 where the e (standing for exponent) separates the number from the exponent. Thus 0.2e004 is interpreted as 0.2x10^4 which equals 0.2x10000 which equals 2000. The above example value of 0.2e-06 is then equivalent to 0.2x10^-6 which equals 0.2x0.000001 which equals 0.0000001. |
3. TRIGONOMETRIC RATIOS |
Methods that return the trigonometric ratios (sine, cosine, tangent, cosecant, secant and cotangent) given an appropriate angle are all also available in the Math class. However, it should be noted that the required angle for these methods must be presented in radians and not degrees. Given an angle D (degrees) we can convert this to an angle R (radians) as follows: R = D * Math.PI/180.0 i.e. multiply by Math.PI/180.0. To convert from radians to degree we multiply by 180.0/Math.PI. Thus: D = R * 180.0/Math.PI |
As might be expected, this is such a common operation that the Math class contains methods to do this:
Note that in both cases the argument and the return value are doubles. Note also that these are class methods. An additional example problem that makes use of the trigonometric ratio methods described here is available. |
Created and maintained by Frans Coenen. Last updated 10 February 2015