![Page 1: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/1.jpg)
1
Programming
![Page 2: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/2.jpg)
2
Aims
Give overview of concepts addressed in Web based programming moduleTeach you enough Java to write simple web and network applications
![Page 3: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/3.jpg)
3
Warning
THIS IS NOT A WEB AUTHORING COURSE We will not teach HTML, PHP, etc…
We will show you how to write Java applications that use the web and internetWe will (hopefully) improve your programming abilityWe will teach Object orientated concepts, good programming practice and how to debug/document your codeIt is vital to bring the code examples with you to the lecture
![Page 4: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/4.jpg)
4
AdviceYou can’t learn to program just by
understanding the theoryProgramming requires practice and more
practice. Don’t give up because it is not easy to start with
once you grasp the concepts it will get easierDon’t be surprised when you make mistakes - learn from them No programming language or environment is perfect sometimes the error messages are difficult to understand or don’t point you to the cause of the problem
![Page 5: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/5.jpg)
5
A few Words about the LabsThe labs are designed for you to learn how Java program behave and experiment and have funYou will learn faster and have more fun if you take the programming examples and modify them to see what happens. You will get used to the error messages
Try to run them in your head or on paper before you run them on the computer you will get a feel for what the language can and can
not doAll the examples are carefully chosen to illustrate a point. Don’t worry that they are not all useful programs
![Page 6: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/6.jpg)
6
This Weeks Topics
Object Orientated Concepts Objects Methods and Attributes Messages, parameters and
protocols Classes Instances
![Page 7: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/7.jpg)
7
Objects
Basic Java Language Section
![Page 8: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/8.jpg)
8
Objects model real world entities such as a person, food, car, house, umbrellaObjects only need to model the “interesting” information and behaviours For a personnel database person may contain
name, address, age, current wage etc. and behaviours such as change address, promotion
In a computer game health, current weapon and direction may be important and behaviours such as shoot, walk, run and change direction
Object oriented concepts
Objects
![Page 9: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/9.jpg)
9
Anatomy of an object - Terminology
The individual pieces of information such as the health of a monster or the number of bullets are called attributes The entire information stored inside an object can be referred to en masse as its state
![Page 10: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/10.jpg)
10
Anatomy of an object
The behaviours of on object such as change address are implemented inside methodsMethods are bundles of codeWhen invoked the code in a method is executed line by lineMethods may examine and modify any of the attributes inside the object they are part of
![Page 11: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/11.jpg)
11
Java Language Structure
Basic Java Language Section
![Page 12: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/12.jpg)
12
Java Structure – Based around blocks
Blocks are defined between a { and a } { must be balanced by a }A Method is a block with a nameAttributes also require names Names cannot contain spaces or start with a number such as 2A block may be nested totally inside a block but no block may be partially inside another block
![Page 13: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/13.jpg)
13
Java Class Example
class Hero
{
int bullets;
void shoot()
{
bullets=bullets-1;
}
}
Name of class
Indicates start of class
Indicates end of class
Indicates start of method
Name of method
Indicates end of method
Name of attribute
![Page 14: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/14.jpg)
14
class Monster
{
int health;
void take_damage()
{
health=health-10;
}
}
Java Class Example2This is the body
of the class it contains one
method and one attribute
This is the body of method it
contains one line of code and is
inside the body of the class
![Page 15: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/15.jpg)
15
Java Structure – Based around blocks
{}
{}
{
}
{}
{
}{
}
One block followed
by another block is
fine
One block inside by another block is
fine
One partially inside by another block is
an error
![Page 16: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/16.jpg)
16
Object oriented concepts
What is a program?A program is a collection of objects (possible many different types) that interact together by calling each other’s methods.For example in a computer game if the hero shoots a monster several methods are called:
The hero's gun uses one bullet (shoot method)The monster loses health (take damage method)
![Page 17: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/17.jpg)
17
Object oriented concepts
TerminologyInvoking a behavior by calling a method is referred to as sending the object a messageThe set of messages an object understands is called its protocolSending an object a message it does not understand usually results in a error and your program stopping/crashing
![Page 18: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/18.jpg)
18
Collaborating Objects
bullets = bullets -1
Shoot message
health = health - 10
Take damage message
user hits the mouse button
Hero object receives a
shoot messageMonster object
receives a take_damage
message
![Page 19: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/19.jpg)
19
Objects Summary
The state of the object is stored inside the object in attributes e.g. the age of the person
Responses to messages are stored as executable code (the code associated with a message is called a method).
The set of messages an object can understand is its protocol
![Page 20: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/20.jpg)
20
Messages and Objects
Basic Java Language Section
![Page 21: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/21.jpg)
21
Simple Messages
Sometimes receiving a message is enough information to perform your behaviourFor instance receiving a message that it is your birthday is enough to increment your age
![Page 22: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/22.jpg)
22
class Monster
{
int health;
void take_damage()
{
health=health-10;
}
}
Object oriented concepts
Objects – Java Example
Indicates it’s a simple message
Name of method
![Page 23: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/23.jpg)
23
Messages with Parameters
Sometimes you need additional information to perform a behaviour. For instance if the monster is hit by a
non-standard weapon we need to know how much damage the weapon did.
This additional information is presented as one or more parameters (also called arguments in some older books)
![Page 24: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/24.jpg)
24
class Monster
{
int health;
void take_unusual_damage(int damage)
{
health = health - damage;
}
}
Objects – More complex Messages
Name of parameter
The attribute health is modified to take the same value as the
parameter minus its old value. When modified its overwritten its old value
is then lost forever
Type of parameter (more about types
later)
![Page 25: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/25.jpg)
25
Creating Instances of Objects
Basic Java Language Section
![Page 26: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/26.jpg)
26
Modelling multiple People/entities/things
Often you need to model lots of monsters such as in a castleIt would be inefficient to separately write code for each Monster you will useWe can manufacture multiple instances of a class such as Monster easilyThe class is a template for manufacturing instances
![Page 27: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/27.jpg)
27
Object oriented concepts
Manufacturing Instances
Monster snotty = new Monster();Monster grotty = new Monster();
Note: Java is case sensitive. By convention instances of classesstart with a lower case letter and classes with an upper case letter
Name of class to create an instance of
Name of object to store the instance
The type of this instance (should be same as Name of class on the right
of the new)
![Page 28: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/28.jpg)
28
Object oriented concepts
Instances
An instance is an object of a particular classe.g grotty and snotty are the names for the instances of the Monster class we createdEach instance has the same behaviorEach instance has its own independent copy of the attributesWhen we create an instance of a class the attributes can be given starting values (about this more later)
![Page 29: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/29.jpg)
29
In Java code we send messages to the names of the instances like this:
snotty.take_damage();
grotty.take_unusual_damage(200);
These messages will change the state of the object snotty by decreasing its health by 10 and grotty by decreasing its health by 200 – OUCH!
Sending messages to an instance
Calls snotty’s take damage with no
parameters
Calls grotty’s take unusual damage with a parameter
value 200
![Page 30: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/30.jpg)
30
How Parameters Work
class Monster{int health; void take_unusual_damage(int damage)
{ health = health - damage;}
}
snotty.take_unusual_damage(200);// More code
When this line is executed control jumps to the
take_unusual_damage method inside the snotty
instance of Monster
Damage is initialised to 200 I.e. the value of the parameter
take_unusual_damage was called with
Then the code inside the method
is run
When the method has been completed the next line
after the call to the method is run
![Page 31: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/31.jpg)
31
Initialising Instances during creation
Basic Java Language Section
![Page 32: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/32.jpg)
32
Initialising Instances
When new is used to create an object the attributes are created.We can initialise its attributes. This is done by creating a method with the same name as the classThis special method is called the constructor
![Page 33: 1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications](https://reader036.vdocuments.site/reader036/viewer/2022062322/5697bff31a28abf838cbc86d/html5/thumbnails/33.jpg)
33
Initialising Objects
class Hero{int bullets;// attribute (state)Hero() // constructor called during new Hero();
{bullets = 10;}
}
h = new Hero(); // more code
Calls the constructor in Hero to initialise the
instance before storing it in h