SlideShare una empresa de Scribd logo
1 de 51
Chapter 2 Getting Started with Java
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The First Java Program ,[object Object],[object Object],[object Object],[object Object]
Program Ch2Sample1 import  javax.swing.*; class  Ch2Sample1 { public static void  main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Declare a name Create an object Use an object
Program Diagram for Ch2Sample1 setSize(300, 200) setTitle(“My First Java Program”) myWindow : JFrame Ch2Sample1 setVisible(true)
Dependency Relationship Instead of drawing all messages, we summarize it by showing only the dependency relationship. The diagram shows that  Ch2Sample1  “depends” on the service provided by  myWindow . myWindow : JFrame Ch2Sample1
Object Declaration JFrame    myWindow; Account customer; Student jan, jim, jon; Vehicle car1, car2; More Examples Object Name One object is declared here. Class Name This class must be defined before this declaration can be stated.
Object Creation myWindow  =  new  JFrame  (  ) ; customer  = new Customer( ); jon = new Student(“John Java”); car1 = new Vehicle( ); More Examples Object Name Name of the object we are creating here. Class Name An instance of this class is created. Argument No arguments are used here.
Declaration vs. Creation Customer  customer; customer  =  new  Customer( ); 1. The identifier  customer  is declared and space is allocated in memory. 2. A  Customer  object is created and the identifier  customer  is set to refer to it. 1 2 customer 2 : Customer customer 1
State-of-Memory vs. Program customer : Customer State-of-Memory Notation customer : Customer Program Diagram Notation
Name vs. Objects Customer  customer; customer  =  new  Customer( ); customer  =  new  Customer( ); Created with the first  new . Created with the second  new . Reference to the first Customer object is lost. customer : Customer : Customer
Sending a Message myWindow  .   setVisible  (  true  )  ; account.deposit( 200.0 ); student.setName(“john”); car1.startEngine(  ); More Examples Object Name Name of the object to which we are sending a message. Method Name The name of the message we are sending. Argument The argument we are passing with the message.
Program Components ,[object Object],[object Object],[object Object],[object Object]
Program Component: Comment /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import  javax.swing.*; class  Ch2Sample1 { public static void  main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Comment
Matching Comment Markers /* This is a comment on one line */ /* Comment number 1 */ /* Comment number 2 */ /* /* /* This is a comment */ */ Error: No matching beginning marker. These are part of the comment.
Three Types of Comments ,[object Object],[object Object],[object Object],[object Object],[object Object],Multiline Comment Single line Comments ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],javadoc Comments
Import Statement /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import  javax.swing.*; class  Ch2Sample1 { public static void  main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Import Statement
Import Statement Syntax and Semantics <package name>  .   <class name>  ; e.g.  dorm  .   Resident; import   javax.swing.JFrame; import   java.util.*; import   com.drcaffeine.simplegui.*; More Examples Class Name The name of the class we want to import. Use asterisks to import all classes. Package Name Name of the package that contains the classes we want to use.
Class Declaration /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import  javax.swing.*; class  Ch2Sample1 { public static void  main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Class Declaration
Method Declaration /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import  javax.swing.*; class  Ch2Sample1 { public static void  main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Method Declaration
Method Declaration Elements public  static  void   main(  String[ ] args  ){ JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } Method Body Modifier Modifier Return Type Method Name Parameter
Template for Simple Java Programs /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import javax.swing.*; class   Ch2Sample1   { public static void  main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } } Import Statements Class Name Comment Method Body
Why Use Standard Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JOptionPane ,[object Object],JOptionPane.showMessageDialog ( null ,  “I Love Java” ) ; This dialog will appear at the center of the screen.
Displaying Multiple Lines of Text ,[object Object],JOptionPane.showMessageDialog ( null ,  “ onetwothree” ) ;
String ,[object Object],[object Object],[object Object],[object Object]
String is an Object 1. The identifier  name  is declared and space is allocated in memory. 2. A  String  object is created and the identifier  name  is set to refer to it. 1 2 1 2 name String  name; name  =  new  String(“Jon Java”); : String Jon Java name
String Indexing The position, or index, of the first character is 0.
Definition: substring ,[object Object],[object Object],[object Object],[object Object]
Examples: substring text.substring(6,8) text.substring(0,8) text.substring(1,5) text.substring(3,3) text.substring(4,2) String text =  “Espresso” ; “ so” “ Espresso” “ spre” error   “”
Definition: length ,[object Object],[object Object],[object Object],[object Object]
Examples: length String str1, str2, str3, str4; str1 =  “Hello”  ; str2 =  “Java”  ; str3 =  “”  ;  //empty string str4 =  “ “  ;  //one space str1.length( ) str2.length( ) str3.length( ) str4.length( ) 5   4   1   0
Definition: indexOf ,[object Object],[object Object],[object Object],[object Object],[object Object]
Examples: indexOf String str; str =  “I Love Java and Java loves me.”  ; str.indexOf(  “J”  ) str2.indexOf(  “love”  ) str3. indexOf(  “ove”  ) str4. indexOf(  “Me”  ) 7   21   -1   3   3 7 21
Definition: concatenation ,[object Object],[object Object],[object Object],[object Object],[object Object]
Examples: concatenation String str1, str2; str1 =  “Jon”  ; str2 =  “Java”  ; str1 + str2 str1 + “ “ + str2 str2 + “, “ + str1 “ Are you “ + str1 + “?” “ JonJava” “ Jon Java” “ Java, Jon” “ Are you Jon?”
Date ,[object Object],[object Object],[object Object],Date today; today = new Date( ); today.toString( ); “ Fri Oct 31 10:05:18 PST 2003”
SimpleDateFormat ,[object Object],[object Object],Date today =  new  Date( ); SimpleDateFormat sdf1, sdf2; sdf1 =  new  SimpleDateFormat(  “MM/dd/yy”  ); sdf2 =  new  SimpleDateFormat(  “MMMM dd, yyyy”  ); sdf1.format(today); sdf2.format(today); “ 10/31/03” “ October 31, 2003”
JOptionPane for Input ,[object Object],String name; name = JOptionPane.showInputDialog ( null ,  “What is your name?” ) ; This dialog will appear at the center of the screen ready to accept an input.
Problem Statement ,[object Object],[object Object],Example: input:  Andrew Lloyd Weber output:  ALW
Overall Plan ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Development Steps ,[object Object],[object Object],[object Object]
Step 1 Design ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Step 1 Code /* Chapter 2 Sample Program: Displays the Monogram File: Step1/Ch2Monogram.java */ import  javax.swing.*; class  Ch2Monogram  { public static void  main  ( String [ ]  args ) { String name; name = JOptionPane.showInputDialog( null ,    &quot;Enter your full name (first, middle, last):“ ) ; JOptionPane.showMessageDialog ( null , name ) ; } }
Step 1 Test ,[object Object],[object Object],[object Object]
Step 2 Design ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Step 2 Design (cont’d) ,[object Object],[object Object],[object Object],[object Object],“ Aaron Ben Cosner” “ Aaron” “ Ben Cosner” “ Ben” “ Cosner” “ ABC”
Step 2 Code /* Chapter 2 Sample Program: Displays the Monogram File: Step 2/Ch2MonogramStep2.java */ import  javax.swing.*; class  Ch2Monogram  { public static void main   ( String [ ]  args ) { String name, first, middle, last,  space, monogram; space =  &quot; “ ; //Input the full name name = JOptionPane.showInputDialog ( null ,    &quot;Enter your full name (first, middle, last):“   ) ;
Step 2 Code (cont’d) //Extract first, middle, and last names first = name.substring ( 0, name.indexOf ( space )) ; name = name.substring ( name.indexOf ( space ) +1,  name.length ()) ; middle = name.substring ( 0, name.indexOf ( space )) ; last = name.substring ( name.indexOf ( space ) +1,  name.length ()) ; //Compute the monogram monogram = first.substring ( 0, 1 )  +  middle.substring ( 0, 1) +  last.substring ( 0,1 ) ; //Output the result JOptionPane.showMessageDialog ( null ,  &quot;Your  monogram is &quot;  + monogram ) ; } }
Step 2 Test ,[object Object],[object Object]
Program Review ,[object Object],[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptmanish kumar
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritancemanish kumar
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP Zaenal Arifin
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 

La actualidad más candente (20)

Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Python3
Python3Python3
Python3
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
11slide
11slide11slide
11slide
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Advanced core java
Advanced core javaAdvanced core java
Advanced core java
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 

Destacado

Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4Vince Vo
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12Vince Vo
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7Vince Vo
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 

Destacado (8)

Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
Rama Ch11
Rama Ch11Rama Ch11
Rama Ch11
 
Rama Ch12
Rama Ch12Rama Ch12
Rama Ch12
 
Rama Ch14
Rama Ch14Rama Ch14
Rama Ch14
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Rama Ch7
Rama Ch7Rama Ch7
Rama Ch7
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 

Similar a Java căn bản - Chapter2

packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxingGeetha Manohar
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAVINASH KUMAR
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxPoonam60376
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxchetanpatilcp783
 
Java programming basics
Java programming basicsJava programming basics
Java programming basicsHamid Ghorbani
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocksİbrahim Kürce
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)Akshay soni
 

Similar a Java căn bản - Chapter2 (20)

packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Oop java
Oop javaOop java
Oop java
 
Oop
OopOop
Oop
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
constructer.pptx
constructer.pptxconstructer.pptx
constructer.pptx
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
Java String
Java String Java String
Java String
 
Core_java_ppt.ppt
Core_java_ppt.pptCore_java_ppt.ppt
Core_java_ppt.ppt
 

Más de Vince Vo

Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13Vince Vo
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10Vince Vo
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9Vince Vo
 
Java căn bản - Chapter8
Java căn bản - Chapter8Java căn bản - Chapter8
Java căn bản - Chapter8Vince Vo
 
Java căn bản - Chapter5
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5Vince Vo
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3Vince Vo
 
Java căn bản- Chapter1
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1Vince Vo
 
Hướng dẫn cài đặt Java
Hướng dẫn cài đặt JavaHướng dẫn cài đặt Java
Hướng dẫn cài đặt JavaVince Vo
 

Más de Vince Vo (19)

Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
 
Java căn bản - Chapter8
Java căn bản - Chapter8Java căn bản - Chapter8
Java căn bản - Chapter8
 
Java căn bản - Chapter5
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
Java căn bản- Chapter1
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1
 
Hướng dẫn cài đặt Java
Hướng dẫn cài đặt JavaHướng dẫn cài đặt Java
Hướng dẫn cài đặt Java
 
Rama Ch13
Rama Ch13Rama Ch13
Rama Ch13
 
Rama Ch12
Rama Ch12Rama Ch12
Rama Ch12
 
Rama Ch10
Rama Ch10Rama Ch10
Rama Ch10
 
Rama Ch8
Rama Ch8Rama Ch8
Rama Ch8
 
Rama Ch9
Rama Ch9Rama Ch9
Rama Ch9
 
Rama Ch6
Rama Ch6Rama Ch6
Rama Ch6
 
Rama Ch5
Rama Ch5Rama Ch5
Rama Ch5
 
Rama Ch4
Rama Ch4Rama Ch4
Rama Ch4
 
Rama Ch3
Rama Ch3Rama Ch3
Rama Ch3
 
Rama Ch2
Rama Ch2Rama Ch2
Rama Ch2
 
Rama Ch1
Rama Ch1Rama Ch1
Rama Ch1
 

Último

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Último (20)

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

Java căn bản - Chapter2

  • 1. Chapter 2 Getting Started with Java
  • 2.
  • 3.
  • 4. Program Ch2Sample1 import javax.swing.*; class Ch2Sample1 { public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Declare a name Create an object Use an object
  • 5. Program Diagram for Ch2Sample1 setSize(300, 200) setTitle(“My First Java Program”) myWindow : JFrame Ch2Sample1 setVisible(true)
  • 6. Dependency Relationship Instead of drawing all messages, we summarize it by showing only the dependency relationship. The diagram shows that Ch2Sample1 “depends” on the service provided by myWindow . myWindow : JFrame Ch2Sample1
  • 7. Object Declaration JFrame myWindow; Account customer; Student jan, jim, jon; Vehicle car1, car2; More Examples Object Name One object is declared here. Class Name This class must be defined before this declaration can be stated.
  • 8. Object Creation myWindow = new JFrame ( ) ; customer = new Customer( ); jon = new Student(“John Java”); car1 = new Vehicle( ); More Examples Object Name Name of the object we are creating here. Class Name An instance of this class is created. Argument No arguments are used here.
  • 9. Declaration vs. Creation Customer customer; customer = new Customer( ); 1. The identifier customer is declared and space is allocated in memory. 2. A Customer object is created and the identifier customer is set to refer to it. 1 2 customer 2 : Customer customer 1
  • 10. State-of-Memory vs. Program customer : Customer State-of-Memory Notation customer : Customer Program Diagram Notation
  • 11. Name vs. Objects Customer customer; customer = new Customer( ); customer = new Customer( ); Created with the first new . Created with the second new . Reference to the first Customer object is lost. customer : Customer : Customer
  • 12. Sending a Message myWindow . setVisible ( true ) ; account.deposit( 200.0 ); student.setName(“john”); car1.startEngine( ); More Examples Object Name Name of the object to which we are sending a message. Method Name The name of the message we are sending. Argument The argument we are passing with the message.
  • 13.
  • 14. Program Component: Comment /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import javax.swing.*; class Ch2Sample1 { public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Comment
  • 15. Matching Comment Markers /* This is a comment on one line */ /* Comment number 1 */ /* Comment number 2 */ /* /* /* This is a comment */ */ Error: No matching beginning marker. These are part of the comment.
  • 16.
  • 17. Import Statement /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import javax.swing.*; class Ch2Sample1 { public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Import Statement
  • 18. Import Statement Syntax and Semantics <package name> . <class name> ; e.g. dorm . Resident; import javax.swing.JFrame; import java.util.*; import com.drcaffeine.simplegui.*; More Examples Class Name The name of the class we want to import. Use asterisks to import all classes. Package Name Name of the package that contains the classes we want to use.
  • 19. Class Declaration /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import javax.swing.*; class Ch2Sample1 { public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Class Declaration
  • 20. Method Declaration /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import javax.swing.*; class Ch2Sample1 { public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } } Method Declaration
  • 21. Method Declaration Elements public static void main( String[ ] args ){ JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } Method Body Modifier Modifier Return Type Method Name Parameter
  • 22. Template for Simple Java Programs /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import javax.swing.*; class Ch2Sample1 { public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } } Import Statements Class Name Comment Method Body
  • 23.
  • 24.
  • 25.
  • 26.
  • 27. String is an Object 1. The identifier name is declared and space is allocated in memory. 2. A String object is created and the identifier name is set to refer to it. 1 2 1 2 name String name; name = new String(“Jon Java”); : String Jon Java name
  • 28. String Indexing The position, or index, of the first character is 0.
  • 29.
  • 30. Examples: substring text.substring(6,8) text.substring(0,8) text.substring(1,5) text.substring(3,3) text.substring(4,2) String text = “Espresso” ; “ so” “ Espresso” “ spre” error “”
  • 31.
  • 32. Examples: length String str1, str2, str3, str4; str1 = “Hello” ; str2 = “Java” ; str3 = “” ; //empty string str4 = “ “ ; //one space str1.length( ) str2.length( ) str3.length( ) str4.length( ) 5 4 1 0
  • 33.
  • 34. Examples: indexOf String str; str = “I Love Java and Java loves me.” ; str.indexOf( “J” ) str2.indexOf( “love” ) str3. indexOf( “ove” ) str4. indexOf( “Me” ) 7 21 -1 3 3 7 21
  • 35.
  • 36. Examples: concatenation String str1, str2; str1 = “Jon” ; str2 = “Java” ; str1 + str2 str1 + “ “ + str2 str2 + “, “ + str1 “ Are you “ + str1 + “?” “ JonJava” “ Jon Java” “ Java, Jon” “ Are you Jon?”
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44. Step 1 Code /* Chapter 2 Sample Program: Displays the Monogram File: Step1/Ch2Monogram.java */ import javax.swing.*; class Ch2Monogram { public static void main ( String [ ] args ) { String name; name = JOptionPane.showInputDialog( null , &quot;Enter your full name (first, middle, last):“ ) ; JOptionPane.showMessageDialog ( null , name ) ; } }
  • 45.
  • 46.
  • 47.
  • 48. Step 2 Code /* Chapter 2 Sample Program: Displays the Monogram File: Step 2/Ch2MonogramStep2.java */ import javax.swing.*; class Ch2Monogram { public static void main ( String [ ] args ) { String name, first, middle, last, space, monogram; space = &quot; “ ; //Input the full name name = JOptionPane.showInputDialog ( null , &quot;Enter your full name (first, middle, last):“ ) ;
  • 49. Step 2 Code (cont’d) //Extract first, middle, and last names first = name.substring ( 0, name.indexOf ( space )) ; name = name.substring ( name.indexOf ( space ) +1, name.length ()) ; middle = name.substring ( 0, name.indexOf ( space )) ; last = name.substring ( name.indexOf ( space ) +1, name.length ()) ; //Compute the monogram monogram = first.substring ( 0, 1 ) + middle.substring ( 0, 1) + last.substring ( 0,1 ) ; //Output the result JOptionPane.showMessageDialog ( null , &quot;Your monogram is &quot; + monogram ) ; } }
  • 50.
  • 51.