SlideShare une entreprise Scribd logo
1  sur  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]

Contenu connexe

Tendances

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
 

Tendances (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
 

En vedette

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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Plus 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
 

Plus 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
 

Dernier

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 

Dernier (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

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.