Java Reference
In-Depth Information
7. The value e x can be approximated by the following sum:
+
+
x 2
>
+
x 3
>
+ c +
x n
>
1
x
2!
3!
n !
Write a program that takes a value x as input and outputs this sum for n taken to
be each of the values 1 to 10, 50, and 100. Your program should repeat the calcula-
tion for new values of x until the user says she or he is through. The expression n !
is called the factorial of n and is defined as
n
Use variables of type double to store the factorials (or arrange your calculation
to avoid any direct calculation of factorials); otherwise, you are likely to produce
integer overflow, that is, integers larger than Java allows.
8. In cryptarithmetic puzzles, mathematical equations are written using letters. Each
letter can be a digit from 0 to 9, but no two letters can be the same. Here is a
sample problem:
n !
1
2
3
=
*
*
*c*
SEND + MORE = MONEY
=
=
=
=
=
=
=
A solution to the puzzle is S
9, R
8, O
0, M
1, Y
2, E
5, N
6,
=
7. Write a program that finds a solution to the cryptarithmetic puzzle of the
following:
TOO + TOO + TOO + TOO = GOOD
D
The simplest technique is to use a nested loop for each unique letter (in this case T,
O, G, D). The loops would systematically assign the digits from 0 to 9 to each
letter. For example, it might first try T
=
0, O
=
0, G
=
0, D
=
0, then T
=
0, O
=
0,
G
=
0, D
=
1, then T
=
0, O
=
0, G
=
0, D
=
2, etc., up to T
=
9, O
=
9, G
=
9,
=
9. In the loop body, test that each variable is unique and that the equation is
satisfied. Output the values for the letters that satisfy the equation.
9. Write a program that calculates the total grade for N classroom exercises as a per-
centage. Use the DecimalFormat class to output the value as a percent. The user
should input the value for N followed by each of the N scores and totals. Calculate
the overall percentage (sum of the total points earned divided by the total points
possible) and output it using the DecimalFormat class. Sample input and output
is shown below.
How many exercises to input?
3
D
VideoNote
Solution to
Programming
Project 3.9
Score received for exercise 1:
10
Total points possible for exercise 1:
10
Score received for exercise 2:
7
Total points possible for exercise 2:
12
Score received for exercise 3:
5
Search WWH ::




Custom Search