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

  1. Java Tutorials:

    1. Classes and Objects

      1. Defining Methods

      2. Passing Information to...

      3. Returning a Value from...

  2. Udemy

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

    2. Methods (11:06) continuation of Person class example, speak, Ctrl+Shift+F, sayHello, no parameters, all void

    3. Getters and Return Values (10:32) Person, speak, calculateYearsToRetirement, returning values, getAge, getName, encapsulate

  3. Bucky's Room

    1. Use Methods with Parameters (6:40) apples, tuna


Supplemental Resources


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:

  1. Modifiers—such as public, private, and others you will learn about later.

  2. The return type—the data type of the value returned by the method, or void if the method does not return a value.

  3. The method name—the rules for field names apply to method names as well, but the convention is a little different.

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

  5. An exception list—to be discussed later.

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

  • Command-Line Arguments

  • Methods Activity

  • Make a class with gets and sets

  • Exercises


Hour 2

  • Scrum Meeting

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