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. }