CS115 lecture notes, Spring 1999 Week 1, Friday Reading: Chapter 1 Quiz #1 Go over syllabus. Don't take notes. 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! 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?