Topic1 ClassesAndObjects
Topic1 ClassesAndObjects
Topic1 ClassesAndObjects
What are objects and classes? Creating objects multiple instances Interacting with objects methods parameters data types Source code
compilation v. interpretation
Class:
describes, abstractly, all objects of a particular kind (e.g. the class of all cars)
Autumn 2006
Introduction to Programming
Classes
A class describes a kind of object e.g. Car It specifies what sorts of information (attributes) we have about these kinds of object e.g. make, colour, speed, location It specifies the allowable behaviours and operations on these kinds of object (methods) e.g. move, stop, accelerate, wipers on/off
Autumn 2006 Introduction to Programming
Objects
What colour is the car?
which car?
Creating Objects
Shapes Example
Autumn 2006
Introduction to Programming
Fundamental Concepts
class - Circle, Triangle object - triangle1, square23 constructor - new Circle() method - makeVisible() parameter - changeSize(int newSize) field - private String color, private int size data type - String color, int size
Autumn 2006 Introduction to Programming
Autumn 2006
Introduction to Programming
Data Types
Every field has a type specified for it. Each parameter has a type specified for it. So far have met types: int e.g. 50, -3, 0, 129
String e.g. green or Bill or 129 (The quotes are very important)
boolean -- only two possible values: true and false NB: every class also defines a type!
Autumn 2006
Introduction to Programming
red
Autumn 2006
Introduction to Programming
Observations
Many instances can be created from a single class An object has attributes: values stored in fields. The class defines what fields an object has, but each object stores its own set of values the class defines what methods each object (instance) has The class defines special methods to create objects (constructors)
Autumn 2006 Introduction to Programming
Autumn 2006
Introduction to Programming
Autumn 2006
Introduction to Programming
Picture Demo
Autumn 2006
Introduction to Programming
Objects as Values
A field can have an object as its value
The type of such a field is the class of the objects it can take as values
Autumn 2006
Introduction to Programming
Exercise
Read Chapter 1 of Objects First with Java, and do all the exercises. Help will be given in exercise classes in week 2. Spend as much time as is necessary Try and finish before second lab class If you find this easy, read ahead in book!!!
Autumn 2006 Introduction to Programming
Source Code
Each class has source code (Java code) associated with it that defines its details (fields and methods). Source code must be compiled before it can be executed (run)
To create objects (instances of the class) To interact with objects
Autumn 2006 Introduction to Programming
What is Compilation?
Compilation is the process whereby one computer language is translated into another (usually simpler and more low-level i.e. machine-orientated) language.
The program that does the translation is known as a compiler.
Traditionally, programs in a high-level computer language (e.g. Pascal, C, Lisp) are compiled into assembly language (essentially machine code)
Java programs are translated into an intermediate language known as Java Virtual Machine (VM) code.
This VM code is then normally interpreted in order to run the program.
Autumn 2006 Introduction to Programming
What is Interpretation?
A computer can only directly execute programs written in its own machine language.
For many languages, the compiler translates the highlevel language program into machine language which can then be executed by the computer
In the case of Java, the compiler produces intermediate VM code i.e. not actual machine language The VM interpreter is a program (running on the machine) which carries out the intended actions of the VM instructions.
Autumn 2006 Introduction to Programming
An Analogy
When you see (256378 + 5674543) you do not execute this directly in your head. Instead, you follow a procedure (algorithm) for long addition, using pen and paper. In other words you interpret the high-level instruction (add these two numbers together) in order to get the result
Autumn 2006
Introduction to Programming
Modified source
Run-Time Errors
Errors that happen when you run a successfully compile program. There are two kinds:
1.
Those that give a run-time error message because while your program is running it does something illegal (e.g.
dividing something by zero trying to access a non-existent object.
2.
Those that give no error message, but your program simply does not do what you want!
These are the hardest kinds of error to track down.
Introduction to Programming
Autumn 2006
Advice
All programmers make errors, even experienced ones
Do not panic! Do not get depressed!
I doubt if a single large piece of software exists which is error free (bug free)
Autumn 2006
Introduction to Programming
Autumn 2006
Introduction to Programming
Return values
Autumn 2006
Introduction to Programming
Objects as Parameters
A parameter can be given an object as its value The type of such a parameter is the class of the objects it can be given (passed) as its value
Autumn 2006
Introduction to Programming
Exercise
Read Chapter 1 of Objects First with Java, and do all the exercises. Help will be given in exercise classes Try and finish before your second lab class If you find this easy, read ahead in book!!!
Autumn 2006
Introduction to Programming