4 Methods
Oracle Academy Section
Section 4: Java Methods and Library Classes
Lesson 1: What is a Method?
Lesson 2: The import Declaration and Packages
Section 4 Quiz 1 (Lessons 1-2)
Java Foundations Certification Exam Topics
Java Methods
Describe and create a method
Create and use accessor and mutator methods
Create overloaded methods
Describe a static method and demonstrate its use within a program
Key Resources
Java Tutorials:
Classes and Objects
Udemy
Classes and Objects (11:16) class in separate file, class in same file, objects, a class is like a type, classes can contain data (instance variables) and subroutines (methods), class Person with name and age, person1 and person2
Methods (11:06) continuation of Person class example, speak, Ctrl+Shift+F, sayHello, no parameters, all void
Getters and Return Values (10:32) Person, speak, calculateYearsToRetirement, returning values, getAge, getName, encapsulate
Bucky's Room
Use Methods with Parameters (6:40) apples, tuna
Supplemental Resources
Language Companion - classes (CellPhone) gets, sets, constructors. IMPORTANT
Logic Ch 3 Modules
Language Companion - defining, calling, arguments, parameters, scope
Logic Ch 6 Functions
Language Companion - random numbers, writing value returning methods, Math methods, conversion methods (Double.parseDouble, Integer.parseInt), DecimalFormatter, String methods (length, toUpperCase, substring, indexOf)
Other Videos
Writing a Function Pseudocode
The Rectangle Area Problem Pseudocode and Generic Flowchart
Most Important Concepts / Code
public double calculateAnswer(double wingSpan, int numberOfEngines,
double length, double grossTons) {
//do the calculation here
}
A CALL contains an ARGUMENT. A HEADER contains a PARAMETER.
A method is a group of statements that have a name.
Other names for methods include modules, functions and subprocedures.
The code for a method is known as a definition or declaration.
Method declarations have six components, in order:
Modifiers—such as public, private, and others you will learn about later.
The return type—the data type of the value returned by the method, or void if the method does not return a value.
The method name—the rules for field names apply to method names as well, but the convention is a little different.
The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
An exception list—to be discussed later.
The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
The top line of a method is called a header.
Method signature - the method's name and the parameter types.
The line that causes a method to run is a call.
Because of the concept of scope, variables can only be referenced in the method where they are created.
Because of scope, if you want to use a variable in a different module you must pass the variable to the method in parentheses.
The variable you pass is called an argument.
Methods should have descriptive names that include a verb and use camelCase.
It is good to make general purpose, reusable methods. Make methods to just do processing, not input or output.
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int sum = getSum(console);
System.out.println("The sum is " + sum);
}
public static int getSum(Scanner console) {
System.out.print("Type 2 numbers: ");
int num1 = console.nextInt();
int num2 = console.nextInt();
return num1 + num2;
}
Hour 1
Quiz and Concepts Review
Module names:
In other programming languages, the term function is used for a module that returns a value.
Visualizing methods, variables, scope.
Benefits of methods
simpler (small sections)
code reuse (make methods general purpose, just processing)
better testing
faster development
easier facilitation of teamwork
Make a class with gets and sets
Exercises
Hour 2
Incorporating methods
Order of methods doesn't matter to Java. Be logical. main at top.
If many methods read input, declare a Scanner in main and pass it to the others.