MT311 Java Application Development and Programming Languages - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

MT311 Java Application Development and Programming Languages

Description:

MT311. Java Application Development and Programming Languages ... Netbeans and JDK 5.0. MIT Scheme. SWI Prolog. Textbooks ... You will use Netbeans as the IDE. ... – PowerPoint PPT presentation

Number of Views:126
Avg rating:3.0/5.0
Slides: 39
Provided by: ouhkworkat
Category:

less

Transcript and Presenter's Notes

Title: MT311 Java Application Development and Programming Languages


1
MT311Java Application Development and
Programming Languages
  • Li Tak Sing(???)

2
Contact information
  • Email tsli_at_ouhk.edu.hk
  • Office Rm A0936
  • Tel 27686816
  • Course Web Site http//plbpc001.ouhk.edu.hk/mt31
    1f
  • Assignment submission http//plbpc001.ouhk.edu.hk
    /tma/login.htm

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

4
Software
  • Netbeans and JDK 5.0
  • MIT Scheme
  • SWI Prolog

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

6
Overview of the course
  • This course have two modules
  • Java Application development
  • Programming Languages

7
Java Application Development
  • Object-oriented Programming in Java
  • Graphical User Interface
  • Network Programming with Java
  • Java Security

8
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

9
Object-Oriented Programming in Java
  • Object-oriented programming has the following
    characteristics
  • Abstract data type
  • Encapsulation
  • Polymorphism
  • Inheritance

10
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

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

12
Java abstract data type
  • public class car
  • Wheel wheelnew Wheel4
  • 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.

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

14
Bank account with setter and getter
  • public class Account
  • int balance
  • void setBalance(int b) balanceb
  • 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.

15
A program that uses Account
  • .....
  • Account accountnew 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.

16
'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?

17
'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.balanceb
  • Usually, we do not need the 'this' keyword if we
    just want to access the attributes of the
    associated object.

18
'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.aa

19
'this'
  • You want to pass the associated object to another
    method
  • public class ABC
  • ...
  • void method(..)
  • DEF defnew DEF()
  • def.meth(this)
  • ...

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

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

22
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

23
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 studentnew student()
  • student.name"Chan Tai Man"
  • Student.averageAge23
  • ....

24
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.averageAge24
  • Student.averageAge32

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

26
Static attributes
  • public class ABC
  • static int def
  • public void meth()
  • def4
  • 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.

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

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

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

30
Static methods
  • public class Student
  • String name
  • Date birthday
  • static int averageAge
  • static void setAverageAge(int i)
  • averageAgei

31
Static methods
  • The following code contains errors
  • public class Student
  • String name
  • Date birthday
  • static int averageAge
  • static void setName(String n)
  • namen

32
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.namen
  • A static method cannot directly access non-static
    attributes of the class. But it can access it
    through an instance of the class.

33
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)
  • ....

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

35
Main method
  • public class Student
  • String name
  • Date birthday
  • static int averageAge
  • public static main(String st)
  • Student studentnew Student()
  • student.name"Li Tak Sing"
  • averageAge23
  • student.averageAge23
  • Student.averageAge23

36
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 commandjava Student

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

38
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.
Write a Comment
User Comments (0)
About PowerShow.com