**2.21 Calculation of h:**

We know that h is the altitude, therefore the angle made by the line segment h on chord c is 90 degree. We know if radius is perpendicular to a chord then it bisects the chord. So by pythagoras theorem we can calculate the length of h as:

r^{2} = (c/2)^{2} + (r-h)^{2}

(r-h)^{2} = r^{2} – (c/2)^{2}

r-h = (r^{2}-(c/2)^{2})

h = r – (r^{2}-(c/2)^{2})

**//Program to calculate the area given the diameter and chord_length**

import java.io.*;

public class CalArea {

**// function to calculate value of h and then calculate the area**

double calculateArea(int diameter,int chord_length)

{

double radius = (double)diameter/2; **// calculating the radius**

double h = 0;

double l = Math.pow(radius,2)-Math.pow((chord_length/2),2);

l = Math.sqrt(l);

h = radius – l;

double area = ((2*chord_length*h)/3) + (Math.pow(h, 3)/(2*chord_length));

return area;

} **// end of calculateArea**

public static void main(String[] args) {

CalArea a = new CalArea();

double area =a.calculateArea(12,10);**// 12 is the diameter and 10 is the chord length**

System.out.println(“Area =”+area); **//****print the resultant area**

} *// end of main*

} *// end of class*

**Output:**

**R2.25 (a)** 2.0-1.1 = 0.8999999999999999

We get that the result contins round-off error, as 2.0-1.1 = 0.9 but we obtain the result as 0.8999999999999999, which contains round-off error.

**(b)** 1.0E6 * 1.0E6 = 1.0E12

We get that the result is exact since 1.0E6 * 1.0E6 = 1.0E(6+6) = 1.0E12

**(c)** 65536 * 65536 = 0

We get that the result is an overflow since the max int result java can represent is 2^{31} -1 . Since the result is greater than 2^{31}-1, so it goes back to the minimum value and continues from there.

**(d)** 1_000_000L * 1_000_000L = 1000000000000

We get that the result is an exact as it is multiplication of long values (specified by L), and not int multiplication and so overflow doesn’t occur.

**R2.26** 3*1000*1000*1000 = -1294967296 —–1

3.0*1000*1000*1000 = 3.0E9 —–2

- In 1, we see that we get negative result, it is because the result overflows as it is int multiplication and the result is greater than 2
^{31}-1. So it goes back to the minimum value i.e -2147483648 and continues from there.
- In 2 , we get an exact result because it involves multiplication of double values.Hence the result doesn;t overflow since maximum number that can be represented by double is 2
^{63}-1. and the result is less than the maximum number so it returns the exact result.

**R2.27 Recommendations regarding variables and constants that make programs easier to read and maintain are:**

- Variables declared in the programs must be initialized as this can cause errors.
- Variable declaration must have the below structure:

<type><variable_name> = <value according to the type of the variable>

- There are several types of numbers : short,int,long,float,double. Do not use comma as a decimal separator
- Do not use fractions , use decimal notation
- When a variable anme is selected, it should be picked in such a way that it explains its purpose
- Variable names should start with a letter or underscore character and remaining characters must be letters, numbers or underscores
- Spaces are not permitted in variable names
- Variable names are case-sensitive
- Reserved keywords cannot be used for variable names
- In java, variable names should start with a lowercase and class name should start with uppercase letter
- Comments should be added in the program for explanation as this helps anyone who reads your program to understand it
- Variable should be declared before it is used, else it causes error
- When variables are declared as final, it becomes constants. Constants are usually written in capital letters to distinguish it from variables.
- Use named constants to explain the meaning of numeric constants in programs