+ All Categories
Home > Documents > CE203 - Application Programming

CE203 - Application Programming

Date post: 23-Feb-2016
Category:
Upload: brenna
View: 48 times
Download: 1 times
Share this document with a friend
Description:
CE203 - Application Programming. Udo Kruschwitz [email protected]. Module Administration. 10 lectures (2 hours each): Monday 9 :00 ( 4.722 ) 10 labs (2 hours each) Mon day/Thursday ( Lab 1 ) 2 assignments (10% each): Assignment 1 due 6th November (week 6) - PowerPoint PPT Presentation
Popular Tags:
36
CE203 - Application Programming Autumn 2015 CE203 Part 0 1 Udo Kruschwitz [email protected]
Transcript
Page 1: CE203 - Application Programming

CE203 - Application Programming

Autumn 2015 CE203 Part 0 1

Udo [email protected]

Page 2: CE203 - Application Programming

Module Administration

• 10 lectures (2 hours each): Tuesday 09:00 (LTB 7)• 10 labs (2 hours each) Tuesday/Wednesday (Lab 1) • 2 assignments:

– Assignment 1 (10%) due 4th November (week 6)– Assignment 2 (20%) due 7th December (week 11)

• Test (10%) on 10th November (week 7)• Two hour exam in May/June (60% of the module credit)• GLAs: Ludvig Kihlman (lzkihl) / Youngjae Song (syoungb)• Module area: http://orb.essex.ac.uk/CE/CE203/

Autumn 2015 CE203 Part 0 2

Page 3: CE203 - Application Programming

Acknowledgements

• A lot of the material was prepared by Mike Sanderson• Some parts were prepared by Martin Waite• Additional material by me

Autumn 2015 CE203 Part 0 3

Page 4: CE203 - Application Programming

Recommended Reading

There are many books that cover the material in this course (see last year’s recommendations in CE152) – I will refer to

Java How To Program (9th ed.), H.M. Deitel & P.J.Deitel (Prentice Hall, 2012)Introduction to Java Programming (8th ed.), Y.D. Liang (Pearson, 2011)

If you choose to buy a different text-book you should check if it uses at least Java 5.0 and covers the material listed in the syllabus in the course catalogue entry for CE203.(In Lab 1 you will find Java 8 installed now).

Autumn 2015 CE203 Part 0 4

Page 5: CE203 - Application Programming

Recommended Reading 2

Other good books:

• Learning Java (4th ed.), P. Niemeyer and D. Leuck (O’Reilly, 2013)

• Java in a Nutshell (5th ed.), D. Flanagan (O’Reilly, 2005)(reference book)

Autumn 2015 CE203 Part 0 5

Page 6: CE203 - Application Programming

Learning Outcomes

1. Demonstrate a knowledge of core Java application libraries

2. Explain the event-driven model underlying Java GUIs3. Write Java programs with interactive graphical user

interfaces (GUIs) 4. Write Java programs that interact with databases5. Write Java programs that make efficient use of the Java

collections package.6. Work with the Java Security Manager to create secure

program policies.Autumn 2015 CE203 Part 0 6

Page 7: CE203 - Application Programming

Brief Outline

• Review of object-oriented programming with Java• Applets• Exceptions• Collections• JDBC• Security• Guest lecture(s)

Autumn 2015 CE203 Part 0 7

Page 9: CE203 - Application Programming

Today: A Bit of Revision

• What you should know by now• Abstract from the language-specific issues to get a more

conceptual picture of object-oriented programming• Understanding the principles helps seeing the overall

picture

Autumn 2015 CE203 Part 0 9

Page 10: CE203 - Application Programming

Java Characteristics• Simple• Object Oriented• Interpreted• Portable• Architecture-neutral• High-performance• Distributed• Robust• Secure• Multi-threaded• Dynamic

( Source: LIANG, Y.D., “Introduction to Java Programming”, 6th Edition, Prentice-Hall, 2007)

Autumn 2015 CE203 Part 0 10

Page 11: CE203 - Application Programming

Object Orientation – Classes & Objects

An object is an instance of a class many objects of the same class can exist in the same

program 

A class can exist in a program even if no instance of it exists some classes (abstract classes) cannot be instantiated at

all

Autumn 2015 CE203 Part 0 11

Page 12: CE203 - Application Programming

Object Orientation - Attributes

• Both class and object can have attributes• Attributes in Java are simply called variables• Class variables are shared by all instances of the class• Instance variables belong to a particular instance (or

object)• The state of an object is given by the values of its instance

variables

Autumn 2015 CE203 Part 0 12

Page 13: CE203 - Application Programming

Object Orientation - Methods

• Methods can also be defined at either class or instance level

• There are basically three kinds of methods access methods (get and set attribute values) service methods (to offer services to other objects) housekeeping methods (for internal use)

Autumn 2015 CE203 Part 0 13

Page 14: CE203 - Application Programming

Object Orientation – Information Hiding

• External access to an object’s attributes and methods is controlled by means of the scope modifiers private, public and protected

• Direct access to attributes is usually prohibited • Access is usually gained through secure public methods

Autumn 2015 CE203 Part 0 14

Page 15: CE203 - Application Programming

Object Orientation: Composition & Specialisation

Autumn 2015 CE203 Part 0 15

Page 16: CE203 - Application Programming

Parameterisation and References

• In Java, objects are “reference types” (unlike primitive data types!) the value passed when the actual parameter is an object is a reference.

•  Parameters of primitive type are always passed using call by value the value associated with the actual parameter cannot be changed

• Parameters that are objects are always passed using call by reference the value(s) associated with the actual parameter can be changed

Autumn 2015 CE203 Part 0 16

Page 17: CE203 - Application Programming

Method Overloading

We can supply several definitions for a method sum within the same class definition, as long as each definition has a different set of input types. 

int sum (int i, int j) // version 1 {return i + j;} double sum (double e, double f) // version 2 {return e + f;}  int sum (int i, int j, int k) // version 3 {return i + j + k;}

Autumn 2015 CE203 Part 0 17

Page 18: CE203 - Application Programming

Method Overloading 2

sum(4, 5) will call version 1 on the previous slidesum(4.5, 5.5) will call version 2 on the previous slidesum(4, 5, 6) will call version 3 on the previous slide This process is known as static binding, because the decision about which version of the method to apply is made at the compilation stage.

Autumn 2015 CE203 Part 0 18

Page 19: CE203 - Application Programming

Class Definitions

Class definitions provide a unit of scope: they encapsulate data and operations on that data they can specify the attributes and methods of an entire class they can specify the attributes and methods of instances of the

classClass definitions are hierarchical:

they can be written as extensions of existing class definitions they can inherit or redefine parts of the superclass definitions

Autumn 2015 CE203 Part 0 19

Page 20: CE203 - Application Programming

Class Definitions 2

The class definition is (in part) a specification for an objectAn object is an instance of a class just as the number 3

is an instance of the type int Objects are created dynamically (at run time) using the

keyword new and come into existence when memory is allocated for them on the heap

Objects cease to exist when they are no longer required (the space they occupied is automatically reclaimed by the garbage collector)

Autumn 2015 CE203 Part 0 20

Page 21: CE203 - Application Programming

Class Definitions 3

The class definition is (in part) a specification for the entire class

Any member (variable or method) introduced with thekeyword static becomes part of the static context,and exists throughout program execution

the class can exist independently of objects of the class static members can be used without creating objects

Autumn 2015 CE203 Part 0 21

Page 22: CE203 - Application Programming

Class Definitions 4

• A static variable is known as a class variable(one set of class variables is shared by all objects of the class)

• A non-static variable is known as an instance variable(each object of the class has its own set of instance variables)

Autumn 2015 CE203 Part 0 22

Page 23: CE203 - Application Programming

Inheritance

The class definition inherits method and variable definitions• methods and variables defined in the superclass are

automatically included in the definitions of its subclasses• the inheritance is transitive, so classes inherit the attributes

of the superclass of their superclass, and so on.• since every class is derived originally from the class Object,

all objects inherit Object’s methods, e.g. toString• inherited members of the superclass can be redefined in the

subclass (variable shadowing, method overriding)

Autumn 2015 CE203 Part 0 23

Page 24: CE203 - Application Programming

Accessibility

The public and private modifiers may be omitted from class definitions and variable and method declarations. By default, their scope becomes the enclosing package.  

Autumn 2015 CE203 Part 0 24

Page 25: CE203 - Application Programming

Accessibility Rules

Autumn 2015 CE203 Part 0 25

1. With no modifier, access is permitted from anywhere in the enclosing package.

2. The public modifier permits access from beyond the enclosing package.

3. The private modifier prevents access from outside the enclosing class.

4. The most restrictive modification applies

Page 26: CE203 - Application Programming

Accessibility Examples

Autumn 2015 CE203 Part 0 26

• An unmodified member cannot be accessed from outside the package even if it is contained in a public class.

• A public member cannot be accessed from outside the package if it is contained in an unmodified class.

 

Page 27: CE203 - Application Programming

Classes and Instances

As soon as the program starts, the class descriptions are activated:

• space for class variables is allocated at a location within static memory

• the space remains allocated to those variables until the program stops

• classes exist throughout program execution• class members (variables and methods) can always be

used

Autumn 2015 CE203 Part 0 27

Page 28: CE203 - Application Programming

Classes and Instances 2

When a new instance (object) is created with new• space for the instance variables is allocated on the heap

(new returns a reference to that space), the reference may be assigned to more than one variable

• if, at any point, there are no “live” references, the space is reclaimed

• objects only exist while there are existing references to them

• instance members (variables and methods) can only be used when an instance exists

Autumn 2015 CE203 Part 0 28

Page 29: CE203 - Application Programming

Specialisation

Specialisation hierarchies and Interfaces are closely related topics

They both provide support for polymorphism(the ability to define methods that can operate on many different types) which is implemented by means of dynamic binding (which means that the decision about which version of the method should be applied is made while the program is executing)

Autumn 2015 CE203 Part 0 29

Page 30: CE203 - Application Programming

Specialisation 2

Redefinition of methods is called method overriding.

A superclass method can be completely redefined, or added to. When adding extra statements to the superclass method, the superclass method is called first using the super reference, e.g. if a superclass method meth is added to in a subclass, then in the subclass the first statement in the meth definition is the call super.meth(), and then further statements are added for the additional subclass code

Autumn 2015 CE203 Part 0 30

Page 31: CE203 - Application Programming

Specialisation – Dynamic Binding

When a method has been redefined (possibly more than once) in a hierarchy of classes, and that method is called on an object, then the closest definition (going up the hierarchy from the object’s class) is applied, for example:

Autumn 2015 CE203 Part 0 31

Page 32: CE203 - Application Programming

Specialisation – Dynamic Binding 2

Autumn 2015 CE203 Part 0 32

Example (continued):

Page 33: CE203 - Application Programming

Specialisation – Dynamic Binding 3

Autumn 2015 CE203 Part 0 33

Now consider the following situation:

It will not be known until runtime whether the user will choose “cat” or “dog”, i.e.the decision about which version of noise to use cannot be made by the compiler.

Page 34: CE203 - Application Programming

Specialisation – Dynamic Binding 4

Autumn 2015 CE203 Part 0 34

The decision about which version of the method to use is made when the method is called at run-time, and is therefore referred to as dynamic binding.

Dynamic binding is possible in Java because the internal representation of an object includes information about its class definition.Dynamic binding supports the following kind of polymorphism.

Page 35: CE203 - Application Programming

Specialisation – Dynamic Binding 5

Autumn 2015 CE203 Part 0 35

Animal[] animalArray = new Animal[4];…animalArray[0] = felix;animalArray[1] = rover;animalArray[2] = daisy;animalArray[3] = nanny;…for (int i = 0; i < 4; i++) System.out.println (animalArray[i].noise());

Page 36: CE203 - Application Programming

Abstract Classes

Abstract classes are partial specifications:• they (typically) contain at least one abstract method

(a prototype method definition with no body)• and they cannot be instantiated.

Subclasses of the abstract class can be instantiated if they provide full definitions for the abstract methods. The top most levels of a large hierarchy may consist of several abstract classes.

Autumn 2015 CE203 Part 0 36


Recommended