programming for beginners martin nelson elizabeth fitzgerald lecture 6: object-oriented programming

13
Programming for Programming for Beginners Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Upload: pauline-welch

Post on 04-Jan-2016

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Programming for Programming for BeginnersBeginners

Martin Nelson

Elizabeth FitzGerald

Lecture 6: Object-Oriented Programming

Page 2: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Revision of Sessions 1-5

So far, we’ve covered: Data types Flow control (if, for, while, do-while) Software Design Software Testing Common syntax (programming grammar) Good coding style (comments, conventions,

indents)

These ideas apply to nearly all programming languages.

Page 3: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Session 6 - aims & objectives

Feedback from evaluation forms

Introduction to Object Oriented Programming Find out what Classes and Objects are Learn the difference between an Object and a

Class Understand why Classes and Objects are

important in object-oriented programming Learn how to create your own Classes in Java

Page 4: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Object-Oriented Programming Object-orientation is an important programming

paradigm Programs are assembled into smaller elements called

Objects, which represent something in the real world. Objects can contain both data and functionality. The source code responsible for defining an object as

called a Class. An object is a specific instance of a class. Object-oriented code should be highly re-usable. Provides a “construction kit” approach to

programming. Most OO programs will contain at least 2 classes.

Page 5: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Classes

A class could be thought of as a user-defined data type, perhaps holding many pieces of information.

For example, we could create a Date class which contains: day (String) date (int) month (String) year (int)

An object is an instance of a class: ‘Today’ is an object of class Date. ‘Last Saturday’ is an object of class Date.

Page 6: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Classes We might want to create a class to represent a

Person: The class might include...

forename (String)

surname (String)

age, in years (int)

birthday (class Date) weekday (String)

day (int)

month (String)

year (int)

One class can re-use another! “Construction kit” approach to programming!

Page 7: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Naming Convention

Classes are usually named with a capital letter. Primitive variables are named with lower case

letters. This makes it really easy to keep track of what is a

class/object and what isn’t. Ever wondered why String has a capital S, while

int, double, boolean are in lower case? String is actually a class. It is quite unusual as it can be used like a primitive

variable – no other class can do this. More about Strings next week...

Page 8: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Using Classes – 1

Define your class: Classes can be defined in the same file as your

application, or in their own file. For now, we will put the class definition above the

application class, in the same file.class Date{ // Define class variables... String weekday; int day; String month; int year;}

class myprog{ public static void main(String[] args) { // My application code... }}

Page 9: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Using Classes – 2

Now to create an object. This is called instantiating the class.

Step 1: Declare an object called ‘today’. This tells the compiler that ‘today’ is an object of class Date.

Date today; Step 2: Instantiate the class using the reserved

word ‘new’. This creates the object in the computer’s memory.

today = new Date(); Or, you can do both steps together if you want...

Date today = new Date();

Page 10: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Using Classes – 3 You can access an object’s member variables using dot

syntax.

So, we could set up the Today object as follows:

Date today = new Date();

today.weekday = “Tuesday”;

today.day = 1;

today.month = “March”;

today.year = 2010;

We could print today’s day variable:

System.out.println(today.weekday);

Page 11: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Using Classes – 4

What if we have an object inside an object?

Imagine we have a class called Person, and an object of class Person called einstein.

Person einstein = new Person();

Let’s set Einstein’s birthday...

einstein.birthday.weekday=“Friday”;

einstein.birthday.day=14;

einstein.birthday.month=“March”;

einstein.birthday.year=1879;

Page 12: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

The purpose of OO programming

Classes should model the real world – thus leading to efficient coding

Classes should be reusable ‘Construction kit’ approach to programming Many useful classes have already been created.

Available in libraries ready for you to use. Complex code can be created easily. We’ll talk about how to use the libraries in Session 8...

Page 13: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Coming up in Session 7...

Adding functionality to classes (methods)

How to get input from the user.