bit 115

46
BIT 115 Introduction to Programming: Java Learning to Program Robotically INSTRUCTOR: Craig Duckett EMAIL: [email protected]

Upload: phiala

Post on 24-Feb-2016

60 views

Category:

Documents


0 download

DESCRIPTION

BIT 115. Introduction to Programming: Java Learning to Program Robotically. INSTRUCTOR : Craig Duckett EMAIL : [email protected]. What is Computer Programming?. Computers don't do anything without someone telling them what to do—much like the average middle school student. - PowerPoint PPT Presentation

TRANSCRIPT

BIT 115

BIT 115Introduction toProgramming: JavaLearning to Program Robotically

INSTRUCTOR: Craig DuckettEMAIL: [email protected]

Computer Programming

What is Computer Programming?Computers don't do anything without someone telling them what to domuch 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 dostep by stepmuch 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 isnt Difficult, but Time-ConsumingProgramming 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 & ProgrammersWhat do they have in common besides pondering and logic?

Everybody else in the worldPhilosophers & ProgrammersStill Even though most of us practice philosophical thinking and the components of logic and decision-making hundreds of times a day

we just dont 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 dont get whatever it is that philosophers and programmers get and it is for this reason, most people are notphilosophers or programmers.

Philosophers and programmers are a rare and often solitary breed.Now, going forward, lets consider Logic thinking & Decision-Making Logical Thinking

But First, A Simple PuzzleCan 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

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 wont 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.

Luckily, there is.

Instead of looking at the number of chains, we might look at the number of links in one chain.1234A 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

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!123Decision Making

Now, A Simple ScenarioThe Task: Directions 1. Go from Point A to Point B

Where Things CouldGo Wrong The Task: 1. Go from Point A to Point B2. Watch for passing cars at the Intersections OR3. Treat Intersections as 4-Way Stops

Decisions, Decisions

The Task: 1. Go from Point A to Point B2. Stop at all Intersections (2-way stops going North and South)3. Watch for cars going West and East before going through the Intersections.Different Decisions NOT Based on Directions

The Task: 1. Go from Point A to Point B2. Treat Intersections as 4-Way Stops3. 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, turning4. Unexpected things: IF / ELSE / OR / ANDMore Decisions The Task: 1. Go from Point A to Point B2. Treat Intersections as 4-Way Stops3. 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, turning4. Unexpected things: IF / ELSE / OR / AND

Alternative Decisions The Task: 1. Go from Point A to Point B2. Treat Intersections as 4-Way Stops3. 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, turning4. Unexpected things: IF / ELSE / OR / AND

Different Decisions The Task: 1. Go from Point A to Point B2. Consider rules of Green / Red / Yellow before going through the Intersection a) Red - STOP b) Green - GO c) Yellow - CAUTION3. Unexpected things: IF / ELSE / OR / AND

Unexpected Decisions The Task: 1. Go from Point A to Point B2. Consider rules of Green / Red / Yellow before going through the Intersection a) Red - STOP b) Green - GO c) Yellow - CAUTION3. Unexpected things: IF / ELSE / OR / AND

Tree Across Roadway Power Lines Across Roadway Gravel in Roadway Glass/Metal in Roadway

Different Decisions The Task: 1. Go from Point A to Point B2. Consider rules of Green / Red / Yellow before going through the Intersection a) Red - STOP b) Green - GO c) Yellow - CAUTION4. Unexpected things: IF / ELSE / OR / AND

Programming is All About Decision Making, Logic, & Memory Do WhileIf ThenOr ElseAnd OrTrue False

Fail Gracefullyand MeaningfullyAll this DECISION MAKINGhas to be STORED and TRACKED somewhere!And Avoiding Errors/Crashes/Blue Screens

But How Does It All Work?Central Processor Unit (CPU)MemoryStorageInput/Output (I/O)KeyboardMouseMonitorSpeakersNetwork

MemoryRAM (Random Access Memory)DRAM (Dynamic)

Memory Storage Cells"Allocated Memory Locations"

http://static.howstuffworks.com/flash/ram-virtual.swfMemory Text Characters

ASCII (American Standard Code for Information Interchange)Play VideoJava Memory

Throughout the quarter, instead of calling these by their correct name, Allocated Memory Locations, I will usually just called them "Buckets".Programming ParadigmsProcedural (Top Down Function Driven)Object Oriented (Class and Object Driven)

Procedural ProgrammingPrograms in the past used a procedural process for development wherea program performed in series of steps, in a linear fashion and the focus wason steps as shown below:

Object-Oriented ProgrammingIn object-oriented programming, the focus is on real world objectswhich interact with each other. Objects in turn havestate, 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 TermsClass 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: Example Using LanguageDogAnimalMammalBreedFurBarksPlays fetchDigsChase catsEatsSleepsEtc. Class: Example Using LanguageDogAnimalMammalBreedFurBarksPlays fetchDigsetc AnimalMammalMammals are air-breathing vertebrate animals characterized by the possession of endothermy, hair, three middle ear bones, and mammary glands functional in mothers with young. 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 .From Consensus Class Object

Dog

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 ObjectsDog Ball Street CarGeneral AttributesGeneral AttributesGeneral AttributesGeneral Attributes

Specific Propertiesand ActionsSpecific Propertiesand ActionsSpecific Propertiesand ActionsSpecific Propertiesand Actions Class Object In Object-Oriented Programming, a Class is like a cookie cutter and dough, and Objects are like the unique cookies made from them

Class Class Object In Java, the various things (actions) that Objects do are called MethodsPersonWithCapAndBag EXTENDS Person

Objects Do Something (Some Action) In Java, the various things (actions) that Objects can do are called MethodsObject & Method: Dot NotationMailman.fillBag();Mailman.deliverMail();Mailman.pickUpMail();Mailman.putMailInBag();Mailman.runFromBigDog();Paperboy.fillBag();Paperboy.deliverPaper();Paperboy.runFromBigDog();Paperboy.drinksSlurpee();Paperboy.collectsPayment();

ObjectMethodObjectMethodMethods 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. DONT WORRY: We will officially be going over Classes, Objects, and Methods again in the next Lecture, so dont fret if this is a little confusing today. It will all start to sink in, I promise, and were going to have a lot of fun with it 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

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.