Assignment #1
Download
Report
Transcript Assignment #1
Assignment #1
(Assignment due: Wed., Feb. 04, 2015)
First, download all the java programs on my home page
(http://www.uwinnipeg.ca/~ychen2/AlgorithmnNotes/program-list.html) to your computer
and store them in one folder.
1.(25) Write a program that reads in an odd integer number i (1 i 9) and shows a pyramid
as illustared below . For example, if the number entered is 9, print:
1
333
55555
7777777
999999999
2.(25) Write a recursive method which computes the following function:
f(N) = 3*f(N-4) + 4*f(N-3) - 5*f(N-2),
where f(x) = 0 for x<0, f(0) = 1, f(1) = 1,f(2) = 3.
Give th steps of computing f(5).
Assignment #1
3.(20) Define a Java class, in which the queue abstract data structure (ADT) is implemented.
4.(30) Write a JAVA program to calculate the following expression:
1+2+3+4+5+6+7+8+9.
It is required to use Stack data structure (ArrayStack) to do the computation.
Hints:
1.
String s = “1+2+3+4+5+6+7+8+9”;
2.
Using s.charAt(i) to take a character from s.
3.
Generate Integer object:
Integer obj = Integer(s.charAt(i));
4.
Generate Char object:
Character obj = Character(s.charAt(i));
5.
Since each entry in the stack (ArrayStack) is an instance of Object class, remember
type-casting when you take an entry from the stack.