U3: Predefined Methods
No description yet. This curated deck is ready for daily study.
Card preview (24)
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