Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

The Wrapper Classes

Primitive values in Java are not objects; however, Java provides matching classes for each of the
primitive types, so that they can manipulate the primitive types as objects. As shown in Figure 3.16, the
wrapper classes are:
Byte, Short, Integer, Long, Character, Float, Double, Boolean, and Void. The numeric wrapper
classes have all of the methods shown in Figure 3.xx. Each of the classes defines each of these methods
in its own way. There are several other methods in each of the classes, but they are not frequently used at
this level. There is one other method, a class method that is frequently used; each of these classes has a
version of this method. The versions of this method are:

Returns the byte represented by the string value of the


static byte parseByte(String) string.

Returns the double represented by the string value of the


static double
string.
parseDouble(String)

Returns the float represented by the string value of the


static float parseFloat(String) string.

static int parseInt(String) Returns the int represented by the string value of the
string.

Returns the long represented by the string value of the


static long parseLong(String) string.

Returns the short represented by the string value of


static short parseShort(String) the string.

Figure 3.26

Example.

A boat C is 200 meters from the foot B of a vertical cliff, which is 40 meters high. What is the angle of
elevation of the top of the cliff from the boat? Find the line of sight of the boat from the top of the cliff.

Design a class that accepts the two values, calculates the angle and the line drawn from the top of the
cliff to the boat.

Extract the two values from the following strings as input to the class:

Distance from foot of cliff 200


Height of cliff is 40 meters

Solution
 Name the class math.
 Instance variables- call them distance and height
 Instance methods - to return the measure of the angle and the line if drawn from top of cliff to
boat. Let us call them findAngle and lineOfSight respectively.

Algorithm

 Finding angle - apply the arctangent of the height of a right triangle and its base.
Listing 3. 10 Line 14 shows how the method is applied. In addition, Line 16 shows two
concepts: converting the radian measure to degrees, and rounding of the value generated.

 Finding the slant side of a right triangle – apply Pythagoras rule for right triangle. Lines 21 and
22 show how Pythagoras rule is applied. In addition, we are making use of the method pow that
is used to raise a number to some power, and the method sqrt that finds the square root of a
number.

 Assume that the values sent to the constructor are strings. Convert these values to double by
using parseDouble method from the class Double. Lines 7 and 9 demonstrate the usage.
1. class math
2. {
3. double distance, height;
4.
5. public math(String x, String y)
6. {
7. distance = Double.parseDouble(x);
8.
9. height = Double.parseDouble(y);
10. }
11.
12. double findAngle()
13. {
14. double x = Math.atan(height/ distance);
15.
16. return Math.round(Math.toDegrees(x));
17. }
18.
19. double lineOfSight()
20. {
21. double x = Math.pow(height, 2) + Math.pow(distance, 2);
22. return Math.sqrt(x);
23. }
24. }

Listing 3.10

In the test class we have made use the string method substring to extract the numeric string
representations. See Figure 3.11 Lines 8 and 9. Finally Line 18 shows one way of converting a double to
the next integer value equal to or larger than the value itself.

1. class testMath
2. {
3. public static void main(String arg[])
4. {
5. String distance = "Distance from foot of cliff 200";
6. String height = "Height of cliff is 40 meters";
7.
8. distance = distance.substring(28);
9. height = height.substring(19,21);
10.
11. System.out.println("Distance of boat from foot of cliff is: " + distance);
12. System.out.println("The height of the cliff is: " + height);
13.
14. math m = new math(distance, height);
15.
16. System.out.println("The angle of elevation is: " + m. findAngle() + " degrees");
17.
18. System.out.println("The line of sight is " + (int)Math.ceil(m.lineOfSight()) + " meter");
19. }
20. }

Listing 3.11.
Figure 3.27

Summary

In this chapter you have learned about variables, data types, and operations on data Most importantly,
you should have learned the following:

 The conventions that are use to name variables:

 Each variable must be declared before it can be used.

 Java primitive data types and composite types. The primitive type includes numeric types,
which includes integer and floating-point types; character types; and boolean types.

 The integer types are: byte, short, int, long. The corresponding storage requirements for each
of these types are 1 byte, 2 bytes, 4 bytes, and 8 bytes, respectively. The default for an integer
value is int.

 Java floating-point types are float and double. The default of a floating-point number is a
double. The storage requirements are 4 bytes and 8 bytes, respectively.

 The character type, char, is a 2 bytes Unicode representation.

 The third and final primitive data type is the boolean, which has values true or false.

 There are two composite types in Java, namely: array and string. Arrays are used to house more
than one data values of the same type. Individual elements are accessed by use of the index. A
string stores a fixed number of characters that cannot be changed. You can create an array of
string objects.

 You can know the number of items that are in an array by using the string operator length.

You might also like