Introduction to Programming. To gain a sound knowledge of programming principles To gain a sound...

23
Introduction to Programming
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    0

Transcript of Introduction to Programming. To gain a sound knowledge of programming principles To gain a sound...

Introduction to Programming

• To gain a sound knowledge of programming principles

• To gain a sound knowledge of object-orientation

• To be able to critically assess the quality of a (small) software system

• To be able to implement a (small) software system in Java

Module OverviewGOALS

• Objects and classes• Data types • Constructors & methods • Assignment - accessor & mutator methods• Making choices: the conditional statement• Object types & passing by reference• Object interaction• Repetition: using loops• Collections: using array lists and arrays

Module OverviewTOPIC AREAS

Module Text BookBARNES & KOLLING

Available from the university bookshop

David J. Barnes & Michael Kölling

Objects First with JavaA Practical Introduction using BlueJ

Fourth editionPearson Education, 2008

Objects and Classes

Week 1

Software development

• In order to design and build something complex, it is necessary to put some thought into how to break down the design into smaller chunks that are more easily dealt with.

• In software design, the most common methods are called Structured Design and Object-Oriented Design.

• Object-Oriented Design is the most recent type of development and the Java language is itself designed to support this type of design.

Object Oriented Design (OOD)

• Objects • In OOD, a design is made up of a number of objects that may

represent real-life objects and how they interact. • These objects are comprised of two parts: attributes and

methods. Attributes are data items that are associated with the object and methods are pieces of functionality. In other words attributes describe something about the object and methods are things that it can do, or have done to it.

• Classes• In order to use an object we first define what that object is by

means of a class description. A class description defines a type of object in terms of its attributes and methods.

• A class describes what all objects of a particular type have in common. Classes are the blueprints or templates for objects.

• Classes are used to create objects.

OBJECT ORIENTATIONBASICS

OBJECT MODEL

Specifying a class - Dog class what do dogs have in common?

What attributes do dogs have?

What behaviours do dogs have?

Dog class diagram

Dognamecoloursize

runsitbarkbiteeat

attributesfields

behavioursmethods

The Dog class is a template or blueprint for creating dog objects

Dog objects - instances of the class

Dog class

scoobysnoopy

mutleylassie

From the Dog class we can create individual dog objects

Object State

Each object has its own state. Its state is the values that the attributes are set to, which can be different from other objects of the same type

StateState

Methods Methods

DogDog

name scoobycolour brownsize bigrun()sit()bark()

name snoopycolour whitesize smallrun()sit()bark()

I am an object

I am an object

Sending messages to objects - invoking methods

We get objects to do things by calling their methods

snoopy.run()

calls the run() methodon the object snoopy

Passing parameters to methods

Sometimes when we call a method we need to pass some data to the method, to tell it exactly what to do.

snoopy.run(10)

scooby.bark(“loud”)

scooby.sit(), snoopy.run()

actual parameters

Object Orientation Advantages

Reuse - Once we have defined a class we can use it over and over again to create many different objects of that type e.g. Dog class, BankAccount class

Reuse - Once we have defined a class - someone else can use that class to create objects - so we can create class libraries

Encapsulation - we can create objects from a class and use its behaviours without needing to know the internal details of how it works

Reuse - when defining new classes we can compose classes from other existing classes to create complex objects

an object that is made up of other objects!

Wheels, Seats, Chassis, Exhaust, Steering Wheel, etc, etc...

Is this Aston Martin DB4 a Complex Object?

And… a wheel itself is also a complex object!

Tire, Trim, Hub Cap, etc, etc...

WHAT IS A COMPLEX OBJECT?

Object Model

CAR

TIRE

HUBCAP

TRIM

TIRE

HUBCAP

TRIM

TIRE

HUBCAP

TRIM

TIRE

HUBCAP

TRIM

CHASSIS

WHEEL

WHEEL WHEEL

WHEEL

SEAT SEAT

SEATSEAT

REGISTRATIONNUMBER

Object Model

BlueJ Demonstration

• Object – Represents an actual thing!

• Class – ‘Blueprint’ for making an object

• Method – Things an object can do (behaviour)

• Parameter – input required by an object method

• Data type – what type of data is allowed!

Object Model BasicsFUNDAMENTAL CONCEPTS

• Many objects (instances) can be created from a single class.

• An object has attributes (fields), and those attributes have values assigned to them.

• the class defines what fields an object has, but each object stores its own set of values (state)

• Each class has source code (Java) associated with it that defines its fields and methods.

Object Model BasicsOTHER OBSERVATIONS

Object Model BasicsSTATE

Object Model BasicsOBJECT INSTANCES

Required Reading

Objects First With Java – A Practical Introduction using BlueJ

• Chapter 1 pp1-9 (Objects and Classes)