02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.
-
Upload
bethanie-page -
Category
Documents
-
view
214 -
download
0
Transcript of 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.
02/14/2005
Introduction to Programmingwith Java, for Beginners
Midterm 1 Review
02/14/2005 CSE 110 2
Overview
A Java program is a collection of cooperating objects
An object is an instance of a classAll Java code exists within a classTop Down Design
Analyze the problem/simulation/game Decide what classes to use to model it
Bottom Up Programming Write code for a class/method a little at a time
02/14/2005 CSE 110 3
Object “state” and “behavior”
An object has “state” Instance variables that hold its data
An object has “behavior” Methods that perform actions like computations,
reporting information about state (“getters”), changing state (“setters”)
02/14/2005 CSE 110 4
Protecting an Object’s State
A software designer wants his/her objects to behave: Consistently and reliably According to “spec” (specification) True to its documentation (javadocs)
This is accomplished in part by: Making instance variables private Providing public methods that access the state
appropriately
02/14/2005 CSE 110 5
Object Creation
An object is an instance of a class A class has one or more constructors A constructor:
Is a piece of code with the same name as the class Is executed when an object is created with the “new”
operator When the “new” operator is used, Java finds the right class
and then runs the constructor that has right number, order, and types of parameters
02/14/2005 CSE 110 6
Variables
Variable Types Primitive Reference
Variable Scope Instance Variable (a.k.a. “dynamic” variable, field) Method/constructor Parameters Local Variables
02/14/2005 CSE 110 7
ExpressionsAn expression has a value. Some examples: Arithmetic
3 + 5 X * 2
Relational >, >=, <, <=, ==, !=
Boolean true, false, &&, ||, !
Assignment (value is the value assigned) Method call (value is the value returned by method)
02/14/2005 CSE 110 8
Statements
A statement causes an action. Statements end with a semicolon or a {block} Unlike an expression, a statement does not have a value Some examples:
Variable declaration Assignment Method call if while for
02/14/2005 CSE 110 9
Arrays
A way to easily declare many variables of the same type An array is an object Whenever you see [ ] in a Java/C/C++/C# program, that
means array Provide random access Legal indices: 0 through (length - 1) If attempt to access via an illegal index:
ArrayOutOfBoundsException