Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Data Structures and Algorithms with Python
< Prev   CONTENTS   Next >

1.17 Chapter Summary

In this first chapter we have covered a large amount of material which should be mostly review but probably covered some things that are new to you as well. Don't be too overwhelmed by it all. The purpose of this chapter is to get you asking questions about the things you don't understand. If you don't understand something, you should ask your teacher or someone who knows more about programming in Python. They can likely help you. Asking questions is a great way to learn and Computer Science is all about a lifetime of learning.

Here is a list of the important concepts you should have learned in this chapter.

You should:

• know how to create an object, both from a literal value and by calling the object's constructor explicitly.

• understand the concept of a reference pointing at a value (i.e. an object) in Python.

• know how to call a method on an object.

• know how to import a module.

• understand the importance of indentation in Python programs.

• know why you write a main function in Python programs and how to call the main function.

• know how to read records from a file whether they be multi-line, single line, fixed length, or variable length records.

• know how to define a container class like PyList defined in this chapter.

• understand the concept of polymorphism and how that means an object will do the right thing when a method is called.

• understand the Accumulator pattern and how to use it in a program.

• know how to implement a simple GUI using Tkinter in Python. Entry boxes, labels, buttons, frames, and event handlers should all be concepts that are understood and can be programmed by looking back at the examples in this chapter.

• and finally you should know how to read and write XML files in your programs.

There is a lot of example code in this chapter and the final version of the Draw program is provided on the text's website or in Sect. 20.1. While it is doubtful you will be able to memorize each line of the code you found in this chapter, you should make sure you know how things work when you look at it and you should remember that you can use this chapter as a resource. Come back to it often when you need to see how to do something in later chapters. Using this example code as a reference will help to answer a lot of your questions in future chapters.

1.18 Review Questions

Answer these short answer, multiple choice, and true/false questions to test your mastery of the chapter.

1. What does IDE stand for and why is it a good idea to use an IDE?

2. What code would you write to create a string containing the words Happy Birthday!? Write some code to point a reference called text at that newly created object.

3. What code would you write to take the string you created in the last question and split it into a list containing two strings? Point the reference lst at this newly created list.

4. What code would you write to upper-case the string created in the second question. Point the reference named bDayWish at this upper-cased string.

5. If you were to execute the code you wrote for answering the last three questions, what would the string referenced by text contain after executing these three lines of code?

6. How would you create a dictionary that maps “Kent” to “Denise” and maps “Steve” to “Lindy”? In these two cases “Kent” and “Steve” are the keys and “Denise” and “Lindy” are the values.

7. Consult Chap. 17. How would you map a key to a value as in the previous problem when the dictionary was first created as an empty dictionary? HINT: This would be called setting an item in the documentation in the appendix. Write a short piece of code to create an empty dictionary and then map “Kent” to “Denise” and “Steve” to “Lindy”.

8. What method is called when x < y is written? In which class is the method a member? In other words, if you were presented with x < y in a program, how

would you figure out which class you needed to examine to understand exactly what x < y meant?

9. What method is called when x " y is written?

10. What is the loop and a half problem and how is it solved?

11. Do you need to use the solution to the loop and a half problem to read an XML file? Why or why not?

12. Polymorphism and Operator Overloading are closely related concepts. Can you briefly explain how the two concepts are similar and how Python supports them? HINT: No is not a valid answer.

13. What would you write so that a program asks the user to enter an integer and then adds up all the even integers from 2 to the integer entered by the user. HINT: You might want to review how to use the range function to accomplish this and decide on what pattern of programming you might use.

14. How do you create a window using Tkinter?

15. What is the purpose of a Frame object in a Tkinter program?

16. What are three types of widgets in the Tkinter framework?

17. When reading an XML file, how many lines of code does it take to read the file?

18. How do you get a single element from an XML document? What line(s) of code do you have to write? Provide an example.

19. When traversing an XML document, how do you get a list of elements from it? What line(s) of code do you have to write? Provide an example.

20. What is an attribute in an XML document and how do you access an attribute's value? Provide an example from the text or from another example you find online.

1.19 Programming Problems

1. Starting with the version of the Draw program that reads an input file with variable length records, add a new graphics command of your choice to the program. Consider how it would be written to a file, create a test file, write your code, and test it. You must design two files: a sample test file, and the program itself. Some examples might be a graphics command to draw a star with some number of points, a rectangle with a height and width, etc.

2. Starting with the Draw program provided in Sect. 20.1, extend the program to include a new button to draw a new shape for the Draw program. For instance, have the draw program draw a star on the screen or a smiley face or something of your choosing. HINT: If you use the forward and back methods to draw your shape, you can scale it by multiplying each forward and back amount by a scale value. Then, you can let the user pick a scale for it (or use the radius amount as your scale) and draw your shape in whatever size you like. To complete this exercise you must extend your XML format to include a new graphics command to store the relevant information for drawing your new shape. You must also define a new graphicsCommand class for your new shape.

3. Add the ability to draw a text string on a Draw picture. You'll need to let the user pick a point size. For a real challenge, let the user pick the font type from a drop-down list of font types. Draw a string that you have the user enter in an entry box.

4. Find an XML document of your choice on the internet, write code to parse through the data and plot something from that data whether it be some value over time or something else. Use turtle graphics to plot the data that you find.

5. Add a new button to the drawing program presented in Sect. 20.1 that draws a rainbow centered above the current location of the turtle. This can be done quite easily by using sin and cos (i.e. sine and cosine). The sin and cos functions take radians as a parameter. To draw a rainbow, the radians would range from 0 to math.pi from the math module. You must import the math module to get access to math.cos and math.sin as well as math.pi. To draw values in an arc, you can use a for loop and let a variable, i, range from 0 to 100. Then radius * math.cos(i/100.0

* math.pi), radius * math.sin(i/100.0 * math.pi) is the next x,y coordinate of the rainbow's arc. By varying the radius you will get several stripes for your rainbow.

Each stripe should have a different color. To vary the color, you might convert a 24-bit number to hex. To convert a number to hexadecimal in Python you can use the hex function. You must make sure that your color string is 6 digits long and starts with a pound sign (i.e. #) for it to be a valid color string in Python.

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel