Java Reference

In-Depth Information

ple, these two sequences are complements of each other:

sequence 1:
ACGTTAC

sequence 2:
TGCAATG

Notice how the A's and T's line up with each other, as do the C's and G's. Write

a loop to determine if two
String
s
s1
and
s2
representing DNA sequences are

complements of each other. What do you need to assume about the lengths of

those
String
s?

E8.
Write a loop to produce the DNA complement of a
String s
.

E9.
The Fibonacci numbers are the numbers
0
,
1
,
1
,
2
,
3
,
5
,
8
,
…
. Each number is

the sum of the previous two. This
recurrence relation
describes the sequence:

f
0
= 0

f
2
= 1

f
n
=
f
n-1
+
f
n-2
for
n>1

Write code that finds Fibonacci number
n
, where
n>
1. Use this invariant:

invariant:
a=
f
i
and
b=
f
i-1

postcondition:
i = n
(and, therefore,
a =
f
n
)

E10.
Write a loop that reads a file containing integers and computes their sum.

E11.
Write a loop that reads a file containing integers and computes how many

even integers and how many odd integers it contains.

E12.
Compound interest
on an account is computed as follows: if an account has

balance
balance
, and the annual interest rate is
rate
, the next year's balance is

this:

balance + balance * rate

Write a program segment that reads the initial dollar balance (a
double
), the

interest rate (also a
double
, such as
.07
to represent a 7% interest rate), and the

number of years to calculate (an
int
), and computes the final balance in the

account.

E13.
Write a loop (with initialization) that generates an approximation to e, the

“base of the natural logarithm”, using this formula:

e = 1 + 1/1! + 1/2! + 1/3! + 1/4! + ... + 1/k! + ...

(You can see what
e
is by evaluating
Math.E
.) Here,
k!
is “
k
factorial”, the quan-

tity
1*2*...*k
. Use this invariant:

e = 1 + 1/1! + 1/2! + 1/3! + 1/4! + ... + 1/k!
and

tk = 1/k!

Use type
double
for
e
and
tk
. At each iteration, calculate the next term

Search WWH ::

Custom Search