Introduction to Programming: Java Learning to Program Robotically INSTRUCTOR: Craig Duckett EMAIL: [email protected] Computer Programming What is Computer Programming ? Computers don't do anything without someone telling them what to do—much like the average middle school student. To make the computer do something useful, you must give it instructions in either of the following two ways: Write a program that tells a computer what to do—step by step—much as you write out a recipe. Buy a program that someone else has already written that tells the computer what to do. Ultimately, to get a computer to do something useful, you (or somebody else) must write a program. A program does nothing more than tell the computer how to accept some type of input, manipulate that input, and spit it back out again in some form that humans find useful. Programming is Problem-Solving! Essentially, a program tells the computer how to solve a specific problem. Because the world is full of problems, the number and variety of programs that people can write for computers is practically endless. But to tell a computer how to solve one big problem, you usually must tell the computer how to solve a bunch of little problems that make up the bigger problem. If you want to make your own video game, for example, you need to solve some of the following problems: Determine how far to move a figure (such as a car, a spaceship, or a man) on-screen as. Detect whether the figure bumps into a wall, falls off a cliff, or runs into another figure on-screen. Make sure that the figure doesn't make illegal moves, such as walking through a wall. Draw the terrain surrounding the cartoon and make sure that if the figure walks behind an object such as a tree, the tree realistically blocks the figure from sight. Determine whether bullets that another figure fires are hitting the player's figure. If so, determine the amount of damage, how it affects the movement of the damaged figure, and how the damage appears on-screen. The simpler the problem is that you need to solve, the more easily you can write a program that tells the computer how to work. A program that displays a simple Ping-Pong game is much easier to write than a program that displays World War II fighter airplanes firing machine guns and dropping bombs on moving tanks while dodging anti-aircraft fire in a smoke-filled sky. Programming isn’t Difficult, but Time-Consuming Programming really isn't that difficult or mysterious. If you can write step-by-step instructions directing someone to your house, you can write a program. The hardest part about programming is identifying all the little problems that make up the big problem that you're trying to solve. Because computers are completely mindless, you need to tell them how to do everything and also how to continue when something unexpected happens. This means thinking through many “what if” scenarios. Like Philosophers and other wooly thinkers, Programmers are constantly asking questions and considering all the various ifs, ands, and buts of logic and decision-making. “What if…?” Philosophers & Programmers What do they have in common besides pondering and logic? Everybody else in the world Philosophers & Programmers Still … Even though most of us practice philosophical thinking and the components of logic and decision-making hundreds of times a day … … we just don’t do it for a living or make a career out of it. And why? Most people are not “cut out” to be philosophers or programmers. Most people don’t “get” whatever it is that philosophers and programmers “get” and it is for this reason, most people are not philosophers or programmers. Philosophers and programmers are a rare and often solitary breed. Now, going forward, let’s consider Logic thinking & Decision-Making … Logical Thinking But First, A Simple Puzzle Can you make a necklace that costs no more than 15 cents using the four chains below? It costs 2 cents to open a link and 3 cents to close it again. If you're like most people, you'll be blind to the correct answer because you'll become fixated on the answer that seems immediately logical. But immediate logic might not always be the best path of action. But First, A Simple Puzzle 1 2 Immediate logic might suggest that we line up the four chains like this and open and close a link at each other four corners, but this won’t work because 4 (corners) x 2 (open) x 3 (close) = 20, and we only have 15 cents. The trick is to come up with a way where we can do 3 x 2 x 2 = 15. 3 4 Luckily, there is. Instead of looking at the number of chains, we might look at the number of links in one chain. A Simple Solution If we were to open all the links in a single chain that would cost use 6 cents, leaving 9 cents remaining for closing the links, for a total of 15. A Simple Solution 1 2 Now we have three chains with three corners, and three separate links to create the necklace. 3 (corners) x 2 (open) x 3 (close) = 15 Problem solved! 3 Decision Making Now, A Simple Scenario… Game’s on at my house. Beer’s cold! B The Task: Directions 1. Go from Point A to Point B Dude, I’ll be right over! A …Where Things Could Go Wrong … B The Task: 1. Go from Point A to Point B 2. Watch for passing cars at the Intersections OR 3. Treat Intersections as 4-Way Stops A Decisions, Decisions … B The Task: 1. Go from Point A to Point B 2. Stop at all Intersections (2-way stops going North and South) 3. Watch for cars going West and East before going through the Intersections. A Different Decisions NOT Based on Directions… B The Task: 1. Go from Point A to Point B 2. Treat Intersections as 4-Way Stops 3. Consider rules of “Right of Way” before going through the Intersection a) Car to left b) Car to right c) Car ahead, not turning d) Car ahead, turning 4. Unexpected things: IF / ELSE / OR / AND A More Decisions … B The Task: 1. Go from Point A to Point B 2. Treat Intersections as 4-Way Stops 3. Consider rules of “Right of Way” before going through the Intersection a) Car to left b) Car to right c) Car ahead, not turning d) Car ahead, turning 4. Unexpected things: IF / ELSE / OR / AND A Alternative Decisions … The Task: 1. Go from Point A to Point B 2. Treat Intersections as 4-Way Stops 3. Consider rules of “Right of Way” before going through the Intersection a) Car to left b) Car to right c) Car ahead, not turning d) Car ahead, turning 4. Unexpected things: IF / ELSE / OR / AND B Different Decisions … B The Task: 1. Go from Point A to Point B 2. Consider rules of “Green / Red / Yellow ” before going through the Intersection a) Red - STOP b) Green - GO c) Yellow - CAUTION 3. Unexpected things: IF / ELSE / OR / AND A Unexpected Decisions … B The Task: 1. Go from Point A to Point B 2. Consider rules of “Green / Red / Yellow ” before going through the Intersection a) Red - STOP b) Green - GO c) Yellow - CAUTION 3. Unexpected things: IF / ELSE / OR / AND Tree Across Roadway Power Lines Across Roadway Gravel in Roadway Glass/Metal in Roadway A Different Decisions … The Task: 1. Go from Point A to Point B 2. Consider rules of “Green / Red / Yellow ” before going through the Intersection a) Red - STOP b) Green - GO c) Yellow - CAUTION 4. Unexpected things: IF / ELSE / OR / AND A Programming is All About Decision Making, Logic, & Memory Do … While If … Then Or … Else And … Or True … False … Fail Gracefully and Meaningfully All this DECISION MAKING has to be STORED and TRACKED somewhere! …And Avoiding Errors/Crashes/Blue Screens The dreaded “Blue Screen of Death” But How Does It All Work? Central Processor Unit (CPU) Memory Storage Input/Output (I/O) Keyboard Mouse Monitor Speakers Network Memory RAM (Random Access Memory) DRAM (Dynamic) Memory Storage Cells "Allocated Memory Locations" http://static.howstuffworks.com/flash/ram-virtual.swf Memory Text Characters ASCII (American Standard Code for Information Interchange) Play Video Java Memory Throughout the quarter, instead of calling these by their correct name, Allocated Memory Locations, I will usually just called them "Buckets". Programming Paradigms Procedural (Top Down “Function” Driven) Object Oriented (“Class” and “Object” Driven) Procedural Programming Programs in the past used a procedural process for development where a program performed in series of steps , in a linear fashion and the focus was on steps as shown below: Object-Oriented Programming In object-oriented programming, the focus is on real world objects which interact with each other. Objects in turn have state, attributes and operations. Objects are viable things that do something. Objects perform actions. Class , Object, and Method: Here Comes the Hippy-Dippy Stuff! Objected-Oriented Programming: Three Important Terms • Class – A template or blueprint which defines attributes and actions (properties and behavior). It is like the ‘idea’ of a thing, but not the actual thing itself. Example: a blueprint of a house is not something you can live in, but you can use the blueprint to construct a house. • Object – Something that is created (instantiated) from the Class blueprint or template. Example: A house constructed from a blueprint that you can actually live in would be an Object • Method – The action or behavior that an Object can do as defined by the Class. Example: You can fly a plane (an Objected made from the Plane class), but you cannot fly a car (an Object made from the Car class)…at least not yet! Class A “Class” is the idea of how something might look or what something might do Class Car A Class “Car” will have some ideas as to how a car should look and what it might do. We will have to come up with these ideas… Class Car fourWheels() oneSteeringWheel() oneEngine() twoOrFourDoors(?) twoOrFourSeats(?) transmissionType(?) You can create methods that will define how a “car” might look and what it might do. Class Car fourWheels() oneSteeringWheel() oneEngine() twoOrFourDoors(?) twoOrFourSeats(?) transmissionType(?) myCar Now you can create an Object from the Car class that can use the Car methods to look a certain way and do certain things as defined by the Car class. Class Car fourWheels() oneSteeringWheel() oneEngine() twoOrFourDoors(?) twoOrFourSeats(?) transmissionType(?) myCar myCar.fourWheels() myCar.oneEngine() myCar.twoOrFourSeats(4) myCar.oneSteeringWheel() myCar.twoOrFourDoors(4) myCar.transmissionType(auto) Now you can create an Object from the Car class that can use the methods so it can look a certain way and do certain things as defined by the Car class. Class: Example Using Language • • • • • • • • • • • Animal Mammal Breed Fur Barks Plays fetch Digs Chase cats Eats Sleeps Etc. … Class: Example Using Language The word "animal" comes from the Latin word animalis, meaning "having breath". The biological definition of the word refers to all members of the kingdom Animalia, encompassing creatures as diverse as fish, reptiles, birds, mammals, and insects . Mammals are air-breathing vertebrate animals characterized by the possession of endothermy, hair, three middle ear bones, and mammary glands functional in mothers with young. • • • • • • • • Animal Mammal Breed Fur Barks Plays fetch Digs etc … From Consensus Class Object We think in terms of “Class” all the time … For instance, do you understand this sentence? The dog chased the ball into the street and nearly got hit by the car. Okay, if you understand this sentence, then … • • • • What kind of dog was it? What kind of ball was it? What kind of street was it? What kind of car was it? “Class” Objects “Dog” “Ball” General Attributes General Attributes General Attributes General Attributes Specific Properties and Actions Specific Properties and Actions Specific Properties and Actions Specific Properties and Actions “Street” “Car” Class Object In Object-Oriented Programming, a Class is like a cookie cutter and type of dough, and Objects are like the actual cookies made from them. In this example, the cookie cutter and dough make up the Class which supplies specific attributes like type of dough, shape, and size. Any cookie made from this Class is an Object and will inherit the attributes as defined by the Class (i.e., type of dough, shape, and size). Class Class Object PersonWithCapAndBag EXTENDS Person In Java, the various things (actions) that Objects do are called Methods Objects Do Something (Some Action) In Java, the various things (actions) that Objects can do are called Methods Object & Method: Dot Notation Mailman.fillBag(); Mailman.deliverMail(); Mailman.pickUpMail(); Mailman.putMailInBag(); Mailman.runFromBigDog(); Object Method Paperboy.fillBag(); Paperboy.deliverPaper(); Paperboy.runFromBigDog(); Paperboy.drinksSlurpee(); Paperboy.collectsPayment(); Object Method Methods are part of the Class from which the Objects are made (or instantiated in programming terms). Methods are referenced by a name with double paretheses ( ) at the end. So, Putting it All Together… In Java, we will be doing using Classes, Objects, and Methods to code programs • Class A class is the blueprint (containing attributes and actions) from which individual objects are created • Object An object is a named instance of a class that can now actually do something • Method A method is an action from a class that the object might do DON’T WORRY: We will “officially” be going over Classes, Objects, and Methods again in the next Lecture, so don’t fret if this is a little confusing today. It will all start to sink in, I promise, and we’re going to have a lot of fun with it What you will learn in this Introduction to Programming class is just the "tip of the iceberg" but it will establish a firm foundation for you to confidently go on to learn and excel in other programming languages.