Unformatted text preview:

1.1 Your First ProgramWhy Programming?Why programming? Need to tell computer what you want it to do.Naive ideal. Natural language instructions.Prepackaged solutions (apps)? Great, when what they do is what you want.Programming. Enables you to make a computer do anything you want.Ada LovelaceAnalytic Engine“Please simulate the motion of N heavenly bodies, subject to Newton’s laws of motion and gravity.”well, almost anything[stay tuned]LanguagesMachine languages. Tedious and error-prone.Natural languages. Ambiguous; can be difficult to parse.High-level programming languages. Acceptable tradeoff.[ real newspaper headlines, compiled by Rich Pattis ]Kids Make Nutritious Snacks.Red Tape Holds Up New Bridge.Police Squad Helps Dog Bite Victim.Local High School Dropouts Cut in Half.“Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.” – Donald KnuthWhy Program?Why program?•A natural, satisfying and creative experience.•Enables accomplishments not otherwise possible.•Opens new world of intellectual endeavor.First challenge. Learn a programming language.Next question. Which one?Naive ideal. A single programming language.Our Choice: JavaJava features.•Widely used.•Widely available.•Embraces full set of modern abstractions.•Variety of automatic checks for mistakes in programs.Java economy. •Mars rover.•Cell phones.•Blu-ray Disc.•Web servers.•Medical devices.•Supercomputing.•…James Goslinghttp://java.net/jag$100 billion,5 million developersWhy Java?Java features.•Widely used.•Widely available.•Embraces full set of modern abstractions.•Variety of automatic checks for mistakes in programs.Facts of life. •No language is perfect.•We need to choose some language.Our approach.•Minimal subset of Java.•Develop general programming skillsthat are applicable to many languagesIt’s not about the language!“There are only two kinds of programming languages: those people always [gripe] about and those nobody uses.” – Bjarne StroustrupA Rich Subset of the Java LanguagePrimitive Numeric Types!===>=<=<>--/+%-++*Integer.parseInt()Double.parseDouble()ParsingMath.max()Math.min()Math.pow()Math.sqrt()Math.PIMath.abs()Math.log()Math.sin()Math LibraryMath.exp()Math.cos()System.out.println()System.out.print()System.out.printf()SystemforifFlow Controlwhileelse!||trueBoolean&&false;,({Punctuation)}a[i]newa.lengthArraysmatches()charAt()length()+StringcompareTo()""booleancharlongintBuilt-In TypesStringdoubletoString()finalmain()newpublicclassObjectsprivatestatic=AssignmentProgram Development EditCompileRunProgramming in JavaProgramming in Java.Create the program by typing it into a text editor, andsave it as HelloWorld.java.HelloWorld.java/******************************************* * Prints "Hello, World" * Everyone's first Java program. *******************************************/public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }}Programming in JavaProgramming in Java.Create the program by typing it into a text editor, andsave it as HelloWorld.java.Compile it by typing at the command-line:javac HelloWorld.java.This creates a Java bytecode file named: HelloWorld.class.command-line% javac HelloWorld.java(or click the Compile button in DrJava)Programming in JavaProgramming in Java.Create the program by typing it into a text editor, andsave it as HelloWorld.java.Compile it by typing at the command-line:javac HelloWorld.java.Execute it by typing at the command-line:java HelloWorld.% javac HelloWorld.java% java HelloWorldHello, Worldcommand-lineProgram Development (using DrJava)Program development in Java (using DrJava).1. Edit your program using the built-in text editor.2. Compile it to create an executable file.3. Run your program.texteditorProgram Development (using DrJava)Program development in Java (using DrJava).1. Edit your program.2. Compile it by clicking the “compile” button.3. Run your program.compilebuttoncreatesUseArgument.classProgram development in Java (using DrJava).1. Edit your program.2. Compile it to create an executable file.3. Run your program by clicking the “run” button or using the command line.Program Development (using DrJava)Alternative 1:run button(OK if no args)Alternative 2:command line(to provide args)both useUseArgument.classNote: Program StyleThree versions of the same program.// java HelloWorldpublic class HelloWorld{ public static void main(String[] args) { System.out.println("Hello, World"); }}/************************************************************************* * Compilation: javac HelloWorld.java * Execution: java HelloWorld * * Prints "Hello, World". By tradition, this is everyone's first program. * * % java HelloWorld * Hello, World * *************************************************************************/public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }}public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }Fonts, color, comments, and extra space are not relevant to Java.Note: Program StyleDifferent styles are appropriate in different contexts.•DrJava•Booksite•Book•COS 126 assignmentEnforcing consistent style can•Stifle creativity.•Confuse style rules with language rules.Emphasizing consistent style can•Make it easier to spot errors.•Make it easier for others to read and use code.•Enable development environment to provide useful visual cues.Bottom line for COS 126: Life is easiest if you use DrJava style.1.2 Built-in Types of DataBuilt-in Data TypesData type. A set of values and operations defined on those values.add, subtract, multiply, divide3.14156.022e23floating-point numbersdoubleadd, subtract, multiply, divide1712345integersintand, or, nottruefalsetruth valuesbooleansequences of characterscharactersset of values operationsliteral valuestypecompare'A''@'charStringconcatenate"Hello World""CS is fun"Basic DefinitionsVariable. A name that refers to a value.Literal. Programming-language representation of a value.Assignment statement. Associates a value with a variable.Program. Sequence of statements.int a, b;a = 1234 ;b = 99;int c = a + b;assignmentstatementvariablenamecombined declarationand assignment statementdeclaration


View Full Document

Princeton COS 126 - Your First Program

Download Your First Program
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Your First Program and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Your First Program 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?