Professional Documents
Culture Documents
Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: 1.1 - 1.3 self-check: #1-14 exercises: #1-4
What is CSE?
y Computer Science y The study of theoretical foundations of information and computation and their implementation and application in computer systems. -- Wikipedia
y Many subfields
y y y y y y
Graphics, Computer Vision Artificial Intelligence Scientific Computing Robotics Databases, Data Mining Computational Linguistics, Natural Language Processing ...
SOURCES: Tabulated by National Science Foundation/Division of Science Resources Statistics; data from Department of Education/National Center for Education Statistics: Integrated Postsecondary Education Data System Completions Survey; and NSF/SRS: Sur
3
Copyright 2010 by Pearson Education
What is programming?
y program: A set of instructions
Java.
4
Copyright 2010 by Pearson Education
Programming languages
y Some influential ones:
y FORTRAN
y
y COBOL
y
y LISP
y
y BASIC
y
5
Copyright 2010 by Pearson Education
successful in industry; used to build major OSes such as Windows Runs on many platforms (Windows, Mac, Linux, cell phones...) The language taught in this textbook
y Java (1995):
y y
6
Copyright 2010 by Pearson Education
Compiling/running a program
1. Write it. y code or source code: The set of instructions in a program. 2. Compile it. compile: Translate a program from one language to another. y byte code: The Java compiler converts your code into a format named byte code that runs on many computer types. 3. Run (execute) it. y output: The messages printed to the user by a program.
source code compile byte code run output
8
Copyright 2010 by Pearson Education
A Java program
public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } }
System.out.println
y A statement that prints a line of output on the console.
y pronounced "print-linn" y sometimes called a "println statement" for short
11
Copyright 2010 by Pearson Education
y identifier: A name given to an item in your program. y must start with a letter or _ or $ y subsequent characters can be any of those or a number
y y
TheCure 49ers
ANSWER_IS_42 side-swipe
$bling$ Ph.D's
12
Copyright 2010 by Pearson Education
Keywords
y keyword: An identifier that you cannot use because it
13
Copyright 2010 by Pearson Education
Syntax
y syntax: The set of legal structures and commands that can
...
14
Copyright 2010 by Pearson Education
y Compiler output:
Hello.java:2: <identifier> expected pooblic static void main(String[] args) { ^ Hello.java:3: ';' expected } ^ 2 errors
y The compiler shows the line number where it found the error. y The error messages can be tough to understand!
15
Copyright 2010 by Pearson Education
Strings
y string: A sequence of characters to be printed. y Starts and ends with a " quote " character.
y
y Examples:
Escape sequences
y escape sequence: A special sequence of characters used
y Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");
y Output:
\hello how
are "you"?\\
17
Copyright 2010 by Pearson Education
Questions
y What is the output of the following println statements?
System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the downward spiral");
18
Copyright 2010 by Pearson Education
Answers
y Output of each println statement:
a \\ ' """ C: in b c
he downward spiral
19
Copyright 2010 by Pearson Education
Questions
y What println statements will generate this output?
This program prints a quote from the Gettysburg Address. "Four score and seven years ago, our 'fore fathers' brought forth on this continent a new nation."
Answers
y println statements to generate the output:
System.out.println("This program prints a"); System.out.println("quote from the Gettysburg Address."); System.out.println(); System.out.println("\"Four score and seven years ago,"); System.out.println("our 'fore fathers' brought forth on"); System.out.println("this continent a new nation.\"");