Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Introduction The Basics Programming in Java Further Reading Introduction to Java Panagiotis Louridas [email protected] DIEPTELO Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Outline 1 Introduction A Bit of History The Java Runtime Environment Java Programs Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Outline 1 2 Introduction A Bit of History The Java Runtime Environment Java Programs The Basics Java Compared to Other Languages Java Principles Object Orientation in a Nutshell Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Outline 1 2 3 Introduction A Bit of History The Java Runtime Environment Java Programs The Basics Java Compared to Other Languages Java Principles Object Orientation in a Nutshell Programming in Java Hello World Graphical Hello World Graphical Hello World with Interaction Collections Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Outline 1 2 3 4 Introduction A Bit of History The Java Runtime Environment Java Programs The Basics Java Compared to Other Languages Java Principles Object Orientation in a Nutshell Programming in Java Hello World Graphical Hello World Graphical Hello World with Interaction Collections Further Reading Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading A Bit of History The Java Runtime Environment Java Programs A Bit of History Bill Joy, 1990: Sun Aspen Smallworks James Gosling, 1992: FirstPerson. Work on “C++ minus minus” Demise of Apple Newton: efforts shift to interactive TV (ITV) Oak: a language for ITV 1993: Oak becomes Java Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading A Bit of History The Java Runtime Environment Java Programs The Java Runtime Environment Learning Java, 3rd Edition, O’Reilly, 2005 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading A Bit of History The Java Runtime Environment Java Programs Types of Java Programs Applets j2me Desktop applications Server applications Network applications ... Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Java Compared to Other Languages Java Principles Object Orientation in a Nutshell Java Compared to Other Languages Learning Java, 3rd Edition, O’Reilly, 2005 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Java Compared to Other Languages Java Principles Object Orientation in a Nutshell Java Principles Simplify Object Orientation Type safety Runtime information Error handling Threads Collections Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Java Compared to Other Languages Java Principles Object Orientation in a Nutshell What is an Object An object is an entity containing data and code, or state and behaviour The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Java Compared to Other Languages Java Principles Object Orientation in a Nutshell A class example class Bicycle { int cadence = 0; int speed = 0; int gear = 1; v o i d c h a n g e C a d e n c e ( i n t newValue ) { c a d e n c e = newValue ; } v o i d c h a n g e G e a r ( i n t newValue ) { g e a r = newValue ; } v o i d speedUp ( i n t i n c r e m e n t ) { speed = speed + increment ; } void applyBrakes ( i n t decrement ) { speed = speed − decrement ; } void p r i n t S t a t e s ( ) { System . o u t . p r i n t l n ( ” c a d e n c e : ”+c a d e n c e+” s p e e d : ”+s p e e d+” g e a r : ”+g e a r ) ; } } The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Java Compared to Other Languages Java Principles Object Orientation in a Nutshell A Bicycle Demo c l a s s Bi cy c leDe mo { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { // C r e a t e two d i f f e r e n t B i c y c l e o b j e c t s B i c y c l e b i k e 1 = new B i c y c l e ( ) ; B i c y c l e b i k e 2 = new B i c y c l e ( ) ; // I n v o k e methods on t h o s e o b j e c t s b i k e 1 . c h a n g e C ad e n c e ( 5 0 ) ; b i k e 1 . speedUp ( 1 0 ) ; bike1 . changeGear ( 2 ) ; bike1 . printStates ( ) ; b i k e 2 . c h a n g e C ad e n c e ( 5 0 ) ; b i k e 2 . speedUp ( 1 0 ) ; bike2 . changeGear ( 2 ) ; b i k e 2 . c h a n g e C ad e n c e ( 4 0 ) ; b i k e 2 . speedUp ( 1 0 ) ; bike2 . changeGear ( 3 ) ; bike2 . printStates ( ) ; } } The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Java Compared to Other Languages Java Principles Object Orientation in a Nutshell Inheritance The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 c l a s s M o u n t a i n B i k e extends B i c y c l e { // new f i e l d s and methods d e f i n i n g a mountain b i k e // would go h e r e } Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Java Compared to Other Languages Java Principles Object Orientation in a Nutshell Interfaces Interfaces are like classes, but without implementation interface Driveable { boolean s t a r t E n g i n e ( ) ; void stopEngine ( ) ; f l o a t a c c e l e r a t e ( f l o a t acc ) ; boolean t u r n ( D i r e c t i o n d i r ) ; } c l a s s A u t o m o b i l e implements D r i v e a b l e { ... p u b l i c boolean s t a r t E n g i n e ( ) { i f ( notTooCold ) engineRunning = true ; ... } public void stopEngine ( ) { engineRunning = f a l s e ; } public f l o a t a c c e l e r a t e ( f l o a t acc ) { ... } p u b l i c boolean t u r n ( D i r e c t i o n d i r ) { ... } ... } Learning Java, 3rd Edition, O’Reilly, 2005 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Hello World Graphical Hello World Graphical Hello World with Interaction Collections Hello World public class HelloJava { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { System . o u t . p r i n t l n ( ” H e l l o , J a v a ! ” ) ; } } Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Hello World Graphical Hello World Graphical Hello World with Interaction Collections Graphical Hello World import j a v a x . s w i n g . ∗ ; public class HelloJavaSwing { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { JFrame f r a m e = new JFrame ( ” H e l l o , J a v a ! ” ) ; J L a b e l l a b e l = new J L a b e l ( ” H e l l o , J a v a ! ” , J L a b e l . CENTER ) ; f r a m e . add ( l a b e l ) ; frame . s e t S i z e (300 , 3 0 0 ) ; frame . s e t V i s i b l e ( true ) ; } } Learning Java, 3rd Edition, O’Reilly, 2005 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Hello World Graphical Hello World Graphical Hello World with Interaction Collections Graphical Hello World with Interaction The main class import j a v a . awt . ∗ ; import j a v a . awt . e v e n t . ∗ ; import j a v a x . s w i n g . ∗ ; public class HelloJava2 { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { JFrame f r a m e = new JFrame ( ” H e l l o J a v a 2 ” ) ; frame . getContentPane ( ) . add ( new HelloComponent ( ” H e l l o , J a v a ! ” ) ) ; f r a m e . s e t D e f a u l t C l o s e O p e r a t i o n ( JFrame . EXIT ON CLOSE ) ; frame . s e t S i z e (300 , 3 0 0 ) ; frame . s e t V i s i b l e ( true ) ; } } Learning Java, 3rd Edition, O’Reilly, 2005 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Hello World Graphical Hello World Graphical Hello World with Interaction Collections Graphical Hello World with Interaction The HelloComponent class c l a s s HelloComponent extends JComponent implements M o u s e M o t i o n L i s t e n e r { S t r i n g theMessage ; i n t messageX = 1 2 5 , messageY = 9 5 ; // C o o r d i n a t e s o f t h e message p u b l i c HelloComponent ( S t r i n g message ) { t h e M e s s a g e = message ; addMouseMotionListener ( this ) ; } p u b l i c v o i d p ain t Co mp on en t ( G r a p h i c s g ) { g . d r a w S t r i n g ( theMessage , messageX , messageY ) ; } p u b l i c v o i d mouseDragged ( MouseEvent e ) { // Save t h e mouse c o o r d i n a t e s and p a i n t t h e message . messageX = e . getX ( ) ; messageY = e . getY ( ) ; repaint (); } p u b l i c v o i d mouseMoved ( MouseEvent e ) { } } Learning Java, 3rd Edition, O’Reilly, 2005 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Hello World Graphical Hello World Graphical Hello World with Interaction Collections Sort Arguments import j a v a . u t i l . ∗ ; p u b l i c c l a s s FindDups { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { Set<S t r i n g > s = new HashSet<S t r i n g > ( ) ; for ( String a : args ) i f ( ! s . add ( a ) ) System . o u t . p r i n t l n ( ” D u p l i c a t e d e t e c t e d : ” + a ) ; System . o u t . p r i n t l n ( s . s i z e ( ) + ” d i s t i n c t words : ” + s ) ; } } The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Hello World Graphical Hello World Graphical Hello World with Interaction Collections Shuffle Arguments import j a v a . u t i l . ∗ ; public class Shuffle { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { L i s t <S t r i n g > l i s t = A r r a y s . a s L i s t ( a r g s ) ; Collections . shuffle ( l i s t ); System . o u t . p r i n t l n ( l i s t ) ; } } The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java Introduction The Basics Programming in Java Further Reading Further Reading S. Zakhour, S. Hommel, J. Royal, I. Rabinovitch, T. Risser, M. Hoeber, The Java Tutorial: A Short Course on the Basics, 4th edition, Prentice Hall PTR, 2006. Online version at: http: //java.sun.com/docs/books/tutorial/index.html J. Knudsen, P. Niemeyer, Learning Java, 3rd edition, O’ Reilly, 2005. B. Eckel, Thinking in Java, 4th edition, Prentice Hall PTR, 2006. Louridas Introduction to Java