Computer Science II 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 that prints out a greeting. It can say anything you like, except for \Hello, world!”


  • java Greeting Bonjour, tout le monde!


  • Statistics (30 points)


Write a program called 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).


$ java Average


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














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 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 which allows a user to enter a


number n as a command-line 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().


  • java Fib 4




  • java Fib 7




  • java Fib 11




  • Investigations into (30 points)


Thirty- ve digits of are su cient to calculate the circumference of the universe to within the size of a hydrogen nucleus. In other words, nding the value of to ten quadrillion digits is pretty much the de nition 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 in nite series that converges to the correct value. Here’s one that has been around for hundreds of years, known as the Gregory


series: 1 (  1)k+1  
4 = 2k 1 = 1   3 + 5    7 +

Write a program called that takes a number n speci ed by the user (via the command line) and calculates using the rst 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.


$ 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 le called assignment 1, containing these four .java les. Upload it to the Dropbox at This assignment is due Wednesday, August 30, at noon.















error: Content is protected !!