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 (20)

Components of a computer
Components of a computerComponents of a computer
Components of a computer
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception handling in .net
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
operating system
operating systemoperating system
operating system
 
Web development with Python
Web development with PythonWeb development with Python
Web development with Python
 
Applet
 Applet Applet
Applet
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Von Neumann Architecture
Von Neumann ArchitectureVon Neumann Architecture
Von Neumann Architecture
 
Programming
ProgrammingProgramming
Programming
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
Introduction to problem solving in C
Introduction to problem solving in CIntroduction to problem solving in C
Introduction to problem solving in C
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Java input
Java inputJava input
Java input
 
Python file handling
Python file handlingPython file handling
Python file handling
 

En vedette

Chapter2 java oop
Chapter2 java oopChapter2 java oop
Chapter2 java oopsshhzap
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIEduardo Bergavera
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
Assignment 7
Assignment 7Assignment 7
Assignment 7IIUM
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Chapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentChapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentEduardo Bergavera
 
X physics full notes chapter 5
X physics full notes chapter 5X physics full notes chapter 5
X physics full notes chapter 5neeraj_enrique
 
Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)Michael Redlich
 
Physics Numerical Problem, Metric Physics, Kinematic Problems Karachi, Federa...
Physics Numerical Problem, Metric Physics, Kinematic Problems Karachi, Federa...Physics Numerical Problem, Metric Physics, Kinematic Problems Karachi, Federa...
Physics Numerical Problem, Metric Physics, Kinematic Problems Karachi, Federa...Al-Saudia Virtual Academy
 
Chapter 6 Work Energy Power
Chapter 6 Work Energy PowerChapter 6 Work Energy Power
Chapter 6 Work Energy Powermoths
 
Variables y tipos de datos - fundamentos de la programación
Variables y tipos de datos -  fundamentos de la programaciónVariables y tipos de datos -  fundamentos de la programación
Variables y tipos de datos - fundamentos de la programaciónDesarrolloWeb.com
 

En vedette (14)

What is Python?
What is Python?What is Python?
What is Python?
 
Chapter2 java oop
Chapter2 java oopChapter2 java oop
Chapter2 java oop
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Datos Numéricos parte 1
Datos Numéricos parte 1Datos Numéricos parte 1
Datos Numéricos parte 1
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Chapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentChapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software Development
 
X physics full notes chapter 5
X physics full notes chapter 5X physics full notes chapter 5
X physics full notes chapter 5
 
Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)
 
Physics numericals
Physics numericalsPhysics numericals
Physics numericals
 
Physics Numerical Problem, Metric Physics, Kinematic Problems Karachi, Federa...
Physics Numerical Problem, Metric Physics, Kinematic Problems Karachi, Federa...Physics Numerical Problem, Metric Physics, Kinematic Problems Karachi, Federa...
Physics Numerical Problem, Metric Physics, Kinematic Problems Karachi, Federa...
 
Chapter 6 Work Energy Power
Chapter 6 Work Energy PowerChapter 6 Work Energy Power
Chapter 6 Work Energy Power
 
Variables y tipos de datos - fundamentos de la programación
Variables y tipos de datos -  fundamentos de la programaciónVariables y tipos de datos -  fundamentos de la programación
Variables y tipos de datos - fundamentos de la programación
 

Similaire à Chapter 2 - Getting Started with Java

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

Similaire à Chapter 2 - Getting Started with Java (20)

JAVA CONCEPTS
JAVA CONCEPTS JAVA CONCEPTS
JAVA CONCEPTS
 
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
 
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
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
 
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
 
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
 

Plus de Eduardo Bergavera

CLP Session 5 - The Christian Family
CLP Session 5 - The Christian FamilyCLP Session 5 - The Christian Family
CLP Session 5 - The Christian FamilyEduardo Bergavera
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismEduardo Bergavera
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and StringsEduardo Bergavera
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summaryEduardo Bergavera
 

Plus de Eduardo Bergavera (6)

CLP Session 5 - The Christian Family
CLP Session 5 - The Christian FamilyCLP Session 5 - The Christian Family
CLP Session 5 - The Christian Family
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 

Dernier

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Dernier (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Chapter 2 - Getting Started with Java

  • 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.