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.

Slides:



Advertisements
Similar presentations
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
Advertisements

OOP - Object Oriented Programming Object Oriented Programming is an approach to programming that was developed to make large programs easier to manage.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 Programming Week 2 James King 12 August Creating Instances of Objects Basic Java Language Section.
From Class Diagrams to Databases. So far we have considered “objects” Objects have attributes Objects have operations Attributes are the things you record.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Introduction to Programming. To gain a sound knowledge of programming principles To gain a sound knowledge of object- orientation To be able to critically.
Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
State,identity and behavior of objects Sem III K.I.R.A.S.
Address Book in JAVA By What is Address Book Address Book is book or database used for storing entries called contacts Each contact.
TCU CoSc Introduction to Programming (with Java) Getting to Know Java.
Object Oriented Software Development
1 Programming James King 12 August Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple.
1 Web Based Programming Section 6 James King 12 August 2003.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Programming Writing Java Beginning Java Programs.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Classes CS 21a: Introduction to Computing I First Semester,
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Introduction to programming in the Java programming language.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Programming Week 2 2 Inheritance Basic Java Language Section.
Introduction to Programming Writing Java Beginning Java Programs.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Using Objects 1 of 22 Computer Programming Object Classes.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Chapter 20 Concepts for Object-Oriented Databases Copyright © 2004 Pearson Education, Inc.
Programming. To gain a sound knowledge of programming principles To gain a sound knowledge of object- orientation To be able to critically assess the.
OOPS CONCEPT.  OOPS  Benefits of OOPs  OOPs Principles  Class  Object Objectives.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Computer Systems Nat 5 Computing Science
OOP - Object Oriented Programming
Objects First with Java A Practical Introduction using BlueJ
Computer Systems Nat 5 Computing Science
Objectives State the reasons for the complexity involved in the development of software Define the following terms Objects Classes Messages Methods Explain.
Object Oriented Concepts -I
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Procedural Abstraction Object-Oriented Code
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Objects First with Java A Practical Introduction using BlueJ
Java LESSON 7 Objects, Part 1
Chapter 3 Introduction to Classes, Objects Methods and Strings
Teach A-Level Computer Science: Object-Oriented Programming in Python
Objects First with Java A Practical Introduction using BlueJ
Object Oriented Programming Review
Workshop for Programming And Systems Management Teachers
Object Oriented Programming in java
Java Programming Language
Objects First with Java A Practical Introduction using BlueJ
CMSC 202 Exceptions.
Lecture 1 Introduction to Software Construction
Presentation transcript:

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

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 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

4 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 the cause of the problem

5 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

6 This Weeks Topics Object Orientated Concepts Objects Methods and Attributes Messages, parameters and protocols Classes Instances

7 Objects Basic Java Language Section

8 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 Object oriented concepts Objects

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

10 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

11 Java Language Structure Basic Java Language Section

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 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

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

14 class Monster { int health; void take_damage() { health=health-10; } Java Class Example2 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

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

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)

17 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

18 Collaborating Objects bullets = bullets -1 Shoot message health = health - 10 Take damage message user hits the mouse button Hero object receives a shoot message Monster object receives a take_damage message

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

20 Messages and Objects Basic Java Language Section

21 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

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

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)

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)

25 Creating Instances of Objects Basic Java Language Section

26 Modelling 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

27 Object oriented concepts Manufacturing Instances Monster snotty = new Monster(); Monster grotty = new Monster(); Note: Java is case sensitive. By convention instances of classes start 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)

28 Object oriented concepts Instances 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)

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

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

31 Initialising Instances during creation Basic Java Language Section

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 class This special method is called the constructor

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