23
M1G413283 Introduction to Programming 2 1. Designing a program

M1G413283 Introduction to Programming 2 1. Designing a program

Embed Size (px)

Citation preview

Page 1: M1G413283 Introduction to Programming 2 1. Designing a program

M1G413283

Introduction to Programming 2

1. Designing a program

Page 2: M1G413283 Introduction to Programming 2 1. Designing a program

Teaching staff

Jim Paterson Room [email protected]

Katrin HartmannRoom 611

Introduction to Programming 2 1. Designing a program2

Page 3: M1G413283 Introduction to Programming 2 1. Designing a program

Online resources

Blackboard Announcements Assessment information

Course website Can be accessed through Blackboard www.paterson.co.uk/gcal/intro2prog2.shtml Lecture notes, lab & tutorial sheets, etc Links to other resources

Introduction to Programming 2 1. Designing a program3

Page 4: M1G413283 Introduction to Programming 2 1. Designing a program

Reading

No set text – notes will be provided The following books are recommended as

additional reading: Objects First with Java: A Practical Introduction

Using BlueJ by David J. Barnes and Michael Kolling (ISBN 0137005628)

Studying Programming by Sally Fincher (ISBN 1403946876)

Introduction to Programming 2 1. Designing a program4

Page 5: M1G413283 Introduction to Programming 2 1. Designing a program

Assessment

Class test Lab exercises PeerWise

Introduction to Programming 2 1. Designing a program5

Page 6: M1G413283 Introduction to Programming 2 1. Designing a program

Models in software development

A software system can be modelled using, for example, class diagrams

A model of the data in the system can be used to help design a database

A model can also be used to help design and create the software application, or program, which actually carries out the functions of the system

Introduction to Programming 2 1. Designing a program6

Page 7: M1G413283 Introduction to Programming 2 1. Designing a program

Java programs

A Java program can be: A few lines of code which perform a single

function, for example to add two numbers A large enterprise system consisting of many

Java classes which provide very complex services

Anything in between...

Introduction to Programming 2 1. Designing a program7

Page 8: M1G413283 Introduction to Programming 2 1. Designing a program

Java programs

User interface can be: Windows application Web pages Web page applets Phone applications Games Embedded Command line etc...

Introduction to Programming 2 1. Designing a program8

Page 9: M1G413283 Introduction to Programming 2 1. Designing a program

Object-oriented programs

Java is an object-oriented programming language

When an object-oriented program is running, it creates objects which work together, or collaborate, to perform the required actions

These objects often represent entities in the system

GCU Tours system has objects representing customers, bookings, and so on

Introduction to Programming 2 1. Designing a program9

Page 10: M1G413283 Introduction to Programming 2 1. Designing a program

Classes and objects

An OO program creates objects as it runs Nees to have a template which specifies

what type of object will be created and what that type of object can do

This template is called a class In fact, when you write a program, you are

actually writing the templates An object is a single instance of a class –

there can be many objects of the same type

Introduction to Programming 2 1. Designing a program10

Page 11: M1G413283 Introduction to Programming 2 1. Designing a program

Class contents

A class specifies the following for the objects it is used to create: The name of the type of object (e.g. Customer) The properties which each object will have (e.g.

name) The actions which each object can perform (e.g.

change password) The way in which each object is linked with

other objects (e.g. with Booking objects)

Introduction to Programming 2 1. Designing a program11

Page 12: M1G413283 Introduction to Programming 2 1. Designing a program

Other languages

Other popular object-oriented languages include C#, Visual Basic.NET, C++, Python

In your university career you will come across a number of languages

Many of the techniques which you learn with Java have equivalents in other languages

Once you learn Java it is relatively easy to adapt to others.

Introduction to Programming 2 1. Designing a program12

Page 13: M1G413283 Introduction to Programming 2 1. Designing a program

What do classes represent?

Information May be information which needs to be stored

permanently - database

Introduction to Programming 2 1. Designing a program13

-name-address-username-password-datejoined

User

-location-name-description-adultprice-childprice-departure

Package

-departuredate-offer

Tour

-adults-children-bookingdate-status

Booking

0..*

1..1

1..1 0..*

1..1 0..*

Page 14: M1G413283 Introduction to Programming 2 1. Designing a program

What do classes represent

Not all classes represent information Other roles for classes:

Getting input from the user and displaying output Controlling the flow of activity while the program

runs Other specialised tasks for example formatting

output, getting information into and out of the database, and so on

Introduction to Programming 2 1. Designing a program14

Page 15: M1G413283 Introduction to Programming 2 1. Designing a program

The GCU Adventure Game

Simple example of a system which uses objects which do a range of jobs and work together as a complete program

The GCU game is a text adventure game (interactive fiction)

Inspired by Hitchhikers’ Guide to the Galaxy game (but not as exciting!)

Borrowed liberally from example in Barnes and Kolling book

Introduction to Programming 2 1. Designing a program15

Page 16: M1G413283 Introduction to Programming 2 1. Designing a program

Description of the game

The game centres around characters travelling through a world which consists of rooms

Each room has a description Rooms have exits which lead to other rooms Each room can contain several items which

may be used by a character when in that room

Introduction to Programming 2 1. Designing a program16

Page 17: M1G413283 Introduction to Programming 2 1. Designing a program

Description of the game

An item will also have a description A player navigates through a world by typing

commands (go north, go south, quit, and so on)

The game can be played by several players

Introduction to Programming 2 1. Designing a program17

Page 18: M1G413283 Introduction to Programming 2 1. Designing a program

Objects in the game

game – this will be an object which sets up the game world and controls the game play There will be only one Game object

room – this will be an object which represents a room in the game world There may be many rooms.

item – this will be an object which represents an item in a room There may be several items in each room.

Introduction to Programming 2 1. Designing a program18

Page 19: M1G413283 Introduction to Programming 2 1. Designing a program

Objects in the game

description – this simply describes a room or item a property rather than an object in its own right

exit – an exit from a room is really just a way into another room exit is actually a way of referring to a room

object player – this will be an object which

represents a player player will be in one room at any time

Introduction to Programming 2 1. Designing a program19

Page 20: M1G413283 Introduction to Programming 2 1. Designing a program

Objects in the game

There may be several players in the game. Each player should have a name, which is a

property of the object. command – this will be an object as the job

of representing players’ actions may be quite complicated one command object for each input entered by

a player. Others may be required as program develops

Introduction to Programming 2 1. Designing a program20

Page 21: M1G413283 Introduction to Programming 2 1. Designing a program

Model for the game

Class diagram shows classes and properties identified, and suggests how they may be linked, or associated

Introduction to Programming 2 1. Designing a program21

Game

-name

Player

-description

RoomCommand

-exit

-description

Item

Page 22: M1G413283 Introduction to Programming 2 1. Designing a program

Importance of the class diagram

While it is usually not the only part of a model of a system, the class diagram is very important when you start to build the system as a Java program

The classes become the Java classes which you need to write

Other diagrams are also useful, e.g. Activity, use case, sequence

Introduction to Programming 2 1. Designing a program22

Page 23: M1G413283 Introduction to Programming 2 1. Designing a program

What’s next?

Build incrementally a working implementation of the “World of GCU” adventure game

As we do so you will learn about some new programming techniques and tools as they are needed

You will also learn how the associations between classes in the game are implemented in Java

Introduction to Programming 2 1. Designing a program23