In this IB Computer Science lesson you will be learning about:
A method is the part of a Java program that contains the logic to perform a task. You have been using methods all along to perform different tasks. A major reason for using methods is to drastically reduce the number of duplicate statements in a program.
Another reason for using a method is to make it easy for you to maintain the program
Before a method can be called within a program, it must be defined in a method definition within the definition of a class. A method definition contains all the statements necessary for the method to perform a task.
It has two parts: The method header and body of the method
The method header contains three basic elements: the method name, the method argument (or parameter list), and the data type of the value returned by the method
Method Name
Method Argument List
Example
calcGrade (int correctAn, int numberTestQuestions)
The name of an argument should reflect the nature of the data stored in the argument.
Returned Value
Examples
double calcGrade (int correctAn, int numberTestQuestions)
void display ( )
The body of the method is defined within the opening and closing braces and appears below the method header. Java executes the first statement below the opening brace and continues to execute statements sequentially until the closing brace is reached or a return statement is executed. Programmers call data that is returned by a method a return value.
The following method accepts two real valued numbers and returns the average of those two numbers as a real valued number
The following method accepts 4 integer values representing points (x1, y1) and (x2, y2) on a Cartesian coordinate grid and returns the real valued distance between those two points
The following method returns true of false, and accepts 1 integer argument. The return value is true if that argument is an even number and false if it is an odd number
You will have noticed the keyword static in all the method definitions above, this is because for the time being you will call or invoke your methods from your main method, which must be static. (This will be explained when we study object oriented programming later in the course)
To call your created method you must type the method name and put the arguments that you wish to pass to your argument list inside the brackets.
There are basically 3 things to remember when calling methods:
Example:
The following program shows the 3 methods written above in use by a program
Before data is passed to a method, it is stored in memory. When the method is called, Java allocates memory based on the data types of the arguments specified in the method definition. Java then makes available to the method the data passed by the statement that called the method Data is made available to a method in two ways: pass by value or pass by reference
Pass By Value:
Java makes a copy of the data passed to the method and stores the copy in memory. Statements within the method can change the value of data passed to it, but that change isn’t available to the statement that called the method, because each has its own copy of the data
Both print statements will print the value 5 on the screen
Even though the method modified the value of a, it only modified it in its own method, that is the variable ‘a’ is local to its own method
Java basically considers those two completely separate variables
Write methods to accomplish the following tasks. Test them using your main method.
Draws a square on the screen. Accept the side length of the square as an argument in the method.
Calculates the sum of all integers between 2 values. Accept two integers as arguments and return the integer sum of all the numbers (Exclusive) between those integers.
Displays your signature to the screen. Accept no parameters, just output a simple message with your name, school, phone number, and email address
Displays a character a certain number of times on the screen. Accept the character to print, and how many times to print it as arguments to the method.
Calculates the Volume of a triangular prism. Based on the picture below decide what values to accept as arguments to the method and return the volume rounded to the nearest decimal point.
Finds the biggest of 3 numbers. Accept 3 numbers as arguments
and return the largest.
Reduces a fraction to lowest terms. Accept the numerator and denominator as arguments to the method and returns a String in the form “n/d” of that fraction in lowest terms.
Checks if a number is a prime number. Accept an integer as an argument and return a true or false value depending on if the integer is prime or not. Remember a number is prime if has no divisors other than 1 and itself.
Calculates a factorial of an integer. Accept an integer and return its factorial.
o 5! = 5 x 4 x 3 x 2 x 1 = 120
o 3! = 3 x 2 x 1 = 6
Checks if a year is a leap year. Accept a year and return true or false if it is a leap year or not.
o A leap year is a year in which February has 29 days and is determined as follows:
▪ A year that is divisible by 4 is a leap year but not all centennial years are leap years..
• Centennial years are divisible by 100.
• Centennial years are only leap years if they are divisible by 400.
Complete the following as Practice
Check out our programing in python courses that focus on high school level coding.
These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions
Return To International Baccalaureate Computer Science Main Page