Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Question 1 a) Write a method find which puts the numbers that are divisible by 5 in a given two-dimensional array into a one-dimensional array and returns this array. The method will put -1 after the last element in the one-dimensional array. public static int[] find(int[][]mat){ int []ar=new int[15]; int i=0; for (int r=0;r<mat.length; r++) for (int c=0; c<mat[0].length; c++) if (mat[r][c]%5==0) { ar[i]=mat[r][c]; i++; } ar[i]=-1; return(ar); Page 1 } b) Write a main method to: Input a two-dimensional array ar1 having 10x15 elements. Call the above method to create a one-dimensional array ar2. Display the contents of the second array until -1 is reached. import java.util.Scanner; public class q1 { static Scanner kb=new Scanner(System.in); public static void main(String[] args){ int[][] ar1=new int[3][5]; int[] ar2=new int[150]; int r,c,i; for (r=0;r<ar1.length; r++) { System.out.println("Enter " + ar1[0].length + " integers: "); for (c=0; c<ar1[0].length; c++) ar1[r][c]=kb.nextInt(); } ar2=find(ar1); System.out.println("Numbers in the array divisible by 5 "); i=0; while (ar2[i]!=-1) { System.out.println(ar2[i]); i++; } } Question 2 Trace the program segments and show the output in the boxes. 2.00 4.00 3.00 9.00 4.00 16.00 i = 5 k = 11 2 public class tr3{ public static void main(String args[]) { int i=2,k; do { for(k=3; k<11; k+=4) System.out.printf("%5.2f\n", Math.pow(i,k/3)); } while(i++ < 4); System.out.println("i = " + i + " k = " + k); } } Page a. (8 pts) b. (7 pts) public class tr1{ public static void main(String args[]){ int i=1; ***3 &&&4 ***7 &&&8 ***11 12 while (i < 10) switch(i%4) { case 1: i+=2; System.out.println("*** " + i); case 2: i++; break; case 3: i+=3; default: System.out.println("&&& " + i++); } System.out.println(i); } } c. (5 pts) public class tr2{ public static void main(String args[]) { int[]a={3,5,1,6,9,4}; int i; ***3 ???0 ???2 ***9 ---4 for (i=0;i<6;i=i+2) { if (a[i] >= i) System.out.println("*** " + a[i]); if (a[i] >= a[i+1]) System.out.println("--- " + a[i+1]); else System.out.println("??? " + i); } } Page 3 } Question 3 Given the following methods: public static int m1( int[] a) { int tot = 0; for( int i = a.length – 1; i >= 0; i--) if( i % 2 == 1 || a[i] > 0) tot += a[i]; return tot; } public static boolean m2( double d) { return (int) d == (int)(d * 100) % 100; } public static int m3( int n) { int ret = 0; while( n != 0){ ret = ret + n % 10; n = n / 10; } return ret; } What is the output of the following method calls: a) int[] ar1 = {3, 4, 1, 0, -3, -8, 1, 6, 7}; a: 14 System.out.println( "a: " + m1( ar1)); b) int[] ar2 = {13, -4, 1, 10, -3, +8}; System.out.println( "b: " + m3( m1( ar2))); c) if( m2(34.43)) b: 10 c: 16 System.out.println( "c: " + m3( 123)); else System.out.println( "c: " + m3( 358)); d: false 4 System.out.println( "d: " + m2( m3( 358))); Page d) Question 4 Write a complete Java program to input an integer N from the keyboard and print N lines with random numbers between 10 – 99. It will print N random numbers in the first line and decrease by one each printed line as shown below: Sample outputs: Start you code here and continue at the back of the paper: import java.util.*; public class CS111Mt2Q4 { public static void main( String[] args){ Scanner kb = new Scanner( System.in); int N,r; System.out.print( "Enter an integer: "); N= kb.nextInt(); for( int i = 0; i < N; i++){ for( int j = N - i; j > 0; j--){ r = (int)(Math.random()*89 + 10); System.out.print( r + " "); } System.out.println(); } } Page 5 }