SlideShare a Scribd company logo
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]

More Related Content

What's hot

Files in c++
Files in c++Files in c++
Files in c++
Selvin Josy Bai Somu
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Problem Solving Aspect of Swapping Two Integers using a Temporary Variable
Problem Solving Aspect of Swapping Two Integers using a Temporary VariableProblem Solving Aspect of Swapping Two Integers using a Temporary Variable
Problem Solving Aspect of Swapping Two Integers using a Temporary Variable
Saravana Priya
 
Python file handling
Python file handlingPython file handling
Python file handling
Prof. Dr. K. Adisesha
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
Dharani Kumar Madduri
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
Kandarp Tiwari
 
Operator Overloading In Python
Operator Overloading In PythonOperator Overloading In Python
Operator Overloading In Python
Simplilearn
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
tigerwarn
 
Variables in python
Variables in pythonVariables in python
Variables in python
Jaya Kumari
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
 
C++ Overview
C++ OverviewC++ Overview
C++ Overviewkelleyc3
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
Bharat Kalia
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
imran khan
 
Files in java
Files in javaFiles in java

What's hot (20)

Files in c++
Files in c++Files in c++
Files in c++
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Problem Solving Aspect of Swapping Two Integers using a Temporary Variable
Problem Solving Aspect of Swapping Two Integers using a Temporary VariableProblem Solving Aspect of Swapping Two Integers using a Temporary Variable
Problem Solving Aspect of Swapping Two Integers using a Temporary Variable
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
 
Operator Overloading In Python
Operator Overloading In PythonOperator Overloading In Python
Operator Overloading In Python
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
 
Java swing
Java swingJava swing
Java swing
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
File operations in c
File operations in cFile operations in c
File operations in c
 
Files in java
Files in javaFiles in java
Files in java
 

Viewers also liked

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 II
Eduardo 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 I
Eduardo Bergavera
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
IIUM
 
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 Development
Eduardo 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ón
DesarrolloWeb.com
 

Viewers also liked (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
 

Similar to Chapter 2 - Getting Started with Java

JAVA CONCEPTS
JAVA CONCEPTS JAVA CONCEPTS
JAVA CONCEPTS
Shivam Singh
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
ssuser8347a1
 
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
CPD INDIA
 
Oop java
Oop javaOop java
Oop java
Minal Maniar
 
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
Danairat Thanabodithammachari
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
Geetha 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 sir
AVINASH KUMAR
 
constructer.pptx
constructer.pptxconstructer.pptx
constructer.pptx
Nagaraju Pamarthi
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya Johnny
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
Jyothsna Sree
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
AnkitaVerma776806
 
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
Poonam60376
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
BG 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++ Exams
MuhammadTalha436
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
Hamid 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
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 

Similar to 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
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv 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
 

More from Eduardo Bergavera

CLP Session 5 - The Christian Family
CLP Session 5 - The Christian FamilyCLP Session 5 - The Christian Family
CLP Session 5 - The Christian Family
Eduardo Bergavera
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Eduardo Bergavera
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo 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 summary
Eduardo Bergavera
 

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

Recently uploaded

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 

Recently uploaded (20)

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 

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.