ITM 352 Object Oriented Concepts and software representations (simple Java apps) Lecture #2

Preview:

Citation preview

ITM 352

Object Oriented Concepts and software representations

(simple Java apps)

Lecture #2

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 2

AnnouncementsAssignments

Always posted on web site Always tell exactly what to do, when due Assignment 1 will be posted tonight, due 1/28

Relax! There’s plenty of time to panic later… First lectures ‘ease’ you in to OOP Focus on understanding the concepts and terms

Class web page Is now ready (if you notice problems, let me know

ASAP)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 3

AgendaBrief ReviewPicturesLab 1: Tour of JBuilderLecture:

Tour of basic Java programming Introduction to OOP (if time permits)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 4

More on Class Approach CS concepts and OO are subtle at times Our approach is to expose you to subtle concepts early

and often gestation period is vital - you should “grok”

sometime before the end of the class not much serious programming until later in the semester - after you

have “gestated”

E.g. “known” learning curve for OO is:

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 5

A Brief Review

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 6

What is programming?

Direct computer’s actions in detailUses a language that humans can deal with

and translates into something the computer understands

Builds a computer representation of abstractions (a model) that people value is some way

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 7

Java virtual machine

Different types of machines have different machine languages. For portability, Java is implemented differently.

Java sourceprogram

compiler “Java virtualmachine” code

load

browser (or JVM) running in computerloads and executes JVM code

There are native compilers that directly translate Java into machine language.

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 8

Writing and running Java programs (no IDE)

Edit Java program Hello.java (source file).

Enter command javac Hello.java

(Creates object file, Hello.class.)Enter command

java Hello

(Runs the object file.)

compiler

JVM Interpreter

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 9

Applications vs. applets

Applications: stand-alone programs, usually invoked from command line.

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 10

Applications vs. applets (cont.)

Applets: run within a window in a browser.

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 11

A Tour of Concepts and Software Representations

We’ll go into greater depth on this later, today just some examples

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 12

Robot Concept

What is a Robot? The concept

What is our intent for this? set the domain (scope) of the concept

What do we need in this for Robot Warz? Abstraction qualities (assumptions, simplification)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 13

Robots

Robots can:

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 14

Robots

Robots can: move detect things talk and listen pick up and drop things fire a weapon (ouch!)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 15

Robot Qualities move

forward rotate left, right

detect things radar 0-360 degrees scan at a location

talk and listen broadcast and receive

live in a “world” (environment)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 16

Robot World Robots can not move

past walls Robots have limited

energy supply Robots can only move

one unit at a time A robots movement is

restricted by its size and weight

These are environmental

constraints

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 17

Programming: Moving a Robot

Task {Karel.move(); // lets take our first step

}

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 18

Programming: Moving around a wall

Task { // move around wallKarel.move();

Karel.turnRight();

Karel.move();

Karel.move();

Karel.turnLeft();

Karel.move();

}

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 19

Programming: algorithms

Task { // move around wallKarel.move();

Karel.turnRight();

Karel.move();

Karel.move();

Karel.turnLeft();

Karel.move();

}

May not work!!!

?

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 20

Programming: algorithm pseudocode

Turn right;

If facing a wall? then

turn left and if facing a wall? then

turn left and if facing a wall? Then

turn left and if facing a wall? Then

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 21

Programming: algorithm in Karel language

Karel.turnRight();

if ( Karel.isFacingWall() ) {

Karel.turnLeft();

if ( Karel.isFacingWall() ) {

Karel.turnLeft();

if ( Karel.isFacingWall() ) {

….

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 22

Programming: compactness

Karel.turnRight();

while( Karel.isFacingWall() ) {

Karel.turnLeft();

}

Karel.move();

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 23

Programming: completion

If ( Karel.InBox() ) {

Karel.turnRight();

while( Karel.isFacingWall() ) {

Karel.turnLeft();

}

Karel.move();

}

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 24

Qualities of algorithms

An algorithm must:

Be unambiguous Executable (compact)Terminate (converge)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 25

Compiling and running Java programs

Edit Java program Hello.java (source file).

Enter command javac Hello.java

(Creates object file, Hello.class.)Enter command

java Hello

(Runs the object file.)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 26

The Hello! application

class Hello {

// Author: Dr. Chandra

public static void main (String[] args) {

System.out.println(“Hello!”);

} // end of main

} // end of class

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 27

Source file layout

Comments: // to end of line - can go anywhere, and can be omitted

Spaces and line breaks not significant:

Line breaks act like spaces, but are not allowed inside double quotes.

class Hello { public static voidmain(String[] args){System.out.println(“Hello!”);}}

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 28

Source file layout (cont.)

Choose layout for readability.Note how brackets of various types tend to

match up - layout should make this clear

class Hello { // Author: Dr. Chandra public static void main (String[] args) { System.out.println(“Hello!”); } // end of main} // end of class

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 29

Elements of a simple Java application

A simple Java application contains one class definition: class Whatsitsname { ... }

in a file with the .java extension:

Whatsitsname.java.Within the class brackets is a “main method”:

public static void main (String[] args) { ... }

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 30

Elements of a simple Java application (cont.)

Within the brackets of the main method are declarations and executable statements.

The Hello application has no declarations and just one executable statement:

System.out.println(“Hello!”);

This says to print the word Hello! on the monitor.

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 31

Another simple application

public class Area {

public static void main (String[] args) {

double radius;

radius = 4.5;

System.out.print("Radius = 4.5, area = ");

System.out.println(Math.PI*radius*radius);

}

}

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 32

Another simple application (cont.)

Compiling and executing produces this output: Radius = 4.5, area = 63.61725123

Note notation for real numbers println can print either strings or

numbers. print is the same, but does not skip a line.

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 33

Assignment 1

Read Savitch: Ch.2.1, 2.2, 2.3

Compile and run “Hello, I’m feeling…” application

Start Lab 1 exercise (posted on web site tonight)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 34

Why Do We Want Software? What makes computers useful?

Can faithfully represent a conceptual system in a particular

context outside of real time/space.

Computers support software models.

Software implementations are models of (real-world)

conceptual systems.

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 35

Realms

Systems start as people conceived ideasThe task of development is to represent

concepts with technologyDevelopment engineering processes move a

concept from the Realm of Actions (concepts) to the Realm of Representations) (technology)

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 36

Realm of Actions to Realm of Representations

Technology System Realm of

representations - •Technology•Specific

Realm of actions and possibilities - •People•General

System Concept

A translation process:

- informal to formal

- abstract to concrete

- course grain to fine grain

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 37

People vs. Computers

Computers•Linear•Concrete•Discrete•Context Free•Passive•Logical•Consistent•Need Batteries

People•Non-linear•Abstract•Continuous•Context Sensitive•Active•Creative•Inconsistent•Need Donuts

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 38

People vs. Computers (cont.)

People•Semantic•Highly Parallel (small tasks)•Approximate (PAC, PACE)•Learn•Do as they want•Make Choices•Flexible (usually)•Desires power•Informal

Computers•Syntactic•Sequential•Exact •Represent•Do as they are told•Compute•Rigid•Needs power•Formal

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 39

Data vs. Information

Data: collection of facts or results Ex. Raw scores on an exam

Information: processed data (significance is assigned by an audience) Has an intended meaning with context and

relationships directed toward a particular audience

Ex. Average score on an exam

1/16/03 ITM 352 - Spring, 2003 - © Port Intro to OOP - 40

Example: Java Data and Information

Data Object name and fields Event objects Interface

implementation Argument to a method Attribute value Exception Objects

Information Result of some

computations Typing an object with

an interface Caller of a method Attribute owner Exception name used

in a catch block