

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Fall 2008;
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


assertEquals(2, j / k); // a. ______ assertEquals(1, j % k); // b. ______
assertEquals('U', s.charAt(1)); // d. ______
assertEquals(2.0, Math.sqrt(j), 1e-12); // h. ______
assertEquals( "A", grade(90.0) ); // or some argument > 90. assertEquals( "B", grade(80.0) ); // or some argument > 80.0 and < 90. assertEquals( "C", grade(70.0) ); assertEquals( "D", grade(60.0) ); assertEquals( "E", grade(50.0) );
2b.
public String grade( double percentage) { String result; if (percentage >= 90.0) result = "A"; else if (percentage >= 80.0) result = "B"; else if (percentage >= 70.0) result = "C"; else if (percentage >= 60.0) result = "D"; else result = "E"; return result; }
int j = 1; int n = 5; while(j <= n) { System.out.print("Hello"); n++; } Infinite
int j = 1; while(j <= 11) { System.out.print("Hello"); j = j + 3; } 4
int n = 0; for(int j = 1; j < n; j++) { System.out.print("Hello"); } Zero
// Tricky Question for(int j = 1; j <= 11; j++); System.out.print("Hello"); 1
for (int j = -10; j <= 5555; j = j + 5) System.out.print (j + " ");
public int occurencesOf(int target, Scanner scanner) { int result = 0; while(scanner.hasNext() ) { int nextInt = scanner.nextInt(); if (nextInt == search) result++;; } return result; }
public boolean equals(String227 other) { for ( int i = 0; i < Math. min ( this .length(), other.length()); i++) { if (charAt(i) != other.charAt(i)) { // Fond 2 that are not equal return false ; } } // Reach this point if all chars were equal up to the last char of the shorter one // If both have same length (number of chars), return true return this .length() == other.length(); }
public String toString() { String result = ""; for ( int j = 0; j < n; j++) result = result + a[j] + " "; return result; }
// Searching right to left makes this easier. Also. you could use a nested loop to shift. public void removeAllOccurrencesOf( int valueToRemove) { for ( int index = 0; index < n; index++) { if (a[index] == valueToRemove) { shiftLeftFrom(index); n--; index--; } } }
private void shiftLeftFrom( int leftIndex) { for ( int i = leftIndex; i < n - 1; i++) a[i] = a[i + 1]; }