Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Introductory Programming In Java
Comp6700/Comp2140
Week 4, 2016
Henry Gardner/ Alexei Khorev/ Charles Martin
Next week we will be running “little labs” for those of you who are feeling 
lost. These will be posted soon. We will show you some examples today.
Next week is a short week; labs will only be run on Tuesday and 
Wednesday from 11-1pm in N112. All beginning programmers, and 
those feeling lost, are encouraged to attend one of these two labs. If you 
are an experienced programmer, we suggest that you do not need to 
attend these labs and perhaps you could give up your place for someone 
from Monday’s lab. There will be no homework next week.
Homework 3 can be submitted through the Wattle site by 5pm on Friday 
18 March. You do not need to bring it to the labs next week. Just upload 
a single file. If you need to construct an archive of several files, then use 
ZIP. Do not use RAR or other compression protocols.
How are you coping……?
2
The week after next is meant to be your first “mid-semester” lab 
exam. Come to your designated lab session and be prepared to sit 
this exam. We will provide you with tips about this exam soon. 
Alexei and I are considering whether or not it would be a good idea 
to push this lab exam back until the week after Easter – which would 
mean scheduling some lab times later in that week if they are 
available
Assignment 1 has now be set. Alexei will introduce it.
Do your best to keep up with this course. If you are a beginning 
programmer, you will need to keep practicing. Ask questions if you 
are lost. Take advantage of the many resources available to help 
you learn Java. Note that you will often need to refer to online 
tutorials and documentation to make sure that you are getting Java 
syntax correct.
3
Aside: Top-down design
Abstract classes and Interfaces
Today
4
Useful technique for scoping the overall structure of a 
single class or a simple program
Useful technique for designing methods
Consider using diagrams or pseudocode
Top Down Design
5
1) Clearly state the problem
• Can be quite difficult to do!
• Problem statement is often not very clear
• User doesn’t know what they want.
2) Define the inputs required, and the outputs produced
• What data is required? How is it formatted? What is legal and 
not legal input?
• What data is outputted? How is it formatted?
3) Decompose the program into classes and their associated 
methods
• Find classes for major “entities” in your modelling of the 
problem.
• Define what data they will need to store.
• Define what operations will be required for those data.
TDD…Steps
6
4) Design the algorithm that you intend to implement for each 
method
• Look for logical divisions within a method and divide it into 
chunks based on these divisions (“decomposition”). 
• If these chunks are still too large then decompose them again
until your algorithm is composed of many small pieces which 
you understand and which you can implement easily.
• Think how a computer might walk through these small 
chunks step-by-step (“stepwise refinement”). Perhaps write them 
out in “pseudocode”. 
5) Turn these little chunks and the overall program into Java code.
• For professional programmers, this can be the easiest part!
6) Test the Java code; compare with requirements; iterate where 
necessary
TDD…..
7
Pseudocode
A loose mixture of Java and English (or…Mandarin?)
Clearly show which steps are sequential and which are branches 
or loops
Consider which data structures are needed
Example: Binary search in pseudocode
TDD…..
8
It is very common to use both design approaches at the 
same time.
Use top-down to ensure that the overall architecture is in 
place.
Use bottom-up to work on some detailed aspects of the 
code.
Top-Down versus Bottom-Up?
9
What are abstract classes and intefaces?
Not all methods are implemented. (Some default methods can be 
implemented.)
A programmer inherits (“extends” or “implements”) these abstract 
entities and implements the methods to meet their specifications
Why use them?
Saves work! Sometimes you need to leave some work undone for 
a future programmer to complete…
ALLOWS YOU TO SPECIFY REQUIREMENTS ON OTHER 
PEOPLE’S CODE
Client programs
Other subsystems 
How are abstract classes different to interfaces?
Errrr……………
Abstract Classes and Interfaces
10
Capture an abstraction — a state or behavioural feature which is 
common in a family of types, which they all have, but in somewhat 
different (particular) way.
The whole family is characterised by declaring that something is 
done, but not how it is done.
The “how”, ie, the concrete implementation, can differ from one 
(child) type to another. 
In Java, an abstract class usually has one or more declared, but not 
implemented methods which are marked with the keyword abstract 
But you can declare a class abstract without having a single 
abstract method in it! (Why? To encourage the implementer to 
consider non-default implementations…)
Abstract classes cannot be instantiated.
Abstract classes
11
The classes which provide implementation of abstract methods 
(concrete subclasses) must extend the abstract class.
The opposite is also possible: a class can extend a concrete 
superclass and “override” a normal (implemented) method to 
make it abstract (perhaps obscure…?).
Abstract classes…
12
13
Specify a “contract” on implementing classes but without 
(traditionally) any implementation themselves.
Classes can “implement” an unlimited number of interfaces but can 
only “extend” one super class (including abstract classes).
The only members allowed are non-private method declarations and 
constants — static (no instantiation!) and final (but “public static 
final” modifiers can be omitted in declarations). 
Interfaces
14
In the manner of extending abstract class to get complete 
implementation and object creation, an interface must be 
implemented in a concrete class via implementation of every method 
declared in the interface.
If a class implements multiple interfaces, they are declared in a 
comma separated list:
Interfaces…
15
Interfaces define the method signatures, and implementing classes 
must preserve them.
An interface name can be used anywhere a type can be used; 
interface provides the most flexible form of type definition.
Can use an interface type as a formal parameter for a method. 
When the method is executed, an object of a real class, which 
implements that interface, is passed as the actual parameter.
Same principle applies to method return types.
Using interface types alone suffices to compile classes. But the 
code will not run properly unless real, concrete implementations of 
those interface types are available.
Interfaces as types
16
https://docs.oracle.com/javase/8/docs/api/index.html
Runnable: “should be implemented by any class whose instances 
are intended to be executed by a thread.”
Defines one method called “run”
Comparable: “imposes a total ordering on the objects of each class 
that implements it”
Note generic type declaration using 
See https://docs.oracle.com/javase/tutorial/java/generics/types.html
Examples of Interfaces
17
Use keyword “default”
Makes interfaces conceptually similar to Abstract Classes!
Example: 
Interfaces can have default methods
18
Interfaces are extendable (NewInterface extends 
OldInterface) for adding new method declarations 
without breaking existing classes which implement the 
OldInterface type. 
Unlike class extension, multiple inheritance of interfaces 
is allowed (no case for competing implementations, and 
parent interface’s constants get hidden).
Interfaces can extend other interfaces
19
Suppose a class D inherits from two classes C and B where both C 
and B inherit from a common abstract ancestor A.
If C and B implement abstract methods from A in different ways, 
then you might get into problems.
This used not to be possible in Java (but possible in other 
languages) because you can only extend one class.
But you can implement many interfaces.
So you can now have multiple inheritance problems when default 
methods are implemented in Java interfaces. Be aware of this.
Multiple inheritance
20
Interfaces can specify and assume requirements on implementing 
classes
Can separate interface from implementation
As implementations change with technology, their interfaces can 
be held constant and software systems will be stable over time.
Interfaces can be used to package up whole software subsystems, 
while just exposing a small number of methods for other subsytems 
to use. (The Façade design pattern.)
Interfaces and software engineering
21