Transcript MT311-1
MT311
Java Application Development and
Programming Languages
Li Tak Sing(李德成)
Contact information
Email: [email protected]
Office: Rm A0936
Tel: 27686816
Course Web Site:
http://plbpc001.ouhk.edu.hk/~mt311f
Assignment submission:
http://plbpc001.ouhk.edu.hk/tma/login.htm
Continuous assessment
Three assignments each carries 20% of the
final continuous assessment score.
One test that carries 20% of the final
continuous assessment score.
20% comes from the work done during
tutorials.
According to OUHK rules, you need to pass
the continuous assessment in order get a
pass in the course.
Software
Netbeans and JDK 5.0
MIT Scheme
SWI Prolog
Textbooks
Java Programming: Advanced Topics, by Joe
Wigglesworth and Paula McMillan, 3rd Edn,
Thomson
Concepts of Programming Languages, by
Robert Sebesta, 6th Edn, Addison Wesley,
2003.
Overview of the course
This course have two modules
–
–
Java Application development
Programming Languages
Java Application Development
Object-oriented Programming in Java
Graphical User Interface
Network Programming with Java
Java Security
Programming Languages
Introduction to Programming Languages
Variables, Data Types, and Expressions
Control Structures, Subprograms, and
Implementation of Subprograms
Object-Oriented Programming Languages
Functional and Logic Programming
Languages
Object-Oriented Programming in Java
Object-oriented programming has the
following characteristics
–
–
–
–
Abstract data type
Encapsulation
Polymorphism
Inheritance
Tutorials
You will be given a tutorial sheet each time
detailing what you have to do.
You need to write Java programs in the first
semester.
You will use Netbeans as the IDE.
You need to bring either a floppy or a USB
drive to store your works.
You need to upload your work to the web site
at http://plbpc001.ouhk.edu.hk/~mt311f
Abstract data type
Abstraction: to describe an object using the most
important information only.
For example, there are thousands of components of
a car. However, if you are asked to describe a car,
you would not describes it in terms of all of its
component. Rather, you would just select a few
important components to describe it. For example, a
car is something that has wheels, steering wheels,
engine and seats.
Java abstract data type
public class car {
Wheel wheel[]=new Wheel[4];
SteeringWheel steeringWheel;
Engine engine;
}
This is an abstract data type of a car
The car is described in terms of three of its main components: wheel, steering
wheel and the engine.
Java programs are collections of classes
All program codes are stored inside classes
Java is a strong typing language which means that all errors regarding typing
will be reported. You also need to declare the type of all entities before they
can be used.
Java abstract data type
A bank account
public class Account {
int balance;
}
This class now only contains data, no program code. This class contains one field
called balance which is an integer. Usually when you define a class, you also
define some procedures to manipulate the attributes of the class. They are
called methods in OO terms. For example, we can have two methods, one to
get the balance, the other to set the balance.
Bank account with setter and
getter
public class Account {
int balance;
void setBalance(int b) { balance=b;}
int getBalance() {return balance;}
}
For method setBalance, a parameter is passed into the method which is an integer
named b. In fact, the method also have access to another entity, which is the
bank account itself. Each method of a class is associated with an instance of
the class. So in the code inside the method setBalance, we have accessed b,
which is passed as a parameter, and we have accessed balance, which an
attribute of the instance of Account.
For method getBalance, it does not have any parameter, so the code inside
getBalance can only access the content of the associated Account.
A program that uses Account
.....
Account account=new Account(); //create a new account
account.setBalance(100); //set the balance to 100
System.out.println(account.getBalance()); //print the
//balance
....
In the above code, a new instance of Account, account, is created. Then
the setBalance method of account is called with 100 as the parameter.
Then, 100 is assigned to the balance attribute of account.
'this'
Every method of a class is associated with
an instance of that class. For example, in the
method setBalance of Account, we can
access the attributes of the object that is
associated with the method. However, what
can we do to access the object itself instead
of just an attribute?
'this'
We can do that by using the 'this' keyword.
The setBalance method can be rewritten
here using the 'this' keyword:
void setBalance(int b) { this.balance=b;}
Usually, we do not need the 'this' keyword if
we just want to access the attributes of the
associated object.
'this'
You need to use 'this' in the following
situation:
–
there is a name conflict so that you want.
example:
public class ABC {
int a;
void meth(int a) {
this.a=a;
}
}
'this'
–
You want to pass the associated object to another
method:
public class ABC {
...
void method(..) {
DEF def=new DEF();
def.meth(this);
...
}
}
Static attributes
Attributes model the properties of an
instance of a class. However, there are
properties that are those of the whole class,
not just an instance.
For example, assume that we have a class
for OUHK student.
Static attributes
The attribute name, birthday are of course
properties of a particular instance of the
class Student.
However, the average age of all students is
not a property of any particular student.
Instead it is the property of all students. In
Java term, we can say that the average age
of all students is the property of the class
Student.
Static attributes
The property of a class is defined as static
attribute using the static keyword:
public class Student {
String name;
Date birthday;
static int averageAge;
}
Static attributes
A static attribute is the property of a class,
not that of a particular instance. So it is
referenced using the name of the class:
....
Student student=new student();
student.name="Chan Tai Man";
Student.averageAge=23;
....
Static attributes
Note that it is ok to access a static attribute
through either an object or the class. So
both of the following two statements are
correct:
student.averageAge=24;
Student.averageAge=32;
Static attributes
However, even if you access a static attribute
using an instance of the class, the accessed
attribute is still nothing to do with that
instance.
The name of the class is not needed if you
want to access a static attribute of a class
inside a method of the same class.
Static attributes
public class ABC {
static int def;
public void meth() {
def=4;
}
}
The static attribute def of ABC is referenced
inside the method meth of ABC. There is no
need to specify the class name ABC here.
Static attributes
However, if you like, you can replace def in
the above program by any of the followings:
–
–
this.def (This is only valid if it is used inside
a method of ABC)
ABC.def
(This is valid anywhere in your
program)
Static methods
Just like static attributes, we can also have
static methods.
A static method can access all static
attributes of the class.
A static method cannot access non-static
attributes as the static method does not have
an associated instance.
Static methods
Just like static attributes, a static method
should be accessed through the name of the
class. But you can still access it through the
name of an object or using 'this' or even
without any qualifier if inside a method of the
same class. However, in all cases, the static
method is still not associated with any
instance of the class.
Static methods
public class Student {
String name;
Date birthday;
static int averageAge;
static void setAverageAge(int i) {
averageAge=i;
}
}
Static methods
The following code contains errors
public class Student {
String name;
Date birthday;
static int averageAge;
static void setName(String n) {
name=n;
}
}
Static methods
However, the following code is correct:
public class Student {
String name;
Date birthday;
static int averageAge;
static void setName(Student s, String n) {
s.name=n;
}
}
A static method cannot directly access nonstatic attributes of the class. But it can
access it through an instance of the class.
Main program
A main program in Java must be a public and
static method of a class in the following form:
public class Student {
....
public static void main(String st[]) {
....
}
}
Main method
Since a main method is a static method, it
can only access static attributes and
methods of the class.
If you want to access non-static attributes,
you need to create an instance of the class
first and then access them through the
instance.
Main method
public class Student {
String name;
Date birthday;
static int averageAge;
public static main(String st[]) {
Student student=new Student();
student.name="Li Tak Sing";
averageAge=23;
student.averageAge=23;
Student.averageAge=23;
}
}
Compile a Java program
The class must be stored in a file called
Student.java
compile using the command
javac Student.java
A file Student.class will be generated
To run the program, use the command
java Student
Java programs
When a Java program is compile, the
product is a file with extension .class.
The file is in the format known as bytecode.
Bytecode is not machine code. That is to say,
it is not directly executable in any computer.
Instead, it needs an interpreter to interpret
the code and then execute it accordingly.
Java programs
The interpreter is called a virtual machine or
VM in short.
This property has two consequences:
–
–
It runs slower than those programs that are
directly compiled to machine code like C++.
The same bytecode programs can be executed in
many different platforms as long as a VM is
available. This is the reason why Java is so
popular on the Internet.