Chapter 7 Flashcards
What is a user-defined method
Methods created by the user.
What is a pre-defined method.
Methods already created for use.
Examples: Println; printf; showInputDialog
Advantages of user defined method.
- Allows you to reuse methods many times
- Different people can work on different methods in the program simultaneously
- Allows user to focus on one part of the program at a time.
- Enhances readability of a program; reduces amount of code in program.
What are the various pre-defined methods used to separate a person’s first and last name?
- length
- indexOf
- subString
What is the pre-defined method, length?
Tells you the number of characters in a given string.
strLen = name.length( );
What is the pre-defined method, indexOf?
Returns the location of the parameters entered into the method.
Example: we use a “space” when separating first and last names
spaceLoc = name.indexOf(“_”);
What is the pre-defined method substring?
Extracts a string from within a string
Example:
firstName = name.substring(0,spaceLoc)
How does one know what pre-defined methods exist and how to use them?
Using a programming textbook or class.
What info must you know about a predefined method in order to use it.
- name of the method
- The parameters that are required to use it.
- The method type (what the method will return)
Define the term parameters.
Listed after the method name in parentheses. Determines the output. Need to list parameters in the order that it is defined in the method.
Define the term method type.
The return type given from a method.
Data type
List methods’ return types and number of parameters.
- length returns an int data type, and has no parameters.
- indexOf returns int, and has 1 parameter.
- substring returns a String, and has 2 parameters.
List and describe the 2 types of user defined methods.
- Value returning: must do something with the returned value.
- Void: does not return a value.
List and describe the 2 parts of a method definition.
- Heading: lists return type, name of the method, the number and data of parameters.
- Body: gives code to accomplish the task performed by the method.
Give an example of a method definition.
modifier(s) returnType methodName (formal parameter list)
{ //code for method. Tasks performed here. }
Define formal parameters.
The variables declared in the heading, within parentheses.
Define Actual Parameters.
The variables or expressions used in a call to a method.
What modifier is used for the methods we used in class?
We used Static and final.
But here are some more:
private, public, abstract, protected.
Be able to code a simple method, similar to what was created in class.
Look at program for this.
How many formal parameters can a method have?
0 or more.
What statement is used to return a value from a method?
return ____;
The blank could be a calculation, variable name, or literal name.
Be able to call a method in from main
Look at programs.
Is a literal value or expression allowed to be used as an actual parameter?
Can’t find info on this question.