Starting from:
$30

$24

Assignment #1 Solution

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!”




```sh

$ java Greeting

Bonjour, tout le monde!







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.




Note: This program will use a `Scanner` object such as you learned about in CS I. This is the only occasion in this class that you are likely to use a `Scanner` object for user interaction. You will be using command-line arguments or GUI widgets in every other program you write (or, sometimes, not interacting with a user at all).




```sh

$ java Average

Enter a series of numbers. Enter a negative number to quit.

1

2

4.5

3

5

-1

You entered 5 numbers averaging 3.1.







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 F<subscriptn</subscript = F<subscript(n−1)</subscript + F<subscript(n−2)</subscript, where F<subscript1</subscript = 1 and F<subscript2</subscript = 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 a command-line argument. The program will print out the `n`th Fibonacci number.




Hint: you will have to change the argument in the variable `arg[0]` from a `String` to an `int` using `Integer.parseInt()`.







```sh

$ java Fib 4

3

$ java Fib 7

13

$ java Fib 11

89

```




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:




![Gregory Series](https://i.stack.imgur.com/ZfOyV.png)




Write a program called `Gregory.java` that takes a number `n` specified by the user (via the command line) and calculates π using the first n terms of the Gregory series. The program should print this approximate value of π, as well as the percentage error 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.




```sh

$ 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

```

```

```

More products