7
2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 1 Course info Object-Oriented Design Object-Oriented Programming Software Specification SMD135 Programs and Data Structures Lecture 1 2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 2 Course plan… 2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 3 Teachers LP3 Johan Karlsson Lectures Tomas Johansson Lab assistent Håkan Jonsson Lectures Examiner 2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 4 Lab assignments 6 labs with dead-lines to be carried out in groups of 3. 4 in LP3 and 2 in LP4. Grading takes place in the lab, at a computer, and includes Demostration (test-runs) • Functionality Inspection of the code (bring a print-out + front page) • Soundness Java code conventions Questions to the team members about the program What? Why? How?

Teachers LP3Lab assignments · 2004-01-20 F rel sning 1 SMD135 - H kan Jonsson 9 É but object-oriented programs arenÕt constructed from objects. Objects are the basic building blocks

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Teachers LP3Lab assignments · 2004-01-20 F rel sning 1 SMD135 - H kan Jonsson 9 É but object-oriented programs arenÕt constructed from objects. Objects are the basic building blocks

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 1

Course info

Object-Oriented

Design

Object-Oriented

ProgrammingSoftware

Specification

SMD135 Programs and

Data Structures

Lecture 1

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 2

Course plan…

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 3

Teachers LP3

Johan Karlsson

Lectures

Tomas Johansson

Lab assistent

Håkan Jonsson

Lectures

Examiner

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 4

Lab assignments

• 6 labs with dead-lines to be carried out in groupsof 3.– 4 in LP3 and 2 in LP4.

• Grading takes place in the lab, at a computer, andincludes– Demostration (test-runs)

• Functionality

– Inspection of the code (bring a print-out + front page)• Soundness

• Java code conventions

– Questions to the team members about the program• What? Why? How?

Page 2: Teachers LP3Lab assignments · 2004-01-20 F rel sning 1 SMD135 - H kan Jonsson 9 É but object-oriented programs arenÕt constructed from objects. Objects are the basic building blocks

5

Datorlab A2506-A2510

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 6

Schedule

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 7

The Object-oriented Model

OOA

OOD

OOP

This course concentrates here

Analyze the problem.

Design a software

Architecture.

Implement in

Code.

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 8

Object-Oriented Design

1. Identify the objects

– Read the specification!

• Nouns = objects

• Verbs = methods

2. Group objects into classes

3. Locate (and design) the necessary classes,

establishing necessary relationsships

Page 3: Teachers LP3Lab assignments · 2004-01-20 F rel sning 1 SMD135 - H kan Jonsson 9 É but object-oriented programs arenÕt constructed from objects. Objects are the basic building blocks

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 9

… but object-oriented programs aren’t constructed from objects.

Objects are the basic building blocks in an O-O program.

Instead, a program consists of one or more classes that define object type.

Each class has two kinds of parts (known as members)

• instance variables determine object state

• methods determine the potential object behavior

Identify the members in the class on the next slide.

Software architecture is determined by classes and their

relationships. Class members, especially public members,

play a key role.

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 10

Classes

• Variables

• Constructor

• Methods

– update

– mutator

– query

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 11

public class PictureFrame { private int width, height; private String material;

/** modifies: width, height, material post: width == w and height == h and material == m */ public PictureFrame(int w, int h, String m) { width = w; height = h; material = m; }

/** post: result == width */ public int frameWidth() { return width; }

/** post: result == height */ public int frameHeight() { return height; } . . .

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 12

PictureFrame

- int width- int height-String material

+ PictureFrame(int w, int h, String m)+ int frameWidth()+ int frameHeight()+ String frameMaterial()

CLASS DIAGRAM - a snapshot of the design

Class name

Instance

variables

methods

What is meant by

+ and -

TwoPictures

- PictureFrame babyPic- PictureFrame graduationPic

+ TwoPictures()+ void setBabyPic(PictureFrame p)+ void setGraduationPic(PictureFrame p)

2

What is the relationship

between PictureFrame

and TwoPictures?

This slide shows some of the TwoPictures design; the next slide showsan implementation of this design.

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Page 4: Teachers LP3Lab assignments · 2004-01-20 F rel sning 1 SMD135 - H kan Jonsson 9 É but object-oriented programs arenÕt constructed from objects. Objects are the basic building blocks

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 13

public class TwoPictures {

private PictureFrame babyPic; private PictureFrame graduationPic;

/** post: babyPic == null and graduationPic == null */ public TwoPictures() { }

/** pre: pf != null post: babyPic == pf */ public void setBabyPic(PictureFrame pf) { babyPic = pf; }

/** pre: pf != null post: graduationPic == pf */ public void setGraduationPic(PictureFrame pf) { graduationPic = pf; }

}

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 14

Object-Oriented Programming

• Classes– Patterns for objects

• General behaviour andrelations

– A set of classes = aprogram

– Are written and foundin text files (usuallystored in file systems)

– Define the type of anobject

• Objects– Exist at run-time only

– Have

• a state stored in variables

• methods who transformthe state (into a final state,the result) based on theway computers are built

– Each object has a specifictype

• Types makes it easier toproduce correct programs.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 15 2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 16

An application begins execution from a method called

main.

The main method must be public, static and void and

have a single parameter of type String[].

If such a main method is stored in Classname.java

it is compiled and run by typing the following shell

commands:

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

How to start executing a Java Program

javac Classname.java

java Classname

A main method is often used merely as a driver = just

starts the real program by creating an object.

Page 5: Teachers LP3Lab assignments · 2004-01-20 F rel sning 1 SMD135 - H kan Jonsson 9 É but object-oriented programs arenÕt constructed from objects. Objects are the basic building blocks

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 17

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics

myBabyPicture myGradPicture

null

null null

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 18

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics

myBabyPicture myGradPicture

null

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 19

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics : TwoPictures

babyPic == null

graduationPic == null

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

myPics

myBabyPicture myGradPicture

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 20

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics : TwoPictures

babyPic

graduationPic == null

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

myPics

myBabyPicture myGradPicture

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Page 6: Teachers LP3Lab assignments · 2004-01-20 F rel sning 1 SMD135 - H kan Jonsson 9 É but object-oriented programs arenÕt constructed from objects. Objects are the basic building blocks

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 21

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics : TwoPictures

babyPic

graduationPic

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

myPics

myBabyPicture myGradPicture

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 22

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics : TwoPictures

babyPic

graduationPic

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

myPics

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 23

Software testing and debugging

• To isolate software faults.

• Read O1.6 in Riley to get a short overview

– Page 461 about software engineering and

document reviews…

– SMD136 Software Engineering

(Programvaruteknik, LP1, Kåre Synnes)

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 24

Software Specification

• To ensure that a program behaves in line

with its specification.

– Requirement specifications, user manuals, on-

line help, object diagrams, class diagrams,

method specifications,… (Fig. O2.1)

Page 7: Teachers LP3Lab assignments · 2004-01-20 F rel sning 1 SMD135 - H kan Jonsson 9 É but object-oriented programs arenÕt constructed from objects. Objects are the basic building blocks

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 25

UML

• Unified Modeling Language

• Class Diagrams

– ”is a…”

– ”has a…”

– ”knows about…”

• Partial specification

2004-01-20 Föreläsning 1 SMD135 - Håkan Jonsson 26

Correctness in practice

• Programming by contract

• Pre and post conditions

– Specify what methods expect and promise

– assert-statement

•java -ea Classfile

• (Exceptions are used to ensure robustness, that the program can handle

the unexpected. More about this next lecture.)