02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

9
02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review

Transcript of 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

Page 1: 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

Page 2: 02/14/2005 Introduction to Programming with 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

Page 3: 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

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”)

Page 4: 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

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

Page 5: 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

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

Page 6: 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

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

Page 7: 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

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)

Page 8: 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

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

Page 9: 02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.

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