U3: Predefined Methods Flashcards
Method [A]
- Has own name
- Action already written
- Stored in separate file “class” having name
Method [B]
- Needs info to be sent
- Can return data to main program
- Can be executed from main program
Passing Arguments
Information sent to a method
Outputting Math Methods
System.outprintln(Math.method(num1, num2))
max
- Maximum of two numbers
- int num1 = 34; int num2 = 12;
- Output: 34
min
- Minimum of two numbers
- int num1 = 34; int num2 = 12;
- Output: 12
pow
- First input to power of 2nd
- int num1 = 2; int num2 = 6;
- Output: 64
abs
- Absolute value
- double num = -34.214;
- Output: 34.214
hypot
- (num1^2 + num2^2)^½:
- int sideA = 4; int sideB = 3;
- Output: 5
ceil
- Rounded up nearest integer
- double num = 82.114;
- Output. 82.0
floor
- Rounded down nearest integer
- double num = 87.876
- Output: 87.0
sqrt
- Square root of number
- double num = 64;
- Output: 8.0
round
- Rounded to nearest integer
- double num = 64.545
- Output: 65
random
- Positive double value between 0.0 and 1.0
- System.out.println(Math.random());
- Possible Output: 0.1311034762657053
toDegrees
- Angle in radians to degrees
- double angle1 = 0.7853981633974483;
- Output: 45
toRadians
- Angle in degrees to radians
- double angle1 = 45;
- Output: 0.7853981633974483
sin
- Sine for angle
- double angle1 = 45;
- Output: 0.649448048
cos
- Cosine for angle
- double angle1 = 45;
- Output: 0.760405965
External Class
Separate area from main program that contains methods program uses
tan
- Tangent for angle
- double angle1 = 45;
- Output: 0.854080685
Passing Arguments
“Passing” data from main program to method
Arguments
- Variables passed in method call
- Inside brackets after name of method
Types of Arguments
Integers, Doubles, etc
Parameters
- Defined in method definition
- Appear when writing actual method
words.[starts/ends]With(“string”)
Returns boolean value if string starts/ends with specific word
Method Call
The use of a method to pass data
Finding Location of Character
- Location stated as integers; starts at 0
- Ex: p of “Computer”: 3
words.charAt(integer)
Returns character located at integer position in string
words.toUpper()
Converts string to all uppercase
words.toLower()
Converts string to all lowercase
[variable].replace(“oldChars”, “newChars”)
Replaces character/set of characters in string with new characters/set of characters
[variable].length()
Returns no. of characters in string
[variable].lastIndexOf(“string”)
Outputs location where stated characters are last found
[variable].indexOf(“string”)
- Returns integer where stated characters are first found in string
- Returns -1 if not found