Applet Graphical User Interface Event

Download Report

Transcript Applet Graphical User Interface Event

Java: How to Program

Methods Summary

Yingcai Xiao

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

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\n%s!\n", getCourseName() ); } // end method displayMessage } // end class GradeBook

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 } // end class GradeBookTest

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\n%s!\n", getCourseName() ); } // end method displayMessage

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 ;

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.

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); } }

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 can’t be changed static: you don’t need to create a Math object to use it and there is only one copy for the Math class.

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.

Parameters and Arguments • Argument promotion

– Converting an argument’s 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 occur—otherwise a compilation error occurs.

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( scalingFactor );

Enumeration – A n 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

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 field’s 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

Method overloading

– Methods of the same name declared in the same class – Must have different sets of parameters – Example: Figure 6.15