100 likes | 116 Views
Answers to Assignment #1 (Assignment due: Wed. Feb. 05, 2003). 1. What does the "plateform independence" mean? How is it implemented in Java? 2. Summarize the basic concepts used in Java, such as class, method, variable, Constructor, ... (as many as possible)
E N D
Answers to Assignment #1 (Assignment due:Wed. Feb. 05, 2003) 1. What does the "plateform independence" mean? How is it implemented in Java? 2. Summarize the basic concepts used in Java, such as class, method, variable, Constructor, ... (as many as possible) 3. 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
import java.lang.*; public class as1q3 { public static void main(String args[]){ int j=5; int a=Integer.parseInt(args[0]); int num=1; int line=1; int i; if(a%2==1 && a>0 && a<10) { while(num<=a) { for(i=1; i<=(j-line); i++) { System.out.print(" "); } for(i=(j-line+1); i<=(j+line-1); i++) { System.out.print(num); } System.out.println(); ++line; num+=2; } } } }
4. 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.
import java.lang.*; public class as1q4 { public static int computeFN(int N) { int result_0 = 1; int result_1 = 1; int result_2 = 3; int result = 0; if(N<0){ System.out.println("N can not be negative !");} switch(N) { case 0: result = result_0; break; case 1: result = result_1; break; case 2: result = result_2; break; } if(N>2){ result = 3*computeFN(N-3) + 4*computeFN(N-2) - 5*computeFN(N-1); } return result; } public static void main(String arg[]) { int result; int N=Integer.parseInt(arg[0]); result=computeFN(N); System.out.println("N = "+N+" F("+N+") = "+result); } }
5. Implement ”quick sort" and sort a sequence containing 20 integers: 3, 4, 6, 1, 10, 9, 5, 20, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 15. Trace 10 steps of the computation. import java.lang.*; public class as1q5 { public static int count_steps = 1; public static void sort(int[] a, int from, int to) { if((a==null)||(a.length<2)||(to-from<=0)) return; int i=from, j=to; int center=a[(from+to)/2]; i--; j++; do { i++; j--; while((i<to)&&(a[i]<center)) i++; while((j>from)&&(a[j]>center)) j--; if(i<j) { int tmp=a[i]; a[i]=a[j]; a[j]=tmp; //print the result of each step }
} while(i<=j); if(from<j){ sort(a,from,j); } if(i<to){ sort(a,i,to); } } public static void main(String args[]) { // Create an array to hold numbers int nums[]={3,4,6,1,10,9,5,20,19,18,17,2,1,14,13,12,11,8,16,15}; //print the input array for(int k=0; k<nums.length; k++) System.out.print(nums[k]+", "); System.out.println(" *INPUT*"); sort(nums,0,(nums.length-1)); } }
i j Trace: 3, 4, 6, 1, 10, 9, 5, 20, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 15 3, 4, 6, 1, 10, 9, 5, 15, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 20 3, 4, 6, 1, 10, 9, 5, 15, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 20 3, 4, 6, 1, 10, 9, 5, 15, 16, 18, 17, 2, 1, 14, 13, 12, 11, 8, 19, 20 3, 4, 6, 1, 10, 9, 5, 15, 16, 18, 17, 2, 1, 14, 13, 12, 11, 8, 19, 20 3, 4, 6, 1, 10, 9, 5, 15, 16, 8, 17, 2, 1, 14, 13, 12, 11, 18, 19, 20 3, 4, 6, 1, 10, 9, 5, 15, 16, 8, 17, 2, 1, 14, 13, 12, 11, 18, 19, 20
i i j j 3, 4, 6, 1, 10, 9, 5, 15, 16, 8, 17, 2, 1, 14, 13, 12, 11 3, 4, 6, 1, 10, 9, 5, 15, 11, 8, 17, 2, 1, 14, 13, 12, 16 3, 4, 6, 1, 10, 9, 5, 15, 11, 8, 17, 2, 1, 14, 13, 12, 16 3, 4, 6, 1, 10, 9, 5, 15, 11, 8, 12, 2, 1, 14, 13, 17, 16 3, 4, 6, 1, 10, 9, 5, 15, 11, 8, 12, 2, 1, 14, 13, 17, 16 3, 4, 6, 1, 10, 9, 5, 15, 11, 8, 12, 2, 1, 14, 13 3, 4, 6, 1, 10, 9, 5, 13, 11, 8, 12, 2, 1, 14, 15 3, 4, 6, 1, 10, 9, 5, 13, 11, 8, 12, 2, 1, 14, 15
i j 3, 4, 6, 1, 10, 9, 5, 13, 11, 8, 12, 2, 1, 14 3, 4, 1, 1, 10, 9, 5, 13, 11, 8, 12, 2, 6, 14 3, 4, 1, 1, 10, 9, 5, 13, 11, 8, 12, 2, 6, 14 3, 4, 1, 1, 2, 9, 5, 13, 11, 8, 12, 10, 6, 14 3, 4, 1, 1, 2, 9, 5, 13, 11, 8, 12, 10, 6, 14 3, 4, 1, 1, 2, 5, 9, 13, 11, 8, 12, 10, 6, 14 3, 4, 1, 1, 2, 5 1, 4, 1, 3, 2, 5 1, 4, 1, 3, 2, 5
i i j j 1, 1, 4, 3, 2, 5 1, 1 1, 1 1 1