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
Practice Exam 1. printf What is the output of the following program? Use the table below to show the appropriate spacing. The first line of output is already given, as are the first two characters of each subsequent line; you should show the rest of the output for the other 6 lines (A thru F). public class Test1Problem5 { public static void main(String[] args) { System.out.printf(" 1234567890\n"); System.out.printf("A.%8d\n", 125); System.out.printf("B.%-10d\n", 125); System.out.printf("C.%4d%d\n", 125, 277); System.out.printf("D.%6.2f%4d\n", 1.256, 277); System.out.printf("E.%7.0f\n", 5.76); System.out.printf("F.%1d %2d %3d\n", 1, 2, 3); } } 1 CSS 161 A . B . C . D . E . F . 1 2 3 4 5 6 7 8 1 2 5 7 2 5 1 2 5 2 7 1 . 2 6 2 9 0 7 7 6 1 2 3 Page 1 2. Classes Define a class, TestList, with the following members: Two instance variable: 1) an array of integers to hold the test scores, 2) an integer variable used to indicate the current number of tests. A constructor that initializes the array elements to 0 and the integer variable to 0. public TestList() A void method that accepts a single int and adds this to the list. public void add(int newScore) A “toString()” method that returns a single string containing every test score in the list public String toString() A boolean method that return whether the provided test score passed as a parameter is in the list. public boolean contains(int testScore) A method called “sum” that adds up and returns the sum of all of the numbers currently in the list public int sum() Note that all methods in this class should only calculate and return values, they should not print anything. The TestList can never grow beyond 10 scores. No comments required. Write a Driver class, TestListDriver, that creates a TestList object and adds 100, 90, and 72 to the list. Then print out the TestList array. The output should be: Scores: 100 90 72 Sum: 262 public class TestList { private int [ ] testScores= new int[10]; private int count; public TestList() { for (int i = 0; i < 10; i++) testScores[i] = 0; count = 0; } CSS 161 Page 2 public void add(int score ) { testScores[count] = score; count++; } public String toString( ) { String strOfScores = "" ; for ( int i = 0; i < count; i++ ) strOfScores = strOfScores + " " + testScores[i]; return strOfScores; } public boolean contains( int testScore) { for ( int i = 0; i < count; i++ ) if ( testScore == testScores[i] ) return true; return false; } public int sum() { int sum = 0; for ( int i = 0; i < count; i++ ) sum += testScores[i]; return sum; } } public class TestListDriver { public static void main (String [] args) { TestList tList = new TestList( ); tList.add(100); tList.add(90); tList.add(72); System.out.println("Scores: " + tList); System.out.println("Sum: " + tList.sum() ); } } CSS 161 Page 3 3. String methods What is the output of the following program? [There are no syntax errors.] public class FinalExamStringTest { public static void main(String[] args) { String s = "March madness"; System.out.printf("a. %d\n", s.length()); System.out.printf("b. %s\n", s.charAt(1)); System.out.printf("c. %d\n", s.indexOf("m")); System.out.printf("d. %d\n", s.lastIndexOf("a")); System.out.printf("e. %d\n", s.indexOf("A")); } } a. 13 b. a c. 6 d. 7 e. -1 4. for loops/if statements What is the output of the following program? [There are no syntax errors.] public class FinalExamIfTest1 { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.print(i); if (i % 2 == 0) System.out.print(" A"); if (i >= 2) System.out.print(" B"); System.out.println(); } } } 0 1 2 3 4 A A B B A B CSS 161 Page 4 5. For loops What is the output of the following program? [There are no syntax errors.] public class FinalExamForTest { public static void main(String[] args) { for (int i = 0; i < 4; i++) { for (int j = 5; j > i ; j--) System.out.print(" "); System.out.print("*"); for (int j = 0; j < i; j++) System.out.print(" "); System.out.println("*"); } } } 1 2 3 4 5 6 7 * * * * * 8 9 10 * * * 6. Arrays Code a method called addArray( ) that takes two parameters: the first for an array of base type int, and the second for an int. The method adds to each array element the second parameter if the value of the element is even and subtracts the second parameter if the value of the element is odd; stores each result in another array of base type int; and returns this new array. Make it a static method. public static int [ ] addArray(int [ ] array, int number) { int [ ] newArray = new int[length.array]; for ( int i = 0; i < array.length; i++) if (array[i] % 2 == 0) newArray[i] = array[i] + number; else newArray[i] = array[i] - number; return newArray; } CSS 161 Page 5 7. Arrays Write a static method, oddEvenCount( int[ ] array), that takes as a parameter an integer array and returns a new array (2 elements) that contains the number of even and odd values in the array. You may assume that the array has at least one element. Count zero as an even number. See the following table for example input parameter and return counts. array evenOddCount(array {7, 4, 35, 2, 5, 4, 2, 3} [4, 4] {25, -1, 1, 2, -1, 3, 4, 1, -1} [2, 7] {100, 2, 56, 0, 2} [5, 0] public static int [] oddEvenCount(int [] array) { int oddCount = 0; int evenCount = 0; int [ ] newArray = new int[2]; for ( int i = 0; i < array.length; i++) if (array[i] % 2 == 0) evenCount++; else oddCount++; newArray[0] = evenCount; newArray[1] = oddCount; return newArray; } CSS 161 Page 6 8. File IO/Arrays Write a method that opens a file containing quiz scores and write out each unique quiz score to an output file. Open the input file (“scores.txt”) and output file (“nodups.txt”) then read each integer score from the file, printing out a list (without duplicates) to the nodups.txt. Assume there can be no more than 10 unique scores. import java.util.Scanner; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; public class FileIOArrays { public static void main(String[]args) { Scanner scoresFile = null; PrintWriter noDups = null; int[] scoresWritten = new int[10]; try { scoresFile = new Scanner(new FileInputStream("scores.txt")); } catch (FileNotFoundException e){ System.out.println("Scores file not found"); System.exit(0); } try { noDups = new PrintWriter(new FileOutputStream("nodups.txt")); } catch (FileNotFoundException e){ System.out.println("Scores file not found"); System.exit(0); } int numwritten = 0; while(scoresFile.hasNext()) { int score = scoresFile.nextInt(); boolean written = false; for(int i = 0; i < scoresWritten.length; i++){ if (score == scoresWritten[i]){ written = true; } } CSS 161 Page 7 if(!written) { noDups.println(score); scoresWritten[numwritten] = score; numwritten++; } } noDups.close(); } } 9. 2-D Arrays Write a method that is passed a two-dimensional int array (5 X 5) that counts the total number of occurrences of 2 or more consecutive integers in a given row. It returns the value as an int. See the following table for example input parameter and return count. Row 1: {5, 4, 4, 2, 1} Count: 1 Row 2: {6, 6, 7, 7, 7} Count: 2 Row 3: {3, 3, 2, 3, 3} Count: 2 Row 4: {3, 1, 2, 1, 3} Count: 0 Row 5: {3, 3, 3, 3, 3} Count: 1 Returns: 6 public class TwoDArrarys { public static void main(String[]args){ int[][] matrix = {{5,4,4,2,1}, {6,6,7,7,7}, {3,3,2,3,3}, {3,1,2,1,3}, {3,3,3,3,3} }; System.out.println("Number of consecutive number: " + countConsecutive(matrix)); } public static int countConsecutive(int[][] matrix) { int numsecs = 0; int last = -1; boolean numberBetween = true; CSS 161 Page 8 for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[i].length - 1; j++) { if( matrix[i][j] == matrix[i][j + 1] && numberBetween) { numsecs++; last = matrix[i][j + 1]; numberBetween = false; } if(matrix[i][j + 1] != last) { numberBetween = true; } } numberBetween = true; } return numsecs; } } CSS 161 Page 9