Introduction to Computer Programming

Download Report

Transcript Introduction to Computer Programming

Introduction to Computer
Programming
CS 126
Lecture 3
Zeke Maier
Plan for Today
• Questions
• Administrivia
• Data Types and
Expressions
• Assignment
Questions
• Steve Jobs
– Health?
• Products?
– iPod
– iPhone
– iTunes
– MacBook
• Macworld
– iTunes Store selling DRM free music
Administrivia
• http://students.cec.wustl.edu/~ejm3/
– TAs!
– CEC accounts
– Lab assignments
– Readings
Data Types
• Everything in Java has a type
– type: “kind of thing”
• Primitive Types
– int: Integer number: 5, 10, 15
– double: Decimal number: 5.15, 10.25
– boolean: true or false
– String: a string of characters: “Hello World”
Expressions
• Expression: anything that has a value
– infix expression syntax
• operators between operands
• Order of operations
– Evaluate left to right under the rules
1. Negation (-)
2. Multiplication(*), Division(/), & Modulus(%)
3. Addition(+), Subtraction(-)
Expression Trees
• Evaluated left to right and bottom to top
+
*
8
%
5
18
5
String Concatenation
• + operator will combine strings together
– Other primitive values will be converted to
strings when concatenation occurs
Boolean Expressions
• Expressions that evaluate to true or false
•
•
•
•
&&
||
!
==
and operator
or operator
not operator
equals operator
Variables
• A symbol which can hold a value of a
particular type
int age;
String myName;
double cars;
• Assign a value of a variable
int age = 24;
String myName = “Zeke”;
double cars = 0.5;
• Assign the value of an expression to a
variable
double money = 45.50 - (3 * 1.50);
Assigning New Values
• Variables can be reassigned a new value
int age = 24;
age = 25;
int age = 24;
age = age + 1;
• final keyword forces variable to retain
initial value
• final variables, called constants, have all
uppercase names
final int AGE = 24;
AGE = 25; //Error
Composition
• Combining small blocks of code
– How could we compose these three
statements into a single statement?
int age;
age = 24;
System.out.print(age);
Naming Abstraction
• Giving names to values: using variables
– What are some advantages?
• Choose meaningful names for variables!
Circle Area Example
Assignment
• No Class Monday
• Readings
– Wednesday
• AD Chapter 2, Chapter 7: 7.8 - 7.10
• KG Notes