Methods Flashcards
methods, char variables, and boolean (28 cards)
Write the General Structure of a Method
public static returntype methodname (zero or more input variables){
* // create local variables
* // do math ao logic
* // return result based on returntype (if applicable)
}
Function of “methodname” in General Structure
public static returntype methodname (zero or more input variables){
* // create local variables
* // do math ao logic
* // return result based on returntype (if applicable)
}
give a name to method
* use this name in main program to ‘call’ the method
Function of “zero or more input variables” in General Structure
public static returntype methodname (zero or more input variables){
* // create local variables
* // do math ao logic
* // return result based on returntype (if applicable)
}
take data in through input variables
- Note: variable names in main program do not have to match names in method
- Note: if there are no input values, leave ( ) blank when calling method.
- Note: the input variables do not have to match the returntype/output
Function of “returntype” in General Structure
public static returntype methodname (zero or more input variables){
* // create local variables
* // do math ao logic
* // return result based on returntype (if applicable)
}
specifies type of data being output by the method
Examples of ReturnTypes…
String
int
double
boolean
void (a.k.a. nothing)
long
char
Explain Boolean
a variable type which holds the values: true or false
- has no quotations
What word is “char” short for?
character
Special features of Char variables
can use math comparison symbols on them
Function of:
strWord.charAt(integer#)
take the single character with the index/subscript of the number in () from a given string
Write the line of code which takes a single letter in a string USING CHAR VARIABLES.
chrLetter = strWord.charAt(integer#);
Type of quotations used by char variables
single quotations
Type of quotations used by String variables
double quotations
Function of return keyword
returns a value back to main program
Which return type does not need a return keyword?
return type: void
Do local variables in methods exist in the main method?
No
Write code to call a method in a different file (eg. allisontools file)
allisontools.methodname(input variables);
Benefits of Methods
- main programs are shorter
- more efficient – you do not need to rewrite the same lines of code (math/logic) over and over again
- reduces the number of errors – you can write parts of the program at a time and test as you go
- easier collaboration/teamwork – different team members can work on different methods
- money: tool files can be so useful that you can sell them for a price
Methods
pieces of code which programmers can access over and over again when its functionality is needed
When do methods run?
when placed in main method & given proper input
Alternate Names for Methods
Methods
Functions
Procedures
Long Variable Type
integers that can hold numbers larger than 2 billion
integer variable data that can hold numbers larger than 2 billion
Function of Long Variables
Why use int when you have long?
to save RAM
Where must tools files be located
in same folder as the programs which use it