1.(25) Write a program that reads in an integer number and prints the following output as long as the number is odd and greater than 0 and less than 10. For example, if the number entered is 9, print: 1 3 3 3 5 5 5 5 5 7 7 7 7 7 7 7 9 9 9 9 9 9 9 9 9 Answer: public class Pyramid { public static void main (String args[]){ for (int i = 0; i < 10; i++) { if (i%2 != 0) { for (int j = 0; j < 20 - i/2; j++){ System.out.print(" ");} for (int k = 0; k < i; k++) System.out.print(i); } System.out.println(); } } } 2.(25) Write a recursive method called computeFN which computes the following function for values of N: f(N) = 3*f(N-3) + 4*f(N-2) - 5*f(N-1) where f(0) = 1, f(1) = 1, f(2) = 3. Answer: //import javax.swing.*; public class Test1 { public static int function(int n) { if (n==0) return 1; if (n==1) return 1; if (n==2) return 3; else return 3*function(n-3) + 4*function(n-2) - 5*function(n-1); } public static void main(String []args){ //String a = JOptionPane.showInputDialog("Enter odd number from 1 to 9"); //System.out.println("Enter odd number from 1 to 9"); int num = Integer.parseInt(args[0]); System.out.println(num); int b = function(num); System.out.println("f =" + " " + b); } } The steps of computing of f(5): 1) check n < 0 ? no, then 2) check n == 0 ? no, then 3) check n == 1 ? no, then 4) check n == 2 ? no, then 5) compute 3*f(5 – 3) + 4*f(5 – 2) – 5*f(5 – 1) Recursive calls: 1 = f(2), Compute f(3) 5.1 step 1, 2, 3, and 4 repeat 5.2 recursive calls: f(3 – 3) = 1 f(3 – 2) = 1 f(3 – 1) = 3 f(3) = 3*f(3 – 3) + 4*f(3 – 2) – 5*f(3 – 1) = -8 Compute f(4) 5.3 step 1, 2, 3, and 4 repeat 5.4 recursive calls: f(4 – 3) = 1 f(4 – 2) = 3 f(4 – 1) = f(3) = -8 f(4) = 3*f(4 – 3) + 4*f(4 – 2) – 5*f(4 – 1) = 55 f(5) = 3*f(5 – 3) + 4*f(5 – 2) – 5*f(5 – 1) = 3*f(2) +4*f(3) - 5*f(4) = 3*3 + 4*(-32) + 5*(-55) = -298 3.(20) Define a Java class, in which the queue abstract data structure (ADT) is implemented. Answer: public class As1C implements Queue { private static final int N = 10; int f; int r; Object[] q; public As1C() { f = 0; r = 0; q = new Object[N]; } public int size() { return (N - f + r) % N; } public boolean isEmpty() { return (r = f); } public Object front() throws QueueEmptyException { if (isEmpty( )) throw new QueueEmptyException("Queue is empty!"); else { return dequeue(); } } public void enqueue (Object element) throws QueueFullException { if (((N - f + r) % N) == N - 1) throw new QueueFullException("Queue is full!!"); else { r = (r + 1) % N; q[r] = element; } } public Object dequeue() throws QueueEmptyException { if (isEmpty) throw new QueueEmptyException("Queue is empty!"); else { Object temp = q[f]; f = (f + 1) % N; return temp; } } public static void main(String[] args) { As1C test = new As1C(); test.enqueue("1"); test.enqueue("2"); test.enqueue("3"); test.enqueue("4"); test.enqueue("5"); test.enqueue("6"); test.enqueue("7"); test.enqueue("8"); test.enqueue("9"); test.enqueue("10"); for (int i = 1; i <= 10; i++) System.out.println(test.front()); System.exit(0); } } 4.(30) Using Stack data structure (ArrayStack), write a JAVA program to calculate the following expression: 1+2+3+4+5+6+7+8+9. Hints: 1. String s = “1+2+3+4+5+6+7+8+9”; Using s.charAt(i) to take a character from s. Generate Integer object: Integer obj = Integer(s.charAt(i)); 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. Answer: public class As1D { public static void main(String [] args){ String s = "1+2+3+4+5+6+7+8+9"; char c = 0; int result = 0; Object temp; Stack myStack = new ArrayStack(); myStack.push(new Integer(1)); for (int i = 1; i < s.length(); i++){ if (s.charAt(i) == '+') myStack.push(new Character(s.charAt(i))); else { temp = myStack.pop(); result = ((Integer)myStack.pop()).intValue(); c = s.charAt(i); result = result + (c-'0'); myStack.push(new Integer(result));} } System.out.println("Total is " + result); } }