1 / 56

Chapter 1

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

havard
Download Presentation

Chapter 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Computers without Programs • 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.

  3. Use small words • 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.

  4. Don’t be ambiguous! • 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!).

  5. Hence: Programming Languages • 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?

  6. Baby Computers • 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!

  7. We’ve come a long way… • 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…

  8. Computer Guts • 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.

  9. Specifics • 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…

  10. Show and tell • Let’s take a look at some computer innards and talk about them for a bit…

  11. What does hardware have to do with software? • 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?

  12. The First 10 bytes of Minecraft • 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.

  13. The Old Way • 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.

  14. Kickin’ it New School • 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.

  15. High Level Programming • 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.

  16. Compiling • 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.

  17. It’s not as easy as it looks • 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.

  18. Fancy Talk • 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.

  19. Semantics • 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!

  20. It’s a tough job • 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.

  21. Java • 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

  22. Simplicity • 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.

  23. Portability • 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.

  24. The JVM • 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.

  25. What does that get us • 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.

  26. Object Oriented Programming • Whoa there, cowboy! • This is a topic for another day. • Don’t worry, we’ll get there soon.

  27. Libraries • 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.

  28. The Catches • 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.

  29. But enough talk… Have at you! • 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!

  30. Baby’s first Program • 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.

  31. A rite of passage • 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!

  32. So what just went on there? • Don’t worry about the “public static yaddayadda” 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.

  33. Things to note • 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.

  34. Which Class, now? • 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: }.

  35. Brace yourselves • 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.

  36. A little comment • 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?

  37. Communicating with ourselves • 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 //

  38. The first of many comments • 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.

  39. So what did you just write? • 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.

  40. System.out • 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.

  41. println • 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

  42. ( ) • 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.

  43. Parameters • 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.

  44. “Hello, World!” • 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”.

  45. ; • In Java, almost all lines must end in a semicolon. • The only exceptions are (most, but not all) lines with curly braces.

  46. When good programs go bad • 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.

  47. Grace Hopper • 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.

  48. Bugs • 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:

  49. The First Computer Bug

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

More Related