节哀顺便!阿门
Assignment 1
COMP 1005, Fall 04
Due Friday, January 30 by 4 pm
Total marks = 50
1. (10 marks) The value of the function y = sin x can be calculated by the infinite series
sin x = x - x3 / 3! + x5 / 5! - x7 / 7! + …
If we take the sum of the first n terms in this series, we have an approximation yn of sin x. Write a Java program that calculates y1, y2, … , yn until |yn - yn-1| is less than a given positive value, called the tolerance, and output the last approximation, i.e. yn.
This program is implemented in a class called Sin (saved in a file Sin.java). This class has two static methods. The first is a private method called sin. This method accepts two double parameters: double x and double tolerance. It returns the approximation yn (of type double) such that |yn - yn-1| < tolerance. The second method is the main method. The main method asks the user to enter the value of x and the value of tolerance, pass these values to method sin, and output the result. Use method getDouble in class Keyboard to read the input from the keyboard.
Since every term, except the first one, in the series can be calculated from the previous term, you should not calculate it from scratch. Keep a temporary variable to record the last term, and up date it in each pass of the loop.
For example, let x = 1. Then y1 = x = 1, and the last term t1 = x = 1. To calculate y2, we need to find the second term t2 = t1 * x * x / (2 * 3) = 1 / 6. y2 = y1 - t2 = 1 - 1 / 6 = 5 / 6. To find y3, we need t3 = t2 * x * x / (4 * 5) = 1 / 120. Then y3 = y2 + t3 = 5 / 6 + 1 / 120 = 101 / 120. t4 = t3 * x * x / (6 * 7) = 1 / 5040, and y4 = y3 - t4 = 4241 / 5040.
2. (10 marks) The Fibonacci sequence is defined recursively as F0 = F1 = 1, Fn = Fn-1 + Fn-2, n ³ 2. The first few terms in this sequence are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. Write a program to calculate the n-th term of this sequence. This program is implemented in a class called Fibonacci, saved in a file Fibonacci.java. This class has three static methods: The first is a private method called fibonacci. This method accepts an integer parameter n, and returns Fn. The other method is the method main. Method main asks the used to enter a nonnegative integer n, calls method fibonacci, and outputs Fn. Use a separate method to read the input. If the user enters a negative integer, your program should ask the user to re-enter it.
Method fibonacci should be implemented using a loop. (We did not talk about recursion. Even if you know recursion, you should not implement this method in a recursive way, because the recursive implementation is not efficient). Keep two local variables in this method to remember two previous terms, and use these values to calculate the next term. These local variables are updated in each pass of the loop. For example, suppose we want to calculate F5. Define local variables first to record Fn-1 and second to record Fn-2. The calculation is carried out as follows:
n Fn first second
0 1
1 1 1 1
2 2 1 2
3 3 2 3
4 5 3 5
5 8 5 8
Do not use array in this problem!
3. (10 marks) Write a program to generate the histogram of an integer array. The histogram is a diagram with vertical columns of stars to represent the values of an array of non-negative integers. For example, if the array is {2, 5, 0, 3, 6}, then the histogram looks like the following:
*
* *
* *
* * *
* * * *
* * * *
2 5 0 3 6
To make the histogram look better, there is a blank column between two consecutive star columns. This program is implemented in a class Histogram, saved in a file Histogram.java. This program has single method main. It asks the user to enter an array of non-negative integers. Use method getIntegerArray in class Keyboard to read the array, and assume that the array contains only non-negative integers. Then it displays the histogram of the array, and output the array members at the bottom of the histogram as shown in the example. Since the output can only be generated line by line from top down, you should develop an algorithm to output the histogram.
4. (10 marks) Write a program that removes duplicated characters in an array of characters. (Assume this array does not contain blanks). The program is implemented in a class called RemoveDup, saved in a file RemoveDup.java. The program asks the user to enter an array of characters without blanks (use Keyboard.getCharacterArray( ) to read the array), removes all duplicated characters, and output the array again without duplications. Do not create a new array! You should work on the input array itself. After duplicated characters are removed, the end of the array is filled in with the blank characters. You should use problem decomposition to simplify the problem using helper methods. Make sure that your program works for arrays like aaaaaaa or abaabbaaa.
10 more marks are awarded for the style, organization, and readability (including necessary comments) of your program. If a program does not compile, you will get at most 2 marks for the question depending how much is accomplished.