Java: How to Program Methods Summary - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Java: How to Program Methods Summary

Description:

Java: How to Program Methods Summary Yingcai Xiao – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 17
Provided by: xiao125
Category:

less

Transcript and Presenter's Notes

Title: Java: How to Program Methods Summary


1
Java How to ProgramMethodsSummary
  • Yingcai Xiao

2
  • How to Declare/Define a Method (Chapter 3)
  • A method is just a named block of code that can
    be reused/called/invoked again and again.
  • A method must be define inside a class.
  • General format
  • public return-type method-name (parameter list)
    // header of the method
  • // body of the method
  • Examples Figures 3.7-3.8

3
  • How to Declare/define a Method (Chapter 3)
  • public class GradeBook
  • private String courseName // course name for
    this GradeBook
  • // method to set the course name
  • public void setCourseName( String name ) //
    return type void no return value
  • courseName name // store the course name
  • // return back to the calling method even
    though there is no return value.
  • // method to retrieve the course name
  • public String getCourseName()
  • return courseName
  • // end method getCourseName
  • // display a welcome message to the GradeBook
    user
  • public void displayMessage()
  • // calls getCourseName to get the name of
    the course this GradeBook represents
  • System.out.printf( "Welcome to the grade
    book for\ns!\n", getCourseName() )
  • // end method displayMessage
  • // end class GradeBook

4
  • How to use/call a Method (Chapter 3)
  • import java.util.Scanner // program uses Scanner
  • public class GradeBookTest
  • // main method begins program execution
  • public static void main( String args )
  • // create Scanner to obtain input from
    command window
  • Scanner input new Scanner( System.in )
  • // create a GradeBook object and assign it
    to myGradeBook
  • GradeBook myGradeBook new GradeBook()
  • // display initial value of courseName
  • System.out.printf( "Initial course name is
    s\n\n", myGradeBook.getCourseName() )
  • // prompt for and read course name
  • System.out.println( "Please enter the
    course name" )
  • String theName input.nextLine() // read
    a line of text
  • myGradeBook.setCourseName( theName ) //
    set the course name
  • System.out.println() // outputs a blank
    line
  • // display welcome message after specifying
    course name
  • myGradeBook.displayMessage()
  • // end main

5
  • How to use/call a Method (Chapter 3)
  • You need to create an object of a class in order
    to use its (non-static) methods.
  • General format
  • object.method(argument list) // argument list
    must match parameter list in type
  • String theName input.nextLine() // read a
    line of text
  • myGradeBook.setCourseName( theName ) //
    set the course name
  • General format
  • For methods returning a value, it can be
    assigned to a variable.
  • String cName myGradeBook.getCourseName()
  • System.out.printf( "Initial course name
    is s\n\n",cName)
  • When calling a method of a class inside another
    method of the same class, no object needs to be
    create, just call the method.
  • public void displayMessage()
  • System.out.printf( "Welcome to the grade book
    for\ns!\n", getCourseName() )
  • // end method displayMessage

6
  • How to use/call a Method (Chapter 3 and chapter 6)
  • Three ways to call a method
  • Using a method name by itself to call another
    method of the same class
  • Using a variable that contains a reference to an
    object, followed by a dot (.) and the method name
    to call a method of the referenced object
  • Using the class name and a dot (.) to call a
    static method of a class
  • Three ways to return control to the statement
    that calls a method
  • When the program flow reaches the method-ending
    right brace
  • When the following statement executes
  • return
  • When the method returns a result with a statement
    like
  • return expression

7
  • Static
  • static methods
  • public class MyApp
  • public static void main(String args)
  • .
  • No need to create an object to use static methods
    in a class.
  • Math.max(1,2)
  • Can only invoke other static methods inside a
    static method.

8
  • Summary on Static
  • static fields
  • Class vriables one copy shared by all objects of
    the class
  • Example, object counter
  • class MyObject
  • public int i
  • public static int c
  • MyObject() i c
  • public class MyApp
  • public static void main(String args)
  • MyObject obj1 new MyObject()
  • MyObject obj2 new MyObject()
  • MyObject obj3 new MyObject()
  • System.out.printf(i is d, obj3.i)
  • System.out.printf(Number of object has been
    created d, obj3.c)

9
  • Final
  • Final fields
  • Vales can only be initialized at initialization
    and can not be changed.
  • final float PI 3.14
  • Static and final
  • Math.PI
  • final its value cant be changed
  • static you dont need to create a Math object
    to use it and there is only one copy for the Math
    class.

10
  • Parameters and Arguments
  • Number and types of arguments in a method call
    should match the number and types of the
    parameters of called method.
  • Called method
  • public double maximum( double x, double y,
    double z )
  • Calling method
  • double max maximum (1.2, 3.4, 3.5)
  • The signature of a method / function consists of
    the name, number of parameters, type of each
    parameter and the order of them. (Note return
    type is not part of the signature.)
  • Any part of the signature differs, you get a
    different method / function.
  • The signature of the calling statement should
    match the signature of the called method.

11
  • Parameters and Arguments
  • Argument promotion
  • Converting an arguments value, if possible, to
    the type that the method expects to receive in
    its corresponding parameter.
  • In cases where information may be lost due to
    conversion, the Java compiler requires you to use
    a cast operator to explicitly force the
    conversion to occurotherwise a compilation error
    occurs.

12
(No Transcript)
13
  • Random Number Generator
  • Scale and shit can be used to control the range
    of the random numbers being generated.
  • face 1 randomNumbers.nextInt( 6 )
  • number shiftingValue randomNumbers.nextInt(sca
    lingFactor)

14
  • Enumeration
  • An enumeration in its simplest form declares a
    set of constants represented by identifiers.
  • Special kind of class that is introduced by the
    keyword enum and a type name.
  • Definition Example
  • private enum Status CONTINUE, WON, LOST
  • Usage Example
  • Status gameStatus // can contain CONTINUE, WON
    or LOST
  • gameStatus Status.WON // write to
  • if(gameStatus Status.WON) . // read
    from

15
  • Scope
  • The scope of a declaration is the portion of the
    program that can refer to the declared entity by
    its name.
  • The scope of an identifier (variable or method
    name) is the inner most block it is defined.
  • The scope of a parameter in the parameter list of
    a method is the method.
  • The scope of a loop variable defined in the for
    loop header is the body of the loop.
  • A method or fields scope is the entire body of
    the class.
  • The scope of a local-variable declaration is from
    the point at which the declaration appears to the
    end of that block.
  • Example
  • Figure 6.12

16
  • Method overloading
  • Methods of the same name declared in the same
    class
  • Must have different sets of parameters
  • Example
  • Figure 6.15
Write a Comment
User Comments (0)
About PowerShow.com