Assignment #2

Due: Saturday, May 29

From the textbook, do Exercises 5.16, 6.15, 6.26, 6.40 (also note extra instructions below).



Filenames should be

To do these exercises, you will need the MyInput class that was used in lecture and in HW 1. You actually do not need to paste a full copy of this file into your other program files. If you have a copy of MyInput.java in the same directory with your other program files, your compilations should work. (I'll demonstrate this in class).

Extra Instructions

  1. For 5.16, start by asking the user how many terms of the series to compute to, and then let the user enter the information. Use this to print a table of the first N terms of the series (where N is the data entered by the user). Assume the user's input will be a non-negative integer. (see sample output)
     
  2. For 6.15, note that the method's name is to be hypotenuse, and it takes two parameters of type double and returns a double. Also, disregard instructions about applets. Instead, write a test program (a main method) that asks the user to input two sides of a right triangle. Send this data into the method and print out the returned answer.
     
  3. For 6.26, ignore instructions about applets. The method's name should be prime, it should take in an integer, and it should return a boolean. A return of true means that the value is a prime number. A return of false means that it isn't.

    In addition to the method, write a main method that asks the user to input a positive integer N. Using your prime() method, find and print all the prime numbers less than or equal to N, 10 numbers per line.
     

  4. For 6.40, ignore the instruction about an applet. The algorithm given in the problem is a recursive algorithm for computing the GCD called Euclid's algorithm. You may also use Dijkstra's algorithm if you wish. Both are listed below. Your method should be called gcd. It takes two integer parameters, and returns the GCD as an integer.

    Also write a sample main program that asks the user to input two positive integers and then calls the GCD method and prints the result. (Note: positive number means > 0).
     

  5. For all three exercises involving methods, declare your methods to be public and static. You may also use the java.lang.Math class in these programs, where needed. You may assume correct user input in these problems.

Sample Outputs

Sample user input is underlined.

Sample output for 5.16

 Exercise 5.16: 'Approximating PI'
 Compute to how many terms of the series? 20
 terms   PI approximation
 1       4.0
 2       2.666666666666667
 3       3.466666666666667
 4       2.8952380952380956
 5       3.3396825396825403
 6       2.9760461760461765
 7       3.2837384837384844
 8       3.017071817071818
 9       3.2523659347188767
 10      3.0418396189294032
 11      3.232315809405594
 12      3.058402765927333
 13      3.2184027659273333
 14      3.0702546177791854
 15      3.208185652261944
 16      3.079153394197428
 17      3.200365515409549
 18      3.0860798011238346
 19      3.1941879092319425
 20      3.09162380666784

Sample output for 6.15 - run 1

 Exercise 6.15: 'hypotenuse'
 Enter first leg of right triangle: 3
 Enter second leg of right triangle: 4
 Length of hypotenuse = 5.0

Sample output for 6.15 - run 2

 Exercise 6.15: 'hypotenuse'
 Enter first leg of right triangle: 10.5
 Enter second leg of right triangle: 16
 Length of hypotenuse = 19.13765920900464

Sample output for 6.26

 Exercise 6.26: 'Primes'
 Please input a positive number: 500
 The prime numbers less than or equal to 500 are:
 2       3       5       7       11      13      17      19      23      29
 31      37      41      43      47      53      59      61      67      71
 73      79      83      89      97      101     103     107     109     113
 127     131     137     139     149     151     157     163     167     173
 179     181     191     193     197     199     211     223     227     229
 233     239     241     251     257     263     269     271     277     281
 283     293     307     311     313     317     331     337     347     349
 353     359     367     373     379     383     389     397     401     409
 419     421     431     433     439     443     449     457     461     463
 467     479     487     491     499

Sample output for 6.40

 Programming Exercise 6.40 'GCD recursive'
 Input first number : 45
 Input second number : 10
 GCD(45, 10) = 5

Compiling

Remember that the compile command is "javac", at the unix command prompt. Compile your code on program.cs.fsu.edu, and then run your programs with the "java" command.

Submitting:

As with assignment 1, pack up your files on program.cs.fsu.edu with the tar utility. Here's the command:
  tar cvf hw2.tar Ch5_16.java Ch6_15.java Ch6_26.java Ch6_40.java 
Or, if you have all your .java files for this one in a single directory:
  tar cvf hw2.tar *.java
E-mail this tar file to me (myers@cs.fsu.edu) by the due date. Your e-mail subject should be HW2-SUBMIT. Include your name and section in the e-mail body. Please only submit ONCE, unless you make a mistake and need to correct it (before the due date) -- and minimize this. Only the last submission will be graded.

Suggestion

The easiest way to mail a file from your CS account is to send the e-mail directly from your CS address. To do this, log onto shell.cs.fsu.edu, and type "pine" to open the pine e-mail program. This is an easy e-mail program to use (from the unix environment). It's very user friendly and has good help files. I will demonstrate how to attach a file from your account to an e-mail in class.