CS115 lecture notes, Fall 1999 Week 1, Friday Reading: Chapter 1 Quiz #1 Learning styles... 3x5 index cards of Dooooooom! ----- What is a program? A sequence of instructions that specifies HOW to perform a computation. Instructions: input, output, simple math, comparison, repetition Specify HOW: in excruciating detail... beginning programmers often have the feeling of teaching an idiot, because the computer seems not to know many things that are obvious to people. Think of it as an exercise in inter-species communication. Computation: More general than just math -- anything a computer can do is considered computation in broad sense. Amazing thing #1: all computations are made up of the same set of simple instructions. Amazing thing #2: compilers are programs that translate other programs, sometimes improving them in the process! Errors ------ Is programming a walk in the park? Why, no. Sometimes it is frustrating and unpleasant, like all worthwhile things. Some things that go wrong: 1) Syntax errors: computer is a stickler for spelling and grammar. Syntax errors are discovered by the compiler, and prevent it from generating code. You can't run until you get rid of all the syntax errors. 2) Run-time errors: An unusual condition, like dividing by zero, can cause the program to stop executing at some point. Called "exception" in Java. 3) Logic errors: The program runs, but does not do what you expected. Really, these are not three kinds of errors, but three times when you might detect errors. There is only one kind of error --- the program does not do what you wanted it to do. Why? Because you did not specify the right computation. Computer interprets code quite literally, with no understanding of what you are trying to accomplish. Natural and formal languages ---------------------------- Nat lang: evolved, people speak it Formal lang: designed, people use it for special purpose Mathematics uses a formal language, chemistry too. Syntax refers to the rules and structure of the language. Semantics refers to the meaning of the language. A syntax error is a statement that is meaningless because it violates the rules of the language: 1) 3+3=6 is syntactically correct, 3=+6$ is not 2) H2O is correct, 2Zz is not. The statement 3+3=5 is syntactically correct, but semantically incorrect, because what it means is false. The chemical reaction Pb -> Au is syntactically correct, but semantically unlikely. When you read or hear a sentence in English, you unconsciously figure out its structure (nouns, verbs, subjects, objects). This process is called parsing. ----- Pedagogic comments: 1) Do the reading. 2) You are learning two languages: Java and CS vocabulary. Of the two, the CS vocabulary is more important. 3) Don't be fooled by common English words. They don't mean what you think. Better if we used Swedish. 4) If you don't understand a lecture, panic immediately. Read the book. Come see me. DON'T WAIT FOR THINGS TO GET BETTER. They won't. 5) Don't take notes. Java syntax ----------- public class Hello { // main: generate some simple output public static void main (String[] args) { System.out.println ("Hello, world."); } } 1) a program includes a class definition, which is the words "public class Hello", plus everything between the squiggly braces 2) public is optional, class is not, Hello is a name you make up 3) comments can contain anything (until the next line) 4) this class definition contains one method definition, which is the words "public static void main (String[] args)" plus everything between the squiggly braces. 5) Which squiggly brace goes with which? They nest. Indentation helps make it visually obvious. 6) public is optional, static and void are not, and the name "main" is special. It indicates the place in the program where execution begins. 7) System.out.println is the full name of the command to print things. There is no command named "pintln", but there is one named "print". 8) semi-colon at the end of every statement. What's a statement? What's the difference between "println" and "print"? What happens when you get these rules wrong? Mostly syntax errors, possibly a run-time error or two. If there is no method named main, that causes a run-time error. Same thing if you leave out "static". ----- Enough about syntax. Why are we learning to program, anyway? 1) Good way to learn to think like a computer scientist. 2) You might be a programmer. 3) Even if you're not, thinking like a programmer can make you a more productive user. 4) CS material reinforces some mathematics, introduces ideas of engineering, and dovetails with the natural sciences. ----- Intro to Chapter 2 Variables are named storage locations. There are different types of variables for different types of values. Things in quotes are String values. Can create a String variable: String fred; And assign a String value to it: fred = "Hello, world!"; Can create an integer variable: int bob; And assign an integer value to it: bob = 5; Can you put square pegs in round holes?