LETU COSC 2103 - Methods (22 pages)

Previewing pages 1, 2, 21, 22 of 22 page document View the full content.
View Full Document

Methods



Previewing pages 1, 2, 21, 22 of actual document.

View the full content.
View Full Document
View Full Document

Methods

126 views

Lecture Notes


Pages:
22
School:
LeTourneau University
Course:
Cosc 2103 - Computer Science II
Computer Science II Documents

Unformatted text preview:

Methods Chapter 6 Program Modules in Java What we call functions in C are called in Java Purpose Reuse code Modularize the program This can be done by putting the code in a method Various objects in a program can invoke the same method 2 Predefined Methods The Math class These methods are called by The of the class The dot operator The name of the Example double x Math sqrt 900 0 3 Method Declaration Syntax modifiers returnType methodName parameterDeclaration statements modifiers describers etc returnType type of value returned by method or if it does not return a value methodName identifier that names the method parameterDeclaration list of parameters separated by statements define the behavior of the method 4 Argument Promotion Coercion of arguments Forcing arguments to appropriate type to pass to method System out println Math sqrt 4 Evaluates Math sqrt 4 Then evaluates System out println 5 Argument Promotion Promotion rules Specify how to types without data loss 6 Method Definitions Consider a method to square a number public int square int y return y y return square of y end method square We will place it in an SquareIntegers java 7 Method Definitions Consider a method to find the maximum of three numbers in an adapted version of Figure 6 4 Note It is an The maximum function must be not needed 8 Java API Packages Predefined classes grouped into categories of related classes Called Called the Java Application Programming Interface API Note the often used API packages 9 Random Number Generation Consider the random method in the Math class Generates a random double 0 n 1 0 The number can be manipulated and as an int to get desired range For the roll of a die 1 int Math random 6 Note Figure 6 7 and Figure 6 8 10 Game of Chance Rules for Craps Roll dice first time If sum equals 7 or 11 the player wins If sum equals 2 3 or 12 the player loses Any other sum 4 5 6 8 9 10 is that player s point Keep rolling dice until Sum matches player point Player wins Sum equals 7



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Methods and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?