




























































































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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Head first Java - Java programming book
Typology: Study notes
1 / 442
This page cannot be seen from the preview
Don't miss anything!
Beijing • Cambridge • Farnham • Kln • Sebastopol • Taipei • Tokyo
Head First Programming
Wouldn‛t it be dreamy if there were an introductory programming book that didn‛t make you wish you were anywhere other than stuck in front of your computer writing code? I guess it‛s just a fantasy... Paul Barry David Griffiths
Head First Programming by Paul Barry and David Griffiths
Copyright © 2009 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( safari.oreilly.com ). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
November 2009: First Edition.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Head First series designations, Head First Programming , and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
No surfers or snakes were harmed in the making of this book.
TM This book uses RepKover™, a durable and flexible lay-flat binding.
Aideen (future singer/songwriter)
Paul
Deirdre (super mom)
Aaron and Joseph (real-life surfer dudes)
Dawn (without whom the book would never have been finished)
the authors
Authors of Head First Programming
Paul Barry
David Griffiths
http://twitter.com/dgriffiths
table of contents
Table of Contents (Summary)
Table of Contents (the real thing)
while here your brain is doing you a favor by making sure the learning doesn’t stick. Your brain’s thinking, “Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea.” So how do you trick your brain into thinking that your life depends on knowing Programming?
Intro
Intro xxiii 1 Starting to Code: Finding Your Way 1 2 Textual Data: Every String Has Its Place 37 3 Functions: Let’s Get Organized 77 4 Data Files and Arrays: Sort It Out 113 5 Hashes and Databases: Putting Data in Its Place 145 6 Modular Programming: Keeping Things Straight 177 7 Building a Graphical User Interface: Going All Gooey 215 8 GUIs and Data: Data Entry Widgets 257 8 1/2 Exceptions and Message Boxes: Get the Message? 293 9 Graphical Interface Elements: Selecting the Right Tool 313 10 Custom Widgets and Classes: With an Object in Mind 349 i Leftovers: The Top Ten Things (We Didn’t Cover) 385
table of contents
Every string has its place
All programs process data, and one of the most important types of data is text. In this chapter, you’ll work through the basics of textual data. You’ll automatically search text and get back exactly what you’re looking for. Along the way, you’ll pick up key programming concepts such as methods and how you can use them to bend your data to your will. And finally, you’ll instantly power up your programs with the help of library code.
textual data
Your new gig at Starbuzz Coffee 38 Here’s the current Starbuzz code 39 The cost is embedded in the HTML 41 A string is a series of characters 41 Find characters inside the text 42 But how do you get at more than one character? 43 The String Exposed 48 Beans’R’Us is rewarding loyal customers 50 Searching is complex 52 Python data is smart 54 Strings and numbers are different 64 The program has overloaded the Beans’R’Us Server 67 Time... if only you had more of it 68 You’re already using library code 69 Order is restored 74 Your Programming Toolbox 75
table of contents
Starbuzz is out of beans! 78 What does the new program need to do? 79 Don’t duplicate your code... 81 Reuse code with functions 82 Always get things in the right order 84 Return data with the return command 87 Use the Web, Luke 93 The function always sends the same message 94 Use parameters to avoid duplicating functions 96 Someone decided to mess with your code 102 The rest of the program can’t see the password variable 104 When you call a function, the computer creates a fresh list of variables 105 When you leave a function, its variables get thrown away 106 Starbuzz is fully stocked! 110 Your Programming Toolbox 111
Let’s get organized
functions
And complex code can be hard to read, and even harder to maintain. One way of managing this complexity is to create functions. Functions are snippets of code that you use as needed from within your program. They allow you to separate out common actions , and this means that they make your code easier to read and easier to maintain. In this chapter, you’ll discover how a little function knowledge can make your coding life a whole lot easier.
table of contents
Who won the surfing contest? 146 Associate the name with the score 150 Associate a key with a value using a hash 153 Iterate hash data with for 154 The data isn’t sorted 158 When data gets complex 160 Return a data structure from a function 164 Here’s your new board! 168 Meanwhile, down at the studio... 169 The code remains the same; it’s the function that changes 170 TVN’s data is on the money! 174 Your Programming Toolbox 175
Putting data in its place
hashes and databases
Programming languages come with other data-arranging goodies too, and our chosen tool, Python, is no exception. In this chapter, you’ll associate values with names using a data structure commonly called the hash (better known as dictionary to Python-folk). And when it comes to working with stored data , you’ll read data from an external database system as well as from regular text-based files. All the world’s awash with data, so turn the page and start applying your ever- expanding programming skills to some cool data-processing tasks.
table of contents
Keeping things straight
And, although sharing is good, you need to be careful. One programmer might take your code and use it in an unexpected way, while another might change it without even letting you know. You might want to use one function in all your programs and, over time, that function’s code might change to suit your needs. Smart programmers take advantage of modular programming techniques to keep their workload manageable.
modular programming
Head First Health Club is upgrading some systems 178 The program needs to create a transaction file 179 Use strings to format strings 180 The Format String Exposed 186 A late night email ruins your day 187 $50,000... for a donut?! 188 Only the sales from your program were rejected 189 The new bank uses a new format 190 Your coffee bar program still uses the old format 191 Don’t just update your copy 192 So how do you create a module...? 193 The transaction file is working great, too 199 The health club has a new requirement 200 The Starbuzz code 205 The two discount functions have the same name 206 Fully Qualified Names (FQNs) prevent your programs from getting confused 207 The discounts get the customers flooding in 213 Your Programming Toolbox 214
table of contents
Data entry widgets
Almost all GUI applications need to read user data, and choosing the right widgets can change your interface from data entry hell to user heaven. Widgets can accept plain text, or just present a menu of options. There are lots of different widgets out there, which means there are lots of choices, too. And, of course, making the right choice can make all the difference. It’s time to take your GUI program to the next level.
guis and data
Head-Ex needs a new delivery system 258 They’ve already designed the interface 259 Read data from the GUI 260 The Entry and Text widgets let you enter text data into your GUI 261 Read and write data to text fields 262 Large Text fields are harder to handle 263 One of the Head-Ex deliveries went astray 270 Users can enter anything in the fields 271 Radio buttons force users to choose a valid depot 272 Creating radio buttons in tkinter 273 The radio buttons should work together 275 The radio buttons can share a model 276 The system tells the other widgets when the model changes 277 So how do you use models in tkinter? 278 Head-Ex’s business is expanding 282 There are too many depots on the GUI 283 An OptionMenu lets you have as many options as needed 284 The model stays the same 285 Things are going great at Head-Ex 291 Your Programming Toolbox 292
Look, I don‛t care what you guys do, I‛m gonna stay selected.
Yeah, me too.
Huh, and me.
table of contents
What’s that smell? 294 Someone changed the file permissions 295 When it couldn’t write to the file, the program threw an exception 296 Catch the exception 297 Watch for exceptions with try/except 298 There’s an issue with the exception handler 302 A message box demands attention 303 Creating message boxes in Python 304 Your Programming Toolbox 311
Get the message?
exceptions and message boxes
There will always be things beyond your control. Networks will fail. Files will disappear. Smart coders learn how to deal with those kinds of errors and make their programs recover gracefully. The best software keeps the user informed about the bad things that happen and what should be done to recover. By learning how to use exceptions and message boxes , you can take your software to the next level of reliability and quality.