Java Reference
In-Depth Information
30. Suppose that a program has been written that produces the output shown in the previous problem. Now the author
wants the program to be scalable using a class constant called SIZE . The previous output used a constant height of 6,
since there were 6 lines. The following is the output for a constant height of 4. Create a new table that shows the
expressions for the character counts at this new size of 4, and compare these tables to figure out the expressions for
any size using the SIZE constant.
!!!!!!!!!!!!!!
\\!!!!!!!!!!//
\\\\!!!!!!////
\\\\\\!!//////
Exercises
1. In physics, a common useful equation for finding the position s of a body in linear motion at a given time t , based on
its initial position s 0 , initial velocity v 0 , and rate of acceleration a , is the following:
1
2 at 2
s
=
s 0 +
v 0 t
+
Write code to declare variables for s 0 , v 0 , a , and t , and then write the code to compute s on the basis of these values.
2. Write a for loop that produces the following output:
1 4 9 16 25 36 49 64 81 100
For added challenge, try to modify your code so that it does not need to use the * multiplication operator. (It can be
done! Hint: Look at the differences between adjacent numbers.)
3. The Fibonacci numbers are a sequence of integers in which the first two elements are 1, and each following ele-
ment is the sum of the two preceding elements. The mathematical definition of each k th Fibonacci number is the
following:
F
1
k
-
1
2 +
F
1
k
-
2
2
, k
7
2
1
2 = e
F
k
1, k
2
The first 12 Fibonacci numbers are
1 1 2 3 5 8 13 21 34 55 89 144
Write a for loop that computes and prints the first 12 Fibonacci numbers.
4. Write nested for loops to produce the following output:
*****
*****
*****
*****
5. Write nested for loops to produce the following output:
*
**
***
****
*****
 
Search WWH ::




Custom Search