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).
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:
where number is a data item of type integer.
1.1. Note on e-notation
Values for variables are sometimes expressed using what is known as or scientific notation/format. For example:
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.
2. EXAMPLE PROBLEM --- CIRCLE CALCULATION
Nassi-Shneiderman charts for all of the above methods are presented in Figure 4.
Figure 4: Nassi-Shneiderman charts for Circle class methods
2.3.2. CircleApp Class
A Nassi-Shneiderman for the above is presented in Figure 5.
Figure 5: Nassi-Shneiderman charts for CircleApp class method
2.4.1. Circle class
Table 2: Circle Class definition
2.4.2. CircleApp class
Table 3: Circle Class application program
Note how the toString method is invoked in Table 3, we simply include the instance name as the argument in the println (or print) statement which causes the toString method to be invoked automatically.
Remember also that fields, regardless of whether they are private, protected or public, or whether they are class fields or instance fields, can always be used anywhere inside the class in which they are declared. However, data items declared as prt of a method definition, including data items declared as formal parameters (arguments) can only be used within the method in which they are declared.
Software testing encompasses a significant amount of the overall development effort.
Arithmetic Testing: The above is a fairly straightforward procedure and thus very little testing is required. Given an arithmetic statement it is always a good idea to test the effect that negative, positive and zero sample values will have on the outcome. In this case there is only one numeric input, we should therefore test negative and positive inputs as well as zero. If there is more than one input we should test every possible combination of positive, negative and zero input. Thus if we have two inputs we would have 3x3 possible combinations, with three inputs 3x3x3 possible combinations (and so on). This form of testing is known as arithmetic testing.
Data Validation: A second important testing method is known as Data validation testing. Here we provide input data with incorrect syntax, both blatant and subtle. A suggested set of test cases are presented below. The aim of these tests is to insure that the program exits cleanly with causing the system to crash.
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