CS 260 – Exam I September 21, 2012 Page 1 of 4 Name: _______KEY_____________________________ This exam is worth 100 points. There are questions totaling 130 points. You should omit questions worth 30 points. If you do not leave some questions blank or otherwise mark which ones you want to omit, I will quit grading when I have graded questions worth 100 points, so your last questions will not be graded. You may use additional paper from your instructor and staple it to your exam. 1. (20 points) Consider a class for the board game Jungle (right), similar to the Stratego game. Draw a class diagram for this class. Include at least 3 attributes and 3 methods appropriate for this class. (If you include the constructor method, you should have 4 methods.) If you like, your attributes and methods can include playing the game (moving pieces on the board, etc.) as well as the bare essentials of the board. (You do not need to know how to play the game to do this.) Class Game Board Board : Matrix or 2-D array nRows, nCols : integer (These are only examples …) Constructor Clear() Initialize() (Set up starting positions) MovePiece(row1,col1, row2, col2) PrintBoard() (Examples – could be others…) 2. (10 points) What does an iterator do? Why is it necessary to have an iterator, even when, for example, the container ADT (such as a set) doesn’t have any order of elements? The iterator gives access to all the elements of the container. It allows tests for membership (“Is x in the container?”). It allows looping over all elements so you can calculate averages or find minimal and maximal values, etc. 30 Points attempted this page: ______ 30 Cumulative points attempted: ________ Points lost this page: ______ CS 260 – Exam I September 21, 2012 Page 2 of 4 3. (20 points) Assume that you are going to include a game board for the Jungle game program you are writing, and that you will create an ADT for that game board. You need to pick out an underlying data structure to use to build your game board. (For example, you might build your board using a 1-D array, or a list of 1-D lists. Or you might build it using a set, or a dictionary, or a matrix, or …). Pick two possible data structures you might use to build your ADT. (Call them DS1 and DS2.) Then give at least two advantages of using one data structure over the other. (You can give two advantages of DS1 over DS2, or one advantage of DS1 over DS2, along with one advantage of DS2 over DS1.) a. My DS 1: 1D list of 1D lists These are only examples… b. My DS 2: Matrix c. Adv. 1: Matrix is better because … d. Adv. 2: Matrix is better because … 4. (15 points) Why do we bother to develop and use Abstract Data Types (ADTs)? Lots of code has been written without using ADTs. What advantage(s) do they provide to programmers who use them? ADTs allow us to pull together all the operations of a data object, while hiding the details of how it is actually constructed. This information hiding is very important: It allows others using the ADT to write their code without needing that knowledge, and it allows the ADT developers to go back in and fix bugs or completely change the underlying data models without requiring any changes in the client code. 35 Points attempted this page: ______ 65 Cumulative points attempted: ________ Points lost this page: ______ CS 260 – Exam I September 21, 2012 Page 3 of 4 5. Assume that you need to create a new ADT for college courses like CS 150 and Math 125, for use in a degree checker like DegreeWorks. (It could be used to answer questions like, “Has the student passed all the courses required for the BS degree?”) a. (20 points) Outline at least 4 of the basic attributes this ADT would need. (Name, data type, and description or example values.) Attribute name Data type Sentence description Course name String “CS 150” or similar strings Hours Integer 1, 2, 3, … How many hours of credit? Grade String Course grade: “A-” or “C+” or … Semester String or date “Fall, 2012” b. (20 points) Outline at least 4 methods that a “Course” ADT would need. (Name them, including parameters and return values when necessary. Write a sentence or two telling what each method does if it is not obvious from the name. The constructor is given as an example.) Name, parameters, return value Constructor( , , , Sentence description ) Creates the “Course” object, setting the attributes to a default value. CoursePassed() Predicate: Did the student pass this course or not? getGradePoints() Return grade points (hours * grade points/hour, i.e., 4.0 for A, 3.0 for B) Accessors & mutators for each attribute… 8 possibilities… checkValidity() Is the course name in the UA catalog? Right number of hours? Etc. c. (5 points) Draw a class diagram including the (Class) Course These should match the attributes in part A. attributes and methods you outlined above. Constructor These should match the methods in part B. 45 Points attempted this page: ______ 110 Cumulative points attempted: ________ Points lost this page: ______ CS 260 – Exam I September 21, 2012 Page 4 of 4 6. (10 points) Why is it important to have a constructor function? What does it do? The constructor allocates the space in memory, and sets up everything with some initial values that make sense for the ADT. This means that otherwise uninitialized integers will not have some really strange values in the (negative) millions or billions, and other data members will also have reasonable values. 7. In ADTs such as arrays and matrices, we had a method called __getitem__(self) and another method called __setitem__(self, value). a. (5 points) Explain why it was important in Python to use exactly these names. What benefit did that give us? Using these specific names (and other like them) allows Python to substitute operators for the calls to these functions. b. (5 points) Give an example of code that would call each of those functions. myArray[i] = 42 # calls __setitem__() x = myArray[i] # calls __getitem__() 20 Points attempted this page: ______ 130 Cumulative points attempted: ________ Exam points: Q1 20 Q2 10 Q3 20 Q4 15 Q5 45 Q6 10 Q7 10 Total: 130 Points lost this page: ______
© Copyright 2026 Paperzz