final10CS09_key

final10CS09_key - Final Exam Solutions, Spring 2009 Prof....

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Final Exam Solutions, Spring 2009 Prof. Todd Wittman 1.) [2 points] The built-in C# SoundPlayer class was inadequate for our sound playing needs, so we introduced the WMPLib class. Give two reasons why SoundPlayer is not an ideal sound player. The SoundPlayer class can only play wav sound files and it only has one output buffer so that only one sound can be played at a time. 2.) [1 point] What does it mean for a container or variable to be polymorphic ? A polymorphic container can hold items of both the base and derived classes. 3.) [2 points] Explain when it is better to make a data field in a class protecte d rather than private . Give a specific example of data that should be protected, referring to a class we have used in this course. We should make a data field protected if the derived classes will need access to that data. As an example, our Manager class needed access to the base class Employee salary variable because managers receive a salary bonus. 4.) [2 points] Using the <algorithm> library, write a single line of C++ code that prints to the console the number of occurrences of "Homer" in a set S<string> . cout << count(S.begin(),S.end(),"Homer"); 5.) [4 points] In C#, you can sort a List of items using the Sort function. This function can take the name of a function if the comparison is not the standard operator< . Suppose we wish to sort a list of integers from smallest to largest, but with the even numbers showing first and then the odd numbers. Write a function compare so that the C# code below sorts in this manner. Random r = new Random(); List<int> numbers = new List<int>(100); for (int i=0; i<100; i++) numbers[i] = r.Next(); numbers.Sort(compare); static int compare (int x, int y) { if (x==y) return 0; if (x%2==0 && y%2==1) return 1; if (x%2==1 && y%2==0) return -1; if (x<y) return 1; if (x>y) return -1; } 6.) [4 points] The Springfield DMV uses a Vehicle base class to keep track of a vehicle's registration information. Each vehicle is given a unique registration number (reg). The DMV will register different types of vehicles (cars, trucks, motorcycles, boats) and different vehicles may require different information. For example, cars need to track fuel efficiency in miles per gallon (mpg) and trucks need to track weight in
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

final10CS09_key - Final Exam Solutions, Spring 2009 Prof....

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online