the basics apcs 2012-13 mr. ahronheim. computer are powerful tools, but like any tool, they don’t...

56
Chapter 1 The Basics APCS 2012-13 Mr. Ahronheim

Upload: darleen-mcbride

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Chapter 1The Basics

APCS 2012-13Mr. Ahronheim

Page 2: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Computer are powerful tools, but like any tool, they don’t do anything until you use them!

Computers are useful because they can perform many tasks much faster than humans can, but unlike humans, they have a difficult time learning.

As a result it is our jobs as Humans to tell the computer what to do, as absolutely explicitly as possible.

Computers without Programs

Page 3: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

A computer won’t do ANYTHING unless somebody tells them to. Even automated tasks had to be told to them by a human at some point. ◦ The Java auto-updater started right now because

some programmer decided it should run right now!

Because they will try to do anything we ask them to, we have to be very careful what we ask them to do, since they will carry out our orders exactly as we say them.

Use small words

Page 4: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

English is (demonstrably) often open to interpretation. It is vague and the same sentence can mean totally different things depending on the context.

Computers have a lot of trouble with that! So to ease the computer’s understanding of what we mean, we have to come up with languages in which each sentence can be interpreted in as few ways as possible (preferably only one way!).

Don’t be ambiguous!

Page 5: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Programming languages exist so that we as humans have a simple way of telling the computer exactly what we want it to do and the computer has a chance at correctly understanding what we want!

The trick is learning the language, and then accurately translating your wishes into this new language.

Easier said then done, right?

Hence: Programming Languages

Page 6: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

ENIAC is considered by many to be a good example of the very first computers.

It was created in 1946 by the military to compute artillery firing solutions.

It ran at roughly 500 FLOPS! That’s fast!

Baby Computers

Page 7: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

A modern processor can do about 200 gigaflops.

A modern, high-end graphics card can do over 10 teraflops.

Think about that for a minute…

We’ve come a long way…

Page 8: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

A modern computer can be divided into a few major hardware components:◦ CPU – The Central Processing Unit is the brains of

the outfit. It’s the thing that actually runs the show and computes the things that you want computed.

◦ Storage – The memory of the CPU that holds information until it is needed for processing by the CPU. It can be read from or written to, and should contain instructions on how the computer is to process things.

That’s technically all there is as Alan Turing would have said. This is the minimum standard.

Computer Guts

Page 9: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

That said, modern computers divide these tasks further and add a few extra pieces such as:◦ Peripherals – Extra parts that are designed to aid

the CPU in performing specialized tasks. They are a host of TLA’s like GPU, SPU, DAC, etc.

◦ Interface – We prefer our computers to be able to accept extra input easily, so we include things like networking, keyboards and monitors. Not really necessary, but I suppose if you like that kind of thing…

Specifics

Page 10: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Let’s take a look at some computer innards and talk about them for a bit…

Show and tell

Page 11: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The CPU talks in it’s own language, called machine code.

That’s the one that looks like:◦ 01001101 01011010 10010000 00000000

00000011 00000000 00000000 00000000 00000100 00000000 00000000 00000000 11111111 11111111 00000000 00000000

Can you tell what it’s from?

What does hardware have to do with software?

Page 12: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Machine code is hard to read, and even if we could, it goes into extreme detail about what the processor actually does to play Minecraft.◦ Read memory location a, read memory location b,

add memory locations a and b and store the result in c, put your right hand in, take your right hand out, etc.

Writing programs like that would take a long time, and indeed it used to work like that.

The First 10 bytes of Minecraft

Page 13: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

With computer hardware screaming along, software had to keep pace to take advantage of it.

Programming in machine code can still be useful in many tasks◦ This is called “Low-Level” programming because

you’re very close to directly speaking with the processor.

You simply can’t effectively write complex programs like that.

The Old Way

Page 14: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

To help programmers speak to computers, new languages were needed.

These languages needed to be close enough to Machine Language that they could be easily translated for use by the processor, but it also needed to be close enough to human speech to enable us to give complex strings of commands all at once.

Besides, many processors “speak” completely different languages!

Enter the early programming languages.

Kickin’ it New School

Page 15: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

In the 1950’s, languages began to appear that more closely resembled human speech.

Programmers could write their programs using more general ideas, and these programs were fed to a special program called a “compiler.”

The compiler had the job of translating the program from the high-level language into machine language.

High Level Programming

Page 16: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The compiler takes code like:◦ message = “Go Blue”;

And turns it into:◦ 01001101 01011010 10010000 00000000

00000011 00000000 00000000 00000000 00000100 00000000 00000000 00000000 11111111 11111111 00000000 00000000

Just kidding; that was Minecraft again. You get the idea.

Compiling

Page 17: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Remember, programming languages aren’t just machine codes converted into more reasonable words.

A compiler has to analyze programs and create machine code that mimics the plan laid out in the programming language.

It’s not as easy as it looks

Page 18: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The difference is in Syntax versus Semantics. Syntax is the physical format of a line of code.

◦ In Java, almost every line should end with a semicolon. It has little to do with the meaning of the code before the semicolon, but the structure dictates that it’s there.

◦ Computers find this task easy. There are strict rules, and you have to follow them, or else it’s an error.

◦ If I say “Stand up for your rights.” or “Stand up so I can steal your chair.” I am the words “Stand up” in both places. They are syntactically the same.

Fancy Talk

Page 19: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Semantics are the actual meaning of the message.◦ In the “Stand up” example, even if the syntax was

similar, the semantics were very different.◦ In the first example, “Stand up” meant to resist

pressure to change.◦ In the second example the words, “Stand up”

actually meant for you to physically get out of your seat.

Computers and programmers can often disagree about semantics!

Semantics

Page 20: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The compiler has to read your program and analyze both the syntax and semantics of your program and spit out a program in machine code that mimics it exactly.

That’s not easy at all.

It’s a tough job

Page 21: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Java was the creation of a team of programmers, led by James Gosling and Patrick Naughton at Sun Microsystems in the early to mid ‘90’s.

Java has a few important guiding principles:◦ Simple◦ Portable◦ Object-Oriented◦ Good Libraries

Java

Page 22: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Java was created to resemble existing languages like C and C++.

This made it much easier for programmers to learn Java, since so much was already familiar.

Simplicity

Page 23: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

One of the niftiest things about Java is that its compiler doesn’t compile it into machine code for your computer.

Instead it compiles it into an intermediate language called “bytecode”. This is what you download and run when you run a Java program.

The actual conversion to machine code doesn’t happen until you actually run the program.

Portability

Page 24: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The trick is that every computer that wants to run Java bytecode has to have something called the Java Virtual Machine.

Essentially, it is a fake computer that takes in bytecode and spits out the proper machine code.

In essence, the semantic translation happens when the Java code is converted to bytecode, and the syntax is converted when the program actually runs.

The JVM

Page 25: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The important thing to know is this:◦ Any machine that has a JVM can run any Java

Program.◦ You don’t need to compile a “Mac version” and a

“PC version” for each program. You just need a JVM on each platform (which both already exist) and all Java programs compiled into bytecode should play equally nicely on each computer.

◦ This is called “Portability” – that the same bytecode runs on any platform that has a JVM.

What does that get us

Page 26: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Whoa there, cowboy!◦ This is a topic for another day.

◦ Don’t worry, we’ll get there soon.

Object Oriented Programming

Page 27: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Unlike many other disciplines, Computer Programming depends on the ability to use code written by other people.

Why reinvent the wheel?◦ If some nice Java guy wrote a method that prints

text onto the screen, why should every single person have to rewrite that program?

◦ It’s a waste of time! Java has a huge library of pre-written

programs that you are encouraged to explore and use as you see fit.

Libraries

Page 28: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Remember, you’re in a class to learn some of this stuff, so you will be recreating large amounts of code someone else has written.◦ You aren’t writing code for the sake of

productivity, you’re writing it to gain understanding.

While in this class you can (and should!) explore and use the existing Java Libraries, but you can’t use the libraries to completely circumvent your assignments.

Other than that: go nuts.

The Catches

Page 29: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Let’s do some Java-ing!

◦ We’ll start with the all-time most written program in any language!

◦ Let’s have our computers introduce themselves!

But enough talk… Have at you!

Page 30: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Start up Eclipse and give it a workspace location (Preferably on your Z: or H: drive).

Select “New…” then “New Java Project”. Call this project “Hello World” Click next until the creation wizard spits you

out to a screen with a placeholder program.

Baby’s first Program

Page 31: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Place your cursor underneath the line marked “public static void main(String[] args)”

Type in the following words, exactly as written, including punctuation and capitalization.

System.out.println(“Hello, World!”); Now click the small green “play” button at

the top of the screen to run the program. Congratulations! You’re now a programmer!

A rite of passage

Page 32: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Don’t worry about the “public static yadda yadda” stuff. We’ll come back to that.

The important thing is to look at the basic Java syntax and start to get comfortable with it.

Let’s look at a few features.

So what just went on there?

Page 33: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Try changing the “S” in “System” to be lower case.◦ Does the program still run?

This should tell you that Java is case-sensitive. “Artichoke” is not the same as “artichoke”. Keep that in mind.

Things to note

Page 34: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Next thing: all programs should start with the words “public class” and then some class name or another.

We’ll discuss Classes shortly when we move on to chapter 2.

You should also notice that there are curly braces (they look like this: {}) all over the place.

Every open brace { has a corresponding closing brace: }.

Which Class, now?

Page 35: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

In Java, these curly braces are a way for the program to combine several commands into one unit.

The actual type of unit can vary, but whenever we group commands, we do so using these curly braces.

You might find that Eclipse is configured to automatically close many of these braces when you enter an open brace. This is a nice thing. You should thank it when it does this.

Brace yourselves

Page 36: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Underneath the line of code you wrote, type in://System.out.println(“Good-bye, world!”) What happens? What is the difference between this line and the

other?

A little comment

Page 37: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Sometimes there is information that is important to us humans that the computer could care less about.

These are often messages between programmers explaining what a particular set of code is supposed to do, or warning of potential problems.

Java allows us to write these “comments” by starting a line with a double forward slash, or //

Communicating with ourselves

Page 38: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

All comments are completely ignored by the compiler and will not change the finished program in any way.

Comments are strictly for humans, and they further help us understand what is still a bit of a foreign language.

Especially early on, you should write MANY COMMENTS. They will help you structure your programs better and will help ME when I try to help you get your programs working.

More on this later.

The first of many comments

Page 39: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The line “System.out.println(“Hello, World!”); is actually a deceptively complicated line.

There is a lot of stuff going on here. I’ll give you a brief overview, but don’t

stress too much if some it goes over your head.

So what did you just write?

Page 40: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

System.out in this case is the Object that contains a command that we’d like to run.

We can use the commands stored in them by writing out their name (case-sensitive, remember??) followed by a period.

Typically, when you do this, Eclipse will helpfully give you a list of all know commands that are stored in that particular Object.

This is very useful.

System.out

Page 41: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The next word, “println” represents the Method being used.

A method is simply a list of instructions that have been grouped together to accomplish a single task.

Methods are the primary way that we accomplish things in programming.

You can look for yourself to see all the methods that are associated with the Object System.out

println

Page 42: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

All methods MUST be immediately followed by parentheses.

These parentheses contain any extra information that the method needs to do its job.

In the case of println, which prints anything you give it onto the screen, the parentheses contain whatever it is you want put on-screen.

The information in these parentheses are called parameters.

( )

Page 43: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

A method can have anywhere from 0 parameters to a whole ton of them.

You need to look at each method to know which parameters you need to give each method.

The little menu that comes up where you can choose a method is extremely useful for this.

You must match the order and type of information precisely or the method will not run.

Parameters

Page 44: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

As it happens, we very frequently need to deal with long sequences of letters, numbers, and/or symbols.

Because of that, Java makes it very easy to group these sequences together using a data structure called a String.

Anything that you plop inside a pair of double quotes is considered by Java to be a single String.◦ Specifically a “String literal”.

“Hello, World!”

Page 45: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

In Java, almost all lines must end in a semicolon.

The only exceptions are (most, but not all) lines with curly braces.

;

Page 46: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Let me begin by saying that programming can become extremely tricky in a hurry.

It is INEVITABLE that your program will have mistakes in it. That’s ok.

Don’t be surprised if very few of your programs run correctly on your first try.

Number one rule:◦ Don’t get frustrated. If you have to, step away

and cool off. A fresh look is almost always the key to finding pesky bugs.

When good programs go bad

Page 47: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Admiral Grace Hopper, USN was a pioneer in the computer science field.

She is credited with several major breakthroughs in Computer Science.◦ She co-created the programming language

COBOL, which was one of the earliest examples of a modern programming language.

◦ She worked on several early computers, including the Harvard Mark I and II, as well as UNIVAC.

Grace Hopper

Page 48: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

In 1946, Hopper was working at Harvard on a very early computer and she and some of the staff were tracking down a confusing and persistent error.

Eventually, this is what they found:

Bugs

Page 49: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

The First Computer Bug

Page 50: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

There are two major types of errors that you should concern yourself with:◦ Compile-Time, or Syntax errors◦ Run-Time, or Logic errors.

Types of errors

Page 51: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

What’s wrong with this line of code?System.out.pintln(“Hello, World.”); That’s right! It’s supposed to be “println”! In this case, Java has no idea what method

the programmer is talking about and will refuse to compile the program.

This kind of error is relatively easily spotted, because Eclipse will underline it in angry red and slap a red X all over your project.◦ Kind of a dead giveaway

Syntax Errors

Page 52: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

One easy mistake to make once you’re used to using an IDE that underlines errors like that is to assume that the problem must always be in the underlined bit.

Sometimes the code in that piece is only wrong because you’re missing a line somewhere else.

Modern programming environments are pretty good at underlining the right things, but they aren’t perfect.

Syntax errors (cont’d)

Page 53: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Logic errors are far more tricky to chase down because they generally occur in technically correct Java code.

A logic error happens when your code compiles successfully, but it doesn’t actually do what you wanted it to.

A simple example is if you wrote your Hello, World program with the following line:

System.out.println(“Hello, Word!”);

Logic Errors

Page 54: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

System.out.println(“Hello, Word!”); The program was supposed to output Hello,

World! This one will not do that. Despite the fact

that the Java code is syntactically correct, the actual results don’t match up with the intended results.

Eclipse has no way of knowing this, so it will be of minimal help in tracking these down.

Word.

Page 55: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

It should be noted that program crashes fall into this category.

A crash occurs when the Java code is syntactically correct, but when the program attempts to run, a logic error occurs that is bad enough that the program can no longer continue.

Crashing

Page 56: The Basics APCS 2012-13 Mr. Ahronheim.  Computer are powerful tools, but like any tool, they don’t do anything until you use them!  Computers are useful

Summing up

Syntax Errors Logic Errors

Errors involving the structure of your program.

Are noticed when the compiler attempts to process your program. (Compile-time error)

Error involving the results of your program not matching your intent.

Don’t interfere in the compiling process.

Are typically only found once you run your program (Run-time errors)