Description
1 Not Hello World (10 points)
This problem will ensure that your programming environment is functional and you remember how to compile a simple Java class. Write a program called Greeting.java that prints out a greeting. It can say anything you like, except for “Hello, world!”
$ java Greeting Bonjour, tout le monde!
2 Statistics (30 points)
Write a program called Average.java that prompts a user to enter a number, and allows the user to continue to enter numbers until she responds with a negative number. At that point, the program should print out how many numbers the user entered (not including the negative one), and the average of those numbers.
$ java Average Enter a series of numbers. Enter a negative number to quit. 1 2 4 3 5 -1 You entered 5 numbers averaging 3.0.
3 Fibonacci numbers (30 points)
The Fibonacci sequence is a famous mathematical sequence where each successive term is the sum of the two preceding ones. This can be expressed mathematically as Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. The sequence, therefore, goes 1, 1, 2 (1+1), 3 (1+2), 5 (2+3), 8 (3+5), 13 (5+8), 21, 34, 55, 89, 144… Write a program called Fib.java which allows a user to enter a number n as an argument. The program will print out the nth Fibonacci number. Hint: you will have to change the argument in the variable arg[0] from a String to an int using Integer.parseInt().
1
$ java Fib 4 3 $ java Fib 7 13 $ java Fib 11 89
4 Investigations into π (30 points)
Thirty-five digits of π are sufficient to calculate the circumference of the universe to within the size of a hydrogen nucleus. In other words, finding the value of π to ten quadrillion digits is pretty much the definition of pointless. But people seem to enjoy doing it anyhow. How? There are a number of methods, but one common approach is to discover an infinite series that converges to the correct value. Here’s one that has been around for hundreds of years, known as the Gregory series: π 4 = ∞ Pk =1 −1k+1 2k−1 = 1− 1 3 + 1 5 − 1 7 +··· Write a program called Gregory.java that takes a number k specified by the user and calculates π using the first k terms of the Gregory series. The program should print this approximate value of π, as well as the percentage difference between this value and the one provided by Java in the constant Math.PI. Hint: remember to correct for the fact that this series converges to π 4, not π itself.
$ java Gregory 10 Pi according to Gregory series: 3.0418396189294032 This differs from Java’s value by 3.175237710923643 percent. $ java Gregory 1000 Pi according to Gregory series: 3.140592653839794 This differs from Java’s value by 0.03183098066059948 percent.
Turning in
Ensure that all four of your .java programs can be compiled and run from the command line. Create a zip file called assignment 1