Title: CCP111N Java Reminder Week 1
1CCP111N Java Reminder Week 1
2Aims
- Give overview of concepts addressed in Web based
programming module - Teach you enough Java to write simple web and
network applications
3Useful Resources
- These slides are the course book
hopelive.hope.ac.uk/imc/kingj - The BlueJ development environment is available
free from www.bluej.org - The blueJ book is useful if you want additional
practical exercises. Barnes, D and Kolling, M.
Objects first with Java A practical
introduction using BlueJ. Prentice Hall. 2003 - http//java.sun.com This site contains a freely
downloadable version of Java for most operating
systems and computer hardware. It also contains
the very useful Java tutorial and Java language
documentation.
4Advice
- You cant learn to program just by understanding
the theory - Programming requires practice and more practice.
- Dont give up because it is not easy to start
with once you grasp the concepts it will get
easier - Dont 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 dont point you to the cause of the
problem
5A few Words about the Labs
- The labs are designed for you to learn how Java
program behave and experiment and have fun - You 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 do - All the examples are carefully chosen to
illustrate a point. Dont worry that they are not
all useful programs
6Objects
Basic Java Language Section
7 Object oriented concepts Objects
- Objects model real world entities such as a
person, food, car, house, umbrella - Objects 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
8Anatomy 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
9Anatomy of an object
- The behaviours of on object such as change
address are implemented inside methods - Methods are bundles of code
- When invoked the code in a method is executed
line by line - Methods may examine and modify any of the
attributes inside the object they are part of
10Java Language Structure
Basic Java Language Section
11Java Structure Based around blocks
- Blocks are defined between a and a
- must be balanced by a
- A Method is a block with a name
- Attributes also require names
- Names cannot contain spaces or start with a
number such as 2 - A block may be nested totally inside a block but
no block may be partially inside another block
12Java Class Example
Name of class
class Hero int bullets void shoot()
bulletsbullets-1
Name of attribute
Indicates start of class
Name of method
Indicates start of method
Indicates end of method
Indicates end of class
13Java Class Example2
This is the body of the class it contains one
method and one attribute
class Monster int health void
take_damage() healthhealth-10
This is the body of method it contains one line
of code and is inside the body of the class
14Java 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
15Object oriented conceptsWhat is a program?
- A program is a collection of objects (possible
many different types) that interact together by
calling each others 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)
16Object oriented conceptsTerminology
- Invoking a behavior by calling a method
is referred to as sending the object a message - The set of messages an object understands is
called its protocol - Sending an object a message it does not
understand usually results in a error and your
program stopping/crashing
17Messages and Objects
Basic Java Language Section
18Simple Messages
- Sometimes receiving a message is enough
information to perform your behaviour - For instance receiving a message that it is your
birthday is enough to increment your age
19Object oriented conceptsObjects Java Example
Name of method
class Monster int health void
take_damage() healthhealth-10
Indicates its a simple message
20Messages 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)
21Objects More complex Messages
Type of parameter (more about types later)
Name of parameter
- class Monster
-
- int health
- void take_unusual_damage(int damage)
-
- health health - damage
-
-
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
22Creating Instances of Objects
Basic Java Language Section
23Modelling multiple People/entities/things
- Often you need to model lots of monsters such as
in a castle - It would be inefficient to separately write code
for each Monster you will use - We can manufacture multiple instances of a class
such as Monster easily - The class is a template for manufacturing
instances
24Object oriented conceptsManufacturing Instances
Name of class to create an instance of
Name of object to store the instance
- Monster snotty new Monster()
- Monster grotty new Monster()
The type of this instance (should be same as Name
of class on the right of the new)
Note Java is case sensitive. By convention
instances of classes start with a lower case
letter and classes with an upper case letter
25Object oriented conceptsInstances
- An instance is an object of a particular class
- e.g grotty and snotty are the names for the
instances of the Monster class we created - Each instance has the same behavior
- Each instance has its own independent copy of the
attributes - When we create an instance of a class the
attributes can be given starting values (about
this more later)
26Sending messages to an instance
- 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!
Calls snottys take damage with no parameters
Calls grottys take unusual damage with a
parameter value 200
27How Parameters Work
Damage is initialised to 200 I.e. the value of
the parameter take_unusual_damage was called with
- class Monster
-
- int health
- void take_unusual_damage(int damage)
-
- health health - damage
-
-
- snotty.take_unusual_damage(200)
- // More code
Then the code inside the method is run
When this line is executed control jumps to the
take_unusual_damage method inside the snotty
instance of Monster
When the method has been completed the next line
after the call to the method is run
28Initialising Instances during creation
Basic Java Language Section
29Initialising 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 class - This special method is called the constructor
30Initialising 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