MS Word

advertisement
COMP 241: Object-Oriented Programming with Java
Fall 2004
Homework Assignment 3
Due by 11:59pm, Friday, Oct. 15, 2004
1- Write an application that reads in an odd integer n and prints in the standard output an
(n x n) square enclosing a diamond shape as shown below. Each corner asterisk of the
diamond should be immediately next to the center asterisk of one side of the square.
Use an input dialog to read the integer n from the user. Your program should work for
values of n between 5 and 21. You will probably find it necessary to use nested for
loops. Files to be submitted: Diamond.java, Diamond.class
n asterisks
***********
*
*
*
*
* *
*
* *
* *
* *
* *
**
**
* *
* *
* *
* *
*
* *
*
*
*
*
***********
n asterisks
Example figure for n=5. The example above is for n=11.
*****
* * *
** **
* * *
*****
2- Consider the following infinite series sum for computing the number 
 = 4 (1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + …)
Let Sum(n) denote the sum of first n terms of the series above. Write a Java applet that
asks the user to input a positive integer n and in tabular format (using a message dialog)
as in Figure 5.6 in your textbook, prints approximate values of  obtained by Sum(n),
Sum(n+1),…, Sum(n+4). Each approximate value should be printed using eight digits
after the decimal point.
For instance, for n=12, the output of your applet should look like the following
(the approximate values of  are not accurate)
[see next page]
Number of terms in series (n)
12
13
14
15
16
Approximate value of  ( Sum(n))
3.14159232
3.14159238
3.14159233
3.14159237
3.14159236
Files to be submitted: Tabulator.java, Tabulator.class, Tabulator.html
3- Build an applet that asks the user for a positive number epsilon smaller than 1.0
epsilon is the precision within which your applet is required to compute . Let Sum(n)
denote the sum of the first n terms of the series in Problem 3. The output of your program
should be the smallest integer n for which Sum(n) is within epsilon of the real value of
, which you can access using the static field Math.PI. More precisely, your applet
should compute and display the minimum n such that
| Sum(n) – | < epsilon
Files to be submitted: Approximator.java, Approximator.class, ApproximatorApplet.html
Preparing HTML Files and Running Applets
Applets are run differently from Java programs with main methods. They require HTML
files that refer to the class files of the applet. Let MyApplet.class be your compiled
applet class file. Prepare a file named MyApplet.html. The contents of the file must
be like the following:
<object width="500" height="500">
<param name="code" value="MyApplet.class">
</object>
The “code” parameter refers to the name of your class file. The class file and the HTML
file must be in the same directory to be located easily. To run Appletviewer with
your applet class, use the command:
appletviewer MyApplet.html
Download