ee2e1. java programming introduction dr. mike spann [email protected]
TRANSCRIPT
Contents
Java is just another programming language Java is just another programming language (eg. C & C++). So why bother to learn (eg. C & C++). So why bother to learn Java?Java?
Java is ……
PortablePortable Object orientedObject oriented RobustRobust Multi-threadedMulti-threaded GraphicalGraphical ConnectedConnected ExtensiveExtensive
Java applications and applets
ApplicationsApplications – standalone Java programs – standalone Java programs
Applets Applets – Java programs that run inside – Java programs that run inside web browsersweb browsers
‘Hello world’ application
/** * The HelloWorldApp class implements /** * The HelloWorldApp class implements an application that simply displays "Hello an application that simply displays "Hello World!" to the standard output. */ World!" to the standard output. */
public public class HelloWorldApp class HelloWorldApp { { public static void main(String[] args) public static void main(String[] args) { { System.out.println("Hello World!");System.out.println("Hello World!"); } } }}
‘Hello world’ appletclass HelloWorldPanel extends JPanel{ public void paintComponent(Graphics g) { super.paintComponent(g); Font f=new Font(“SansSerif”,Font.BOLD,36); g.setFont(f); g.drawString("Hello World!", 50, 120); }}public class HelloWorldApplet extends JApplet{ public void init() { Container contentPane = getContentPane(); contentPane.add(new HelloWorldPanel()); }}
‘Hello World’ html file
<APPLET CODE = “HelloWorldApplet.class" WIDTH = 300 HEIGHT = 300 >
</APPLET>
Run the ‘Hello World’ applet
http://www.eee.bham.ac.uk/spannm/Java%20Stuff/HelloWorldApplet/HelloWorldApplet.html
Portable Java
Java is ‘program once – run anywhere’Java is ‘program once – run anywhere’ A Java program is compiled to A Java program is compiled to bytecodesbytecodes Bytecodes interpreted by the Bytecodes interpreted by the Java Virtual Java Virtual
MachineMachine
Object Oriented Java
Object oriented programming is concerned Object oriented programming is concerned
with objects and their :with objects and their :
StateState BehaviorBehavior
ExampleExample A bank accountA bank account
transfer()
withdraw()
balance
account numberState
Behaviour
class BankAccount{
public void transfer(int amount);
public void withdraw(int amount);
private:int balance;int accountNumber;
}
Robust Java
Java is easier than C or C++!Java is easier than C or C++! Java has better memory management Java has better memory management
than C or C++than C or C++ You don’t need pointers for accessing You don’t need pointers for accessing
arrays or strings in Javaarrays or strings in Java No chance of dangling pointers or No chance of dangling pointers or
memory leaks in Javamemory leaks in Java
Multi-threaded Java
A A threadthread is a sequence of executing is a sequence of executing statements in a programstatements in a program
A multi-threaded program comprises a A multi-threaded program comprises a number of threads which run in parallel number of threads which run in parallel (but only one thread is being executed at (but only one thread is being executed at one time)one time)
Java supports multi-threading as an Java supports multi-threading as an intrinsic part of the language – multi-intrinsic part of the language – multi-threading is easy in Java!threading is easy in Java!
Graphical Java
Abstract widget toolkit (AWT) + User Abstract widget toolkit (AWT) + User interface library interface library SwingSwing
Swing is a collection of user interface Swing is a collection of user interface components (buttons, menus, dialogue components (buttons, menus, dialogue boxes etc)boxes etc)
AWT does the event handlingAWT does the event handling
Example 1. A tic-tac-toe applet
http://www.eee.bham.ac.uk/spannm/Java%20Stuff/TicTacToeApplet/example1.html
Example 2. Full swing set demo
http://www.eee.bham.ac.uk/spannm/Java%20Stuff/SwingSetApplet/SwingSetApplet.html
Connected Java
Networking is easy in Java!Networking is easy in Java! Java has classes and methods for network Java has classes and methods for network
programming.programming. A Java program can easily access a web-A Java program can easily access a web-
site through a URL connectionsite through a URL connection Java programs can be be written which Java programs can be be written which
implement both client and server side implement both client and server side processingprocessing
Example – simple web browser
http://www.eee.bham.ac.uk/spannm/Java%20Stuff/BookMarkApplet/BookMark.html
So what is Java?
Java consists of 2 things :Java consists of 2 things :
The The Java Virtual MachineJava Virtual Machine (Java VM) (Java VM)
The The Java Application Programming Java Application Programming InterfaceInterface (Java API) (Java API)
Java VM runs the Java bytecode on the Java VM runs the Java bytecode on the different hardware platformsdifferent hardware platforms
Java API Java API is a large collection of ready-is a large collection of ready-made software componentsmade software components You can add additional API’s to this You can add additional API’s to this
layerlayer Graphics, numerical, image processing Graphics, numerical, image processing
etcetc The Java API is grouped into libraries of The Java API is grouped into libraries of
related classes and interfaces; these related classes and interfaces; these libraries are known as libraries are known as packagespackages. .
Java APIjava
lang awt io
Math
String
Thread
Graphics
MathButton
Color
File
InputStream
OutputStream
sqrt()
exp()
Extensive Java – Java technology
Java has additional API’s covering a wide Java has additional API’s covering a wide range of software technologiesrange of software technologies Java2D – 2D graphicsJava2D – 2D graphics Java3D – 3D graphicsJava3D – 3D graphics Java Advanced Imaging (JAI) – image Java Advanced Imaging (JAI) – image
processing processing
Java Speech API – speech processingJava Speech API – speech processing Java Media Framework API – multi-Java Media Framework API – multi-
media applicationmedia application JavaBeans – re-useable user interface JavaBeans – re-useable user interface
componentscomponents Java Servlets – extending web server Java Servlets – extending web server
functionalityfunctionality Java Message Service (JMS) – allowing Java Message Service (JMS) – allowing
programs to exchange messagesprograms to exchange messages Java Telephony API – computer telephonyJava Telephony API – computer telephony
Java TV API – interactive digital TVJava TV API – interactive digital TV
JavaMail API – email applicationsJavaMail API – email applications
JDBC – Java database APIJDBC – Java database API
Examples
Java2D APIJava2D API http://www.cs.kent.ac.uk/people/staff/pgk/www.cs.kent.ac.uk/people/staff/pgk/
teaching/java/lab-guides/getting-started/teaching/java/lab-guides/getting-started/demo/jfc/Java2D/Java2D.html demo/jfc/Java2D/Java2D.html
Java 3D APIJava 3D API http://www.mol3d.com/ http://www.mol3d.com/
Java resources Sun’s Java home pageSun’s Java home page
http://java.sun.com/http://java.sun.com/ Java online tutorialJava online tutorial
http://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/ Comparing C++and JavaComparing C++and Java
http://www.compapp.dcu.ie/~renaat/projects/cvjava.htmlhttp://www.compapp.dcu.ie/~renaat/projects/cvjava.html TextbookTextbook
Core Java 2. Volume 1-FundamentalsCore Java 2. Volume 1-Fundamentals C.S.Horstmann, G. CornellC.S.Horstmann, G. Cornell
Amazon LinkAmazon Link My web pageMy web page
http://www.eee.bham.ac.uhttp://www.eee.bham.ac.uk/spannm/Courses/ee2e.htmk/spannm/Courses/ee2e.htm
OK, so why should I learn Java?
Main reason, Java is Main reason, Java is object orientedobject oriented What is OOP good for?
Modelling asynchronously interacting objects• GUIs• Event simulation• Ray tracing visualisation• CAD simulation• Real-time control/embedded systems• Robotics• Image/Video processing• Client/Server systems• etc
OK, so why should I learn Java?
Java is free to download and is easy to learnJava is free to download and is easy to learn Java has powerful (and free!) development tools e.g. Eclipse , Java has powerful (and free!) development tools e.g. Eclipse ,
NetbeansNetbeans Excellent documentation support – JavadocsExcellent documentation support – Javadocs Great community supportGreat community support Rich collection of open source librariesRich collection of open source libraries Can develop Android apps in Java – supported by Eclipse and Can develop Android apps in Java – supported by Eclipse and
NetbeansNetbeans Android is a good platform for mobile apps because of Android is a good platform for mobile apps because of ease of
release, wide range of devices and its an open platform
So what about this course? EE2E1 (MS)EE2E1 (MS)
Introduction to Java programmingIntroduction to Java programmingBasic ideas about objects and classesBasic ideas about objects and classesWe will also look at more advanced features of JavaWe will also look at more advanced features of Java
• GraphicsGraphics• Files and streamsFiles and streams• Multi-threadingMulti-threading• NetworksNetworks
EE2E2 (DP)EE2E2 (DP) Object Oriented Software DesignObject Oriented Software Design
Assessment
EE2E1 and EE2E2 are assessed jointly (making up EE2E1 and EE2E2 are assessed jointly (making up the EE2E module)the EE2E module)
EE2E1 is assessed through a classtest and EE2E1 is assessed through a classtest and programming exercises and a major programming programming exercises and a major programming assignmentassignment 15% through a 1 hour class test15% through a 1 hour class test 2 x 22.5% through 2 programming exercises2 x 22.5% through 2 programming exercises 40% through a major programming assignment 40% through a major programming assignment
carried out in semester 2carried out in semester 2