MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )

Slides:



Advertisements
Similar presentations
MT311 Tutorial Li Tak Sing( 李德成 ). Uploading your work You need to upload your work for tutorials and assignments at the following site:
Advertisements

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Introduction to Java. What is Java? A computer programming language that can be run as either an application or an applet. –What is the difference? It.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Introduction to Methods
BASIC JAVA PROGRAMMING TUTORIAL. History  James Gosling and Sun Microsystems  Oak  Java, May 20, 1995, Sun World  Hot Java –The first Java-enabled.
+ Java vs. Javascript Jessi Style. + Java Compiled Can stand on its own Written once, run anywhere Two-stage debugging Java is an Object Oriented Programming.
A First Program Using C#
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
POS 406 Java Technology And Beginning Java Code
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Li Tak Sing COMPS311F Introduction. Introduction Name: Li Tak Sing ( 李德成 ) Tel: Room: A0916 Course Web.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CIT 590 Intro to Programming First lecture on Java.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4 Software. Chapter 4: Software Generations of Languages Each computer is wired to perform certain operations in response to an instruction. An.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Introduction to Java John Lewis. Course Overview Introduction Object Orientated Programming Java Structure and Syntax Using the Java Platform Advanced.
Classes, Interfaces and Packages
Li Tak Sing COMPS311F Introduction 1. Introduction Name: Li Tak Sing ( 李德成 ) Tel: Room: A0916 Course.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
The need for Programming Languages
JAVA MULTIPLE CHOICE QUESTION.
Object-Oriented Programming: Classes and Objects
Java Course Review.
Subprograms and Programmer Defined Data Type
CS2011 Introduction to Programming I Objects and Classes
Objects First with Java A Practical Introduction using BlueJ
Session 2: Introduction to Object Oriented Programming
Objects First with Java A Practical Introduction using BlueJ
Handout-2(a) Basic Object Oriented Concepts
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )

Contact information Office: Rm A0936 Tel: Course Web Site: Assignment submission:

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, 6 th 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

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 non- static 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.