* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download method
Survey
Document related concepts
Abstraction (computer science) wikipedia , lookup
Java syntax wikipedia , lookup
Join-pattern wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Structured programming wikipedia , lookup
Scala (programming language) wikipedia , lookup
Resource management (computing) wikipedia , lookup
Go (programming language) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Application Interface Specification wikipedia , lookup
Java (programming language) wikipedia , lookup
Design Patterns wikipedia , lookup
Name mangling wikipedia , lookup
Java performance wikipedia , lookup
Object lifetime wikipedia , lookup
C Sharp syntax wikipedia , lookup
Transcript
Programming James King 12 August 2003 1 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications 2 Warning THIS IS NOT A WEB AUTHORING COURSE We will not teach HTML, PSP, etc… We will show you how to write Java applications that use the web and internet We will (hopefully) improve your programming ability We will teach Object orientated concepts, good programming practice and how to debug/document your code It is vital to bring the code examples with you to the lecture 3 The Course Topics Object orientation and Java programming Applets and GUI programming File handling Input and Output Streams Multiple threads of execution Network programming 4 Useful 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. 5 Advice You can’t learn to program just by understanding the theory Programming 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 easier Don’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 6 the cause of the problem A 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. Don’t worry that they are not all useful programs 7 This Weeks Topics Object Orientated Concepts Objects Methods and Attributes Messages, parameters and protocols Classes Instances Inheritance and overriding 8 Objects Basic Java Language Section 9 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 10 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 11 Anatomy 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 12 Java Language Structure Basic Java Language Section 13 Java 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 14 inside another block Java Class Example Name of class class Hero Name of attribute Indicates start of class { Name of method int bullets; void shoot() { Indicates start of method bullets=bullets-1; Indicates end of method } } Indicates end of class 15 Java Class Example2 class Monster { int health; void take_damage() { health=health-10; } } This 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 16 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 } 17 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 18 damage method) Object oriented concepts Terminology 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 19 Collaborating Objects user hits the mouse button Shoot message bullets = bullets -1 Take damage message Hero object receives a shoot message health = health - 10 Monster object receives a take_damage message 20 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 21 Messages and Objects Basic Java Language Section 22 Simple 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 23 Object oriented concepts Objects – Java Example Name of method class Monster { int health; void take_damage() { health=health-10; } } Indicates it’s a simple message 24 Messages with Parameters Sometimes you need additional information to perform a behaviour. For instance if the monster is hit by a nonstandard 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) 25 Objects – More complex Type of parameter Messages (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 26