Java Reference
In-Depth Information
The following program uses this series to find the approximate value of
p. However, the statements are in the wrong order and there is also a
bug in this program. Rearrange the statements and also find and remove
the bug so that this program can be used to approximate p.
import java.util.*;
public class Ch5_PrExercise2
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
double pi = 0;
long i;
long n;
n = console.nextInt();
System.out.print("Enter the value of n: ");
System.out.println();
if (i % 2 == 0)
pi = pi + (1 / (2 * i + 1));
else
pi = pi - (1 / (2 * i + 1));
for (i = 0; i < n; i++)
{
pi = 0;
pi = 4 * pi;
}
System.out.println("pi = " + pi);
}
}
3. Rewrite the program of Example 5-5, Telephone Digits. Replace the state-
ments from Lines 10 to 28 so that the program uses only a switch structure
to find the digit that corresponds to an uppercase letter.
4. The program Telephone Digits outputs only telephone digits that corre-
spond to uppercase letters. Rewrite the program so that it processes both
uppercase and lowercase letters and outputs the corresponding telephone
digit. If the input is other than an uppercase or lowercase letter, the program
must output an appropriate error message.
5. To make telephone numbers easier to remember, some companies use letters to
show their telephone number. For example, the telephone number 438-5626
can be shown as GET-LOAN. In some cases, to make a telephone number
meaningful, companies might use more than seven letters. For example,
225-5466 can be displayed as CALL-HOME, which uses eight letters. Write
a program that prompts the user to enter a telephone number expressed in
Search WWH ::




Custom Search