Program Design and Development - Solution Key for Quiz | C SC 227, Quizzes of Computer Science

Material Type: Quiz; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-h3c-1
koofers-user-h3c-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227 Practice Quiz SL _______________ Name ___________________________ 50pts
1. Complete the output that would be generated by this code (8pts)
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(23);
list.add(-5);
list.add(-5);
list.add(12);
Collections.sort(list);
for (Integer theNextInt : list)
System.out.print(theNextInt + " ");
System.out.println("=================");
System.out.println(list.contains(-5));
System.out.println(list.contains(9999));
System.out.println(Collection.min());
System.out.println(Collections.binarySearch(list, 23));
System.out.println("=================");
Collections.reverse(list);
for (Integer theNextInt : list)
System.out.print(theNextInt + " ");
2. Complete the code that reads all integers from the file named "numbers" (assume it's found) and prints
how many integers were found (assume numbers only has integers) and the range. The output should be
Count: 18
Range: 17
the file "numbers"
try {
Scanner scanner = new Scanner(new File("numbers");
}
catch (FileNotFoundException fnfe) {
System.out.println("the file 'numbers' was niot found");
}
int range;
int count
// Complete the code
System.out.println("Count: " + count);
System.out.println("Range: " + range);
Add output here
=================
=================
4 -3 2 1
9 11
6 14 9 12 9
-1 0 5 6
0 1 7
pf3

Partial preview of the text

Download Program Design and Development - Solution Key for Quiz | C SC 227 and more Quizzes Computer Science in PDF only on Docsity!

C Sc 227 Practice Quiz SL _______________ Name ___________________________ 50pts

1. Complete the output that would be generated by this code (8pts)

ArrayList list = new ArrayList(); list.add(12); list.add(23); list.add(-5); list.add(-5); list.add(12); Collections. sort (list); for (Integer theNextInt : list) System. out .print(theNextInt + " "); System. out .println("================="); System. out .println(list.contains(-5)); System. out .println(list.contains(9999)); System. out .println(Collection.min()); System. out .println(Collections. binarySearch (list, 23)); System. out .println("================="); Collections. reverse (list); for (Integer theNextInt : list) System. out .print(theNextInt + " ");

2. Complete the code that reads all integers from the file named "numbers" (assume it's found) and prints

how many integers were found (assume numbers only has integers) and the range. The output should be

Count: 18 Range: 17 the file "numbers"

try {

Scanner scanner = new Scanner(new File("numbers"); } catch (FileNotFoundException fnfe) { System. out .println("the file 'numbers' was niot found"); } int range; int count // Complete the code System.out.println("Count: " + count); System.out.println("Range: " + range);

Add output here

3. Complete method intersection so it returns a TreeSet that only has the elements contained in both sets.

The following assertions must pass. (12pts)

@Test public void testIntersectionToReturnNONEmptySet() { TreeSet ts1 = new TreeSet(); ts1.add(1); ts1.add(3); ts1.add(2); ts1.add(4); TreeSet ts2 = new TreeSet(); ts2.add(8); ts2.add(2); ts2.add(4); ts2.add(6); ts2.add(0); // Result should only have 2 and 4 since the // intersection of {1,2,3,4} and {0,2,4,6,8} is {2,4} TreeSet result = intersection (ts1, ts2); Iterator itr = result.iterator(); assertEquals (2, itr.next()); assertEquals (4, itr.next()); assertFalse (itr.hasNext()); }

4. On the next page, complete the program to maintain the average wind speed reading and the total number of

readings until the user closes the window. It should look like the GUI to the left at startup and the GUI to the

right after the user enters the integers 5, 2, and 7 into the text field to the right of Wind Speed (12 pts).

You will need to add instance variables, extra code to the constructor, and a private inner class. Write your

answers in the boxes. (18pts)