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
CIS 3022 Prog for CIS Majors I Print Your Name ____________________________ November 4, 2008 Exam II Your Section # ___________ Total Score ________ Your work is to be done individually. The exam is worth 104 points (four points of extra credit are available throughout the exam) and it has 12 questions. Use these method signatures to answer Question 1. #1: #2: #3: #4: #5: #6: #7: #8: public static void problem() { … } public static void problem(int a, int b) { … } public static void problem(char a, double b) { … } public static void problem(boolean a, char b) { … } public static void problem(boolean a, int b, char c) { … } public static void problem(boolean a, float b) { … } public static void problem(double a, int b) { … } public static void problem(int a, double b, char c) { … } 1. [12 pts] Given the method signatures above, state which method(s) are called, using the method numbers assigned above or none to represent no method matches the call made. problem(7, 8.72, ‘a’); Method(s) Called = ____8____ problem(true, 2, ‘c’); Method(s) Called = ____5____ problem(true, ‘a’); Method(s) Called = ____4____ problem(); Method(s) Called = ____1____ problem(3.41, 55); Method(s) Called = ____7____ problem(7, false); Method(s) Called = ___none___ 2. [3 pts] Using a pre-defined Java method, calculate and print to the screen the result of 6 raised to the power 4. System.out.println(Math.pow(6, 4)); 3. [3 pts] Using a pre-defined Java method, calculate and print to the screen the absolute value contained within the int variable value. System.out.println(Math.abs(value)); 4. [3 pts] Selecting the appropriate type, create your own variable and using a pre-defined Java method initialize your variable to the rounded value of the float decimal. int x = Math.round(decimal); 5. [3 pts] Selecting the appropriate type, create your own variable and using a pre-defined Java method initialize your variable the result of the square root of 285. double x = Math.sqrt(285); November 4, 2008 CIS 3022 Exam II Page 2 of 6 Score_______ 6. [6 pts] Describe the difference between pass by value and pass by reference. Pass by value is used for the primitive type parameters of a method. The method gets a copy of the value and cannot modify the original. Pass by reference is used for the array (and other object) parameters of a method. The method gets a reference to the original and can modify the original. 7. [10 pts] The int array values has been initialized and is given to you. Find each negative number within values and change it to zero. Leave positive numbers unchanged. Print to the screen the total of the negative numbers found. int count = 0; int x; for (x = 0; x < values.length; x++) { if (values[x] < 0) { count++; values[x] = 0; } } System.out.println(count); November 4, 2008 CIS 3022 Exam II Page 3 of 6 Score_______ 8. [10 pts] Continuing to use the int array values, create a new array containing only the non-zero numbers from within values. The new array will only be the size of the number of non-zero numbers found within values. int x; int newLength = 0; for (x = 0; x < values.length; x++) } if (values[x] != 0) { newLength++; } } int newValues = new int[newLength]; int y = 0; for (x = 0; x < values.length; x++) { if (values[x] != 0) { newValues[y++] = values[x]; } } 9. [8 pts] The two-dimensional char array letters has been initialized and is given to you. Print to the screen the total number of char positions within the entire array letters. Recall a two-dimensional array might be jagged, having a different number of columns on each row. int numPositions = 0; for (int y = 0; y < letters.length; y++) { numPositions += letters[y].length; } System.out.println(numPositions); November 4, 2008 CIS 3022 Exam II Page 4 of 6 Score_______ 10. [16 pts] Continuing to use the two-dimensional char array letters, print a representation of letters formatted into a table with row and column headings. Within the printed table, print only the value of any vowels (a, e, i, o, u) found in letters, leave the table position blank for any position holding a char other than a vowel. Print column headings up to the value of the row with the most number of columns. Consider this data and output. Row # # of Columns 0 0 1 2 3 7 5 6 3 0 1 2 3 1 e a i o Output 2 3 e e u 4 5 6 u a int max = 0; // find maximum column number for( int i = 0; i < letters.length; i++ ) if ( max < letters[i].length ) max = letters[i].length; // print column header System.out.print(" "); for( int i = 0; i < max; i++ ) System.out.print( i + " " ); System.out.println(); for( int i = 0; i < letters.length; i++ ) { System.out.print( i + " " ); // print row number for( int j = 0; j < letters[i].length; j++ ) { switch( letters[i][j] ) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.print( letters[i][j] ); break; default: System.out.print( " " ); } System.out.print(" "); } System.out.println(); } November 4, 2008 CIS 3022 Exam II Page 5 of 6 Score_______ 11. [12 pts] Create the signature of the following methods. Note, a full description of the method’s operation is provided for context, however do not implement the methods, only list their signature. a) The method power will receive two decimal values, raise the value of the first to the value of the second, and return the result. public static double power ( double a, double b ) b) The method factorial will receive an integer value, calculate the factorial of the number received, and return the factorial result. public static int factorial ( int n ) c) The method printArray will receive an array of type char, printing the values of the array to the screen, and not return anything. public static void printArray ( char a[] ) 12. [18 pts] Create the class Problems. Assume the methods power, factorial, and printArray are contained in the class Problems. Create the main method. Create a menu system allowing the user to select which of the three methods to call. Once a method is selected, allow the user to enter the information needed to call the method. Give the user an option to end the program. Until the user selects to end the program, allow the user to continue to access the methods. Print any returned results to the user. (Complete your solution on this page and the back side of this sheet.) Solution is on the next page November 4, 2008 CIS 3022 Exam II Page 6 of 6 Score_______ class Problem { public static void main ( String args[] ) { while( true ) { System.out.println("\n1) power"); System.out.println("2) factorial"); System.out.println("3) printArray"); System.out.println("4) Quit"); System.out.print("\nYour selection: "); int choice = UserInput.readInt(); if( choice == 1 ) // power { System.out.print("Please input 1st value: "); double x = UserInput.readDouble(); System.out.print("Please input 2nd value: "); double y = UserInput.readDouble(); System.out.println(x + "^" + y + " is " + power(x,y) ); } else if( choice == 2 ) // factorial { System.out.print("\nPlease input an integer: "); int n = UserInput.readInt(); System.out.println( n +"factorial is " + factorial(n) ); } else if( choice == 3 ) // print array { System.out.print("Enter array length : "); int len = UserInput.readInt(); char a[] = new char[len]; for( int i = 0; i < a.length; i++ ) { System.out.print("Enter char for #" + i + ": "); a[i] = UserInput.readChar(); } printArray( a ); } else if( choice == 4 ) break; else System.out.println("Invalid choice"); } // end while loop } // end main method }