Lecture notes 1 - University of Essex

Download Report

Transcript Lecture notes 1 - University of Essex

CE203 - Application Programming
Ian Daly
[email protected]
Autumn 2016
CE203 Part 0
1
Module Administration
• 10 lectures (2 hours each): Tuesday 16:00 (LTB 7)
• 20 labs (2 hours each) Thursday/Friday (Lab 1)
– Group LABa01: Friday 16:00 – 18:00 (weeks 2-11)
– Group LABa02: Thursday 13:00 – 15:00 (weeks 2-11)
• 2 assignments:
– Assignment 1 (10%) due 9th November (week 6)
– Assignment 2 (20%) due 12th December (week 11)
•
•
•
•
Test (10%) on 15th 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/
• Office hours:
Autumn 2016
Wednesday / Thursday 9:00 – 10:00, room 5A.534
CE203 Part 0
2
Acknowledgements
•
•
•
•
A lot of the material was prepared by Mike Sanderson
Some parts were prepared by Martin Waite
Additional material by Udo Kruschwitz
Further additional material by me
Autumn 2016
CE203 Part 0
3
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 2016
CE203 Part 0
4
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)
• Java for everyone (2nd ed.), C. Horstmann (John Wiley
and sons, 2013)
Autumn 2016
CE203 Part 0
5
Other resources
• Lynda, online Java learning
https://www.lynda.com/
Login with your Essex account.
Autumn 2016
CE203 Part 0
6
Motivating Examples
Research: IBM Watson. ..
Medical research.
Career.
Essex scheduler.
Gaming.
BCI
Tetris =>
Competitions.
Autumn 2016
CE203 Part 0
7
AUTHORSHIP AND PLAGIARISM
Plagiarism is the term used to describe the misuse of authorship. It is a serious
academic offence and is treated as such by the University of Essex.
Types of plagiarism:
• Copy someone else's work as if it were your own;
• Copy sections of someone else's work but change the odd word or phrase;
• Submit the same piece of work for two different assignments, even if they are to
different modules or even to different departments;
• Submit written work produced collaboratively, unless this is specifically allowed;
• Copy the work of another student, even if they have consented;
• Collusion (The passing off of another's work as one's own for one's own benefit and
in order to deceive another).
Common excuses:
Reasons given for committing plagiarism vary from fairly innocent or accidental
mistakes to a deliberate intention to deceive. Unfortunately, no allowance is made for
whether the act was intended or unintended, according to the University definition of
plagiarism and Academic Offences Procedure (see next slides).
University’s Website on Plagiarism
Please, visit these websites for more information:
http://www.essex.ac.uk/ldev/resources/plagiarism/default.aspx
In order to access the University’s materials on plagiarism
guidance, click on the marked (red) link shown above to get
to Moodle page, click “Enrol”, then navigate the webpage on
“Academic Integrity and Plagiarism”.
University’s Website on Academic Offences
What will happen if a student commits an academic offence?
Please, visit this website for more information:
http://www.essex.ac.uk/about/governance/policies/academic-offences.aspx
For details read this document.
Learning Outcomes
1. Demonstrate a knowledge of core Java application
libraries
2. Explain the event-driven model underlying Java GUIs
3. Write Java programs with interactive graphical user
interfaces (GUIs)
4. Write Java programs that interact with databases
5. 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 2016
CE203 Part 0
11
Brief Outline
•
•
•
•
•
•
•
Review of object-oriented programming with Java
Applications and Graphical User Interfaces (GUIs)
Exceptions
Collections
Java database connectivity (JDBC)
Security
Guest lecture(s)
Autumn 2016
CE203 Part 0
12
IDEs
• Integrated development environment
– Many available
• No need to use 1 specific environment.
– Code should work across environments
• However,
– IntelliJ works well.
Autumn 2016
CE203 Part 0
13
Today: A Bit of Revision
• What you should know by now
• A conceptual picture of object-oriented programming
• Understanding principles helps to view overall picture
Autumn 2016
CE203 Part 0
14
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 2016
CE203 Part 0
15
Code style
•
•
Code should be human-readable
Compare…
Class doMath
{
public int sum( int a, int b )
{
return a + b;
// Sum a and b together and return.
}
}
with
Class doMath{ public int sum(int a,int b){return a+b;}}
Use sensible variable / method / class names e.g. doMath vs dM
Why?
So others can read your code.
So you can read your code.
Finally: Add comments!
Autumn 2016
CE203 Part 0
16
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 2016
CE203 Part 0
Class
Data
Procedures
17
Classes vs. Objects
• “class” is a blueprint. Defines the data and functionality
(methods) of the object.
• “object” an instance of a class that exists at runtime. Each
object is defined by a class.
Autumn 2016
CE203 Part 0
18
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 2016
CE203 Part 0
19
Object Orientation - Methods
• Methods can also be defined at either class or instance
level
• Class methods are static
• Instance methods are not
• 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 2016
CE203 Part 0
20
Object Orientation – Information Hiding
• External access to an object’s attributes and methods is
controlled by means of the scope modifiers
• Private
• Public
• Protected
• Direct access to attributes is usually prohibited
• Access is usually gained through secure public methods
Autumn 2016
CE203 Part 0
21
Object Orientation:
Composition & Specialisation
Autumn 2016
CE203 Part 0
22
Parameterisation and References
• In Java, objects are “reference types” (unlike primitive
data types!)
• 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 2016
CE203 Part 0
23
Method Overloading
We can supply several definitions for a method within the
same class definition.
(as long as each definition has a different set of input types.)
int sum (int i, int j)
{return i + j;}
// version 1
double sum (double e, double f) // version 2
{return e + f;}
int sum (int i, int j, int k)
{return i + j + k;}
Autumn 2016
CE203 Part 0
// version 3
24
Method Overloading 2
sum(4, 5)
sum(4.5, 5.5)
sum(4, 5, 6)
will call version 1 on the previous slide
will call version 2 on the previous slide
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 2016
CE203 Part 0
25
Quiz
• Kahoot…
kahoot.it
Autumn 2016
-
CE203_01
CE203 Part 0
26
Class Definitions
Class definitions provide a unit of scope, they:
 encapsulate data and operations
 specify the attributes and methods of an entire class
 can specify the attributes and methods of instances of
the class
Class definitions are hierarchical:
 can extend existing class definitions
 can inherit or redefine parts of superclass definitions
Autumn 2016
CE203 Part 0
27
Class Definitions 2
The class definition is (in part) a specification for an object
An 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)
(use keyword new to allocate memory 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 2016
CE203 Part 0
28
Class Definitions 3
The class definition is (in part) a specification for the entire
class
 Any member (variable or method) introduced with the
keyword 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 2016
CE203 Part 0
29
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 2016
CE203 Part 0
30
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; classes inherit
attributes of the superclass, their parent class,
and so on.
• every class is derived originally from the class
Object,
– all objects inherit Object’s methods, e.g.
Object
Vehicle
Van
Person
Car
Plane
toString
• inherited members of the superclass can be
redefined in the subclass (variable shadowing,
method overriding)
Autumn 2016
CE203 Part 0
Mini
Sportscar
31
Accessibility
public and private modifiers control access to class,
variable, and method definitions. If omitted, their scope
becomes the enclosing package.
Autumn 2016
CE203 Part 0
32
Accessibility Rules
1. No modifier, access is permitted from anywhere in the
enclosing package.
2. public permits access from beyond the enclosing package.
3. private prevents access from outside the enclosing class.
4. protected prevents access from outside enclosing class and subclasses set.
Class
Package
Subclass (same
package)
Subclass (different World
package)
Public
Protected
No modifier
Private
Autumn 2016
CE203 Part 0
33
Accessibility Examples
• 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.
Autumn 2016
CE203 Part 0
34
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 2016
CE203 Part 0
35
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 2016
CE203 Part 0
36
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 2016
CE203 Part 0
37
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 2016
CE203 Part 0
38
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 used, e.g.:
Autumn 2016
CE203 Part 0
39
Specialisation – Dynamic Binding 2
Example (continued):
Autumn 2016
CE203 Part 0
40
Specialisation – Dynamic Binding 3
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.
Autumn 2016
CE203 Part 0
41
Specialisation – Dynamic Binding 4
Dynamic binding = The decision about which version of the
method to use is made when the method is called at run-time.
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.
Autumn 2016
CE203 Part 0
42
Specialisation – Dynamic Binding 5
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());
Autumn 2016
CE203 Part 0
43
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 2016
CE203 Part 0
44
Quiz
• Kahoot…
kahoot.it
Autumn 2016
-
CE203_02
CE203 Part 0
45
Summary
• Today we…
– Introduced the course
– Presented some examples of Java use
– Revised object orientation concepts with examples
Autumn 2016
CE203 Part 0
46