SlideShare a Scribd company logo
Random Creating simple games with Java Methods and Parameters
Agenda In this lecture, we will see that a  program  is a set of classes, a  class  is a set of methods and a  method  is a collection of  statements . We will discover how a message expression  invokes  a particular  method .
Outline Program Classes Kinds of Java Methods Invoking instance methods Passing values by parameter
The Structure of a Java Program   There are four major structural components of Java programs: the program itself classes methods statements
A Java Program Class1 Class 2 Class N A Java Program static var   instance var static var   instance var static var   instance var Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN; Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN; Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN;
Classes A class is: Conceptually:  a category of objects In a Java program:   A block of code that describes what objects in this category are like ( state ), and how they can behave ( message protocol ) Example: we could create different classes to model different kinds of vehicles: Car class   – 4 wheels, fuel powered, doors, steering wheel etc. Bicycle class   – 2 wheels, manually powered, handle bars etc.
A Java Program - a Set of Classes A Java program consists of one or more  classes A class  is like a blueprint that describes what objects of that class are like We can use these classes to create the objects that our program manipulates
Syntax for a Java Class public class Game { /*   Version 1 This program is a number guessing game where the user tries to guess an integer randomly picked by the computer */ } class end delimiter class name class start delimiter class comment body of the class goes here start comment delimiter end comment delimiter visibility modifier class keyword
A Java Class - a Set of Methods The body of each Java class includes a set of  methods A  method  is some code that performs a  single, well defined  task. One Java Class A Java Method A Java Method A Java Method A Java Method
Two Kinds of Java Methods An  instance method  implements a message that is sent to an instance of the class. A  static method  implements a task that is independent of any particular object.  In either case, some code is run and (optionally) a result is returned We will learn about static methods in a later lecture
Syntax for a Java Method public static void main(String args[]) { /* Starting point for a program. */ } method end delimiter method name method start delimiter method comment visibility modifier static keyword return type parameter list body of the method goes here
A Java Method - Statements The body of a method includes a sequence of  statements These statements specify what happens when the method is executed (or “invoked” or “called”) A Java Method A Java Statement A Java Statement A Java Statement A Java Statement
Java Statements There are many kinds of Java statements We can use many different kinds of statements: variable declarations message expressions assignment statements imports  (we don’t put import statements inside methods) Each statement ends with a semi-colon  ;
Invoking an instance method When we execute a piece of code that sends a message to a receiver, the  class  of the  receiver object  is searched for an  instance method  with the  same signature  as the  message expression Once located, the method starts to  execute  (we say that the method has been “ called ” or “ invoked ”) When the method is invoked, any parameters declared in the method signature get created This is sometimes called “ method dispatch ”
Example 1 Consider the following message expression: " Hello " .charAt(1); Returns the character  ' e '  (at location 1) When we execute this code, the  charAt(int)  method is located in the  String  class, and begins to execute  The code that contains the message expression gets suspended while the   charAt()   method executes Any parameters, local variables get created when the charAt( ) method starts to execute
Example 2 "Hello".toUpperCase(); String Class public String toUpperCase() { /* … class of receiver is String empty argument list empty parameter list “ HELLO” Note: returns a String – see method signature! message name is toUpperCase
Example 3 System.out.print("Hello"); Note: does not return anything – see method signature! PrintStream Class public void print(String aString) {  /* … class of receiver is PrintStream message name is print one argument class String one parameter class String
Parameters Q: Why do we need parameters? A: Because sometimes a method needs some (previously existing) information to be “fed in” from another part of the program so that it can do its job.  Parameters are like interfaces between methods
Declaring Parameters Parameters are declared in the  signature  (1 st  line) of a method   Consider our first example: there is a method in the String class which has this signature public char charAt(int index){ Parameter declaration
Parameters & Local Variables Parameters are very much like local variables in that: Lifetime:  the same as the method in which they are declared Scope:  the same as the method in which they are declared But parameters are declared in the first line of the method, not inside the method. When the method is invoked, they are bound to the arguments.
Initializing Parameters // in the main program int number; number = 1; " Hello " .charAt(number); public char charAt(int index){   // method header 1 index 1 number 1 number
An Example  using Primitive Data Types public class Example { public Example ( ) { } private void aMethod( int param) { param = 1;  // notice param is re-bound System.out.println(param); } public static void main(String args[ ] ) { int  argument;  Example anObj; anObj = new Example( ); argument = 6; anObj.aMethod(argument); System.out.print(argument);  …
Summary Methods Parameters (formal and actual) Local variables Return values vs. void Overloading
“ Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25” --Andrew Rutherford

More Related Content

What's hot (20)

PPT
Basic elements of java
Ahmad Idrees
 
PDF
Chapter 01 Introduction to Java by Tushar B Kute
Tushar B Kute
 
PPT
Core Java
Bharat17485
 
PPT
Unit 5 Java
arnold 7490
 
ODP
OOP java
xball977
 
PDF
Lecture02 java
jawidAhmadRohani
 
PPTX
Pj01 3-java-variable and data types
SasidharaRaoMarrapu
 
PPTX
OOP interview questions & answers.
Questpond
 
PPT
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
PPTX
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
PPTX
Learn To Code: Introduction to java
SadhanaParameswaran
 
PPT
Ppt chapter03
Richard Styner
 
PPT
Class method
Richard Styner
 
PPT
Unit 4 Java
arnold 7490
 
PDF
C++ Object oriented concepts & programming
nirajmandaliya
 
PPT
9781111530532 ppt ch10
Terry Yoast
 
PPTX
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce
 
PPT
Md06 advance class features
Rakesh Madugula
 
PPT
Java adapter
Arati Gadgil
 
PPTX
Interfaces and abstract classes
AKANSH SINGHAL
 
Basic elements of java
Ahmad Idrees
 
Chapter 01 Introduction to Java by Tushar B Kute
Tushar B Kute
 
Core Java
Bharat17485
 
Unit 5 Java
arnold 7490
 
OOP java
xball977
 
Lecture02 java
jawidAhmadRohani
 
Pj01 3-java-variable and data types
SasidharaRaoMarrapu
 
OOP interview questions & answers.
Questpond
 
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
Learn To Code: Introduction to java
SadhanaParameswaran
 
Ppt chapter03
Richard Styner
 
Class method
Richard Styner
 
Unit 4 Java
arnold 7490
 
C++ Object oriented concepts & programming
nirajmandaliya
 
9781111530532 ppt ch10
Terry Yoast
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce
 
Md06 advance class features
Rakesh Madugula
 
Java adapter
Arati Gadgil
 
Interfaces and abstract classes
AKANSH SINGHAL
 

Similar to Java (20)

PPTX
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
PPTX
Java Tokens in java program . pptx
CmDept
 
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
PPTX
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPT
Ap Power Point Chpt4
dplunkett
 
PPTX
Basic concept of class, method , command line-argument
Suresh Mohta
 
PPTX
Java method present by showrov ahamed
Md Showrov Ahmed
 
DOCX
The Java Learning Kit Chapter 5 – Methods and Modular.docx
arnoldmeredith47041
 
PPT
Methods intro-1.0
BG Java EE Course
 
PPT
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
 
PPT
Explain Classes and methods in java (ch04).ppt
ayaankim007
 
PPTX
Module 4_Chapter One - Java Methods.pptx
jhesorleylaid2
 
PPTX
1. Methods presentation which can easily help you understand java methods.pptx
jeyoco4655
 
PPTX
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
PDF
Week 7 Java Programming Methods For I.T students.pdf
JaypeeGPolancos
 
PPTX
Multi Threading- in Java WPS Office.pptx
ManasaMR2
 
PPT
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
PPT
SMI - Introduction to Java
SMIJava
 
PPTX
Oop in kotlin
Abdul Rahman Masri Attal
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Java Tokens in java program . pptx
CmDept
 
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
Class and Object.pptx from nit patna ece department
om2348023vats
 
Ap Power Point Chpt4
dplunkett
 
Basic concept of class, method , command line-argument
Suresh Mohta
 
Java method present by showrov ahamed
Md Showrov Ahmed
 
The Java Learning Kit Chapter 5 – Methods and Modular.docx
arnoldmeredith47041
 
Methods intro-1.0
BG Java EE Course
 
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
 
Explain Classes and methods in java (ch04).ppt
ayaankim007
 
Module 4_Chapter One - Java Methods.pptx
jhesorleylaid2
 
1. Methods presentation which can easily help you understand java methods.pptx
jeyoco4655
 
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
Week 7 Java Programming Methods For I.T students.pdf
JaypeeGPolancos
 
Multi Threading- in Java WPS Office.pptx
ManasaMR2
 
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
SMI - Introduction to Java
SMIJava
 
Ad

More from mbruggen (7)

PPT
M C6java7
mbruggen
 
PPT
M C6java6
mbruggen
 
PPT
M C6java5
mbruggen
 
PPT
M C6java4
mbruggen
 
PPT
M C6java3
mbruggen
 
PPT
Java Introductie
mbruggen
 
PPT
M C6java2
mbruggen
 
M C6java7
mbruggen
 
M C6java6
mbruggen
 
M C6java5
mbruggen
 
M C6java4
mbruggen
 
M C6java3
mbruggen
 
Java Introductie
mbruggen
 
M C6java2
mbruggen
 
Ad

Recently uploaded (20)

PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Biography of Daniel Podor.pdf
Daniel Podor
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 

Java

  • 1. Random Creating simple games with Java Methods and Parameters
  • 2. Agenda In this lecture, we will see that a program is a set of classes, a class is a set of methods and a method is a collection of statements . We will discover how a message expression invokes a particular method .
  • 3. Outline Program Classes Kinds of Java Methods Invoking instance methods Passing values by parameter
  • 4. The Structure of a Java Program There are four major structural components of Java programs: the program itself classes methods statements
  • 5. A Java Program Class1 Class 2 Class N A Java Program static var instance var static var instance var static var instance var Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN; Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN; Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN;
  • 6. Classes A class is: Conceptually: a category of objects In a Java program: A block of code that describes what objects in this category are like ( state ), and how they can behave ( message protocol ) Example: we could create different classes to model different kinds of vehicles: Car class – 4 wheels, fuel powered, doors, steering wheel etc. Bicycle class – 2 wheels, manually powered, handle bars etc.
  • 7. A Java Program - a Set of Classes A Java program consists of one or more classes A class is like a blueprint that describes what objects of that class are like We can use these classes to create the objects that our program manipulates
  • 8. Syntax for a Java Class public class Game { /* Version 1 This program is a number guessing game where the user tries to guess an integer randomly picked by the computer */ } class end delimiter class name class start delimiter class comment body of the class goes here start comment delimiter end comment delimiter visibility modifier class keyword
  • 9. A Java Class - a Set of Methods The body of each Java class includes a set of methods A method is some code that performs a single, well defined task. One Java Class A Java Method A Java Method A Java Method A Java Method
  • 10. Two Kinds of Java Methods An instance method implements a message that is sent to an instance of the class. A static method implements a task that is independent of any particular object. In either case, some code is run and (optionally) a result is returned We will learn about static methods in a later lecture
  • 11. Syntax for a Java Method public static void main(String args[]) { /* Starting point for a program. */ } method end delimiter method name method start delimiter method comment visibility modifier static keyword return type parameter list body of the method goes here
  • 12. A Java Method - Statements The body of a method includes a sequence of statements These statements specify what happens when the method is executed (or “invoked” or “called”) A Java Method A Java Statement A Java Statement A Java Statement A Java Statement
  • 13. Java Statements There are many kinds of Java statements We can use many different kinds of statements: variable declarations message expressions assignment statements imports (we don’t put import statements inside methods) Each statement ends with a semi-colon ;
  • 14. Invoking an instance method When we execute a piece of code that sends a message to a receiver, the class of the receiver object is searched for an instance method with the same signature as the message expression Once located, the method starts to execute (we say that the method has been “ called ” or “ invoked ”) When the method is invoked, any parameters declared in the method signature get created This is sometimes called “ method dispatch ”
  • 15. Example 1 Consider the following message expression: " Hello " .charAt(1); Returns the character ' e ' (at location 1) When we execute this code, the charAt(int) method is located in the String class, and begins to execute The code that contains the message expression gets suspended while the charAt() method executes Any parameters, local variables get created when the charAt( ) method starts to execute
  • 16. Example 2 "Hello".toUpperCase(); String Class public String toUpperCase() { /* … class of receiver is String empty argument list empty parameter list “ HELLO” Note: returns a String – see method signature! message name is toUpperCase
  • 17. Example 3 System.out.print("Hello"); Note: does not return anything – see method signature! PrintStream Class public void print(String aString) { /* … class of receiver is PrintStream message name is print one argument class String one parameter class String
  • 18. Parameters Q: Why do we need parameters? A: Because sometimes a method needs some (previously existing) information to be “fed in” from another part of the program so that it can do its job. Parameters are like interfaces between methods
  • 19. Declaring Parameters Parameters are declared in the signature (1 st line) of a method Consider our first example: there is a method in the String class which has this signature public char charAt(int index){ Parameter declaration
  • 20. Parameters & Local Variables Parameters are very much like local variables in that: Lifetime: the same as the method in which they are declared Scope: the same as the method in which they are declared But parameters are declared in the first line of the method, not inside the method. When the method is invoked, they are bound to the arguments.
  • 21. Initializing Parameters // in the main program int number; number = 1; " Hello " .charAt(number); public char charAt(int index){ // method header 1 index 1 number 1 number
  • 22. An Example using Primitive Data Types public class Example { public Example ( ) { } private void aMethod( int param) { param = 1; // notice param is re-bound System.out.println(param); } public static void main(String args[ ] ) { int argument; Example anObj; anObj = new Example( ); argument = 6; anObj.aMethod(argument); System.out.print(argument); …
  • 23. Summary Methods Parameters (formal and actual) Local variables Return values vs. void Overloading
  • 24. “ Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25” --Andrew Rutherford