IB Computer Science Unit 1
IB Computer Science Unit 2
IB Computer Science Unit 3
IB Computer Science Unit 4
IB Computer Science Unit 5
IB Computer Science Unit 6

IB Computer Science Methods in Java

IB Computer Science Learning Goals

In this IB Computer Science lesson you will be learning about:

  • Writing Modular Programs by creating your own methods

Why Write Methods

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.

  • Suppose you have a Java program that displays course registration information
  • Suppose that 15 statements are needed to display the data
  • If you don’t have a method to display the information, this means you need to write 15 statements every time you want to display the data.
  •  If you display the form 10 times, then that’s 150 lines of code.
  •  By creating a method to display the information you only write those 15 lines of code 1 time and then can simply call that method every time you want the information to be displayed

Another reason for using a method is to make it easy for you to maintain the program

  • Changes are made in one place – in the method
  • You don’t have to hunt down and change statements in all the places in your program
  • Instead you look in one place in your program and make the change once

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

 

Method Header

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

  • The name should reflect the task that the method performs
  • You should follow the same naming conventions as you would for naming variables

Method Argument List

  • Some books or programmers call this the parameter list
  • The argument list consists of the data the method needs to perform the task
  • The list is formed by specifying the data type and name of each data element passed to the method.

Example
calcGrade (int correctAn, int numberTestQuestions)

The name of an argument should reflect the nature of the data stored in the argument.

  • You use the name of the argument within statements of the method whenever you need to refer to the data stored in the argument.
  • Any number of arguments can be included in an argument list, as long as a comma separates the arguments and the name of each argument is unique.
  • The argument list is optional, only a method that require data from the statement that called the method needs to have an argument list
  • However the method definition still needs the pair of ( ) even if there isn’t an argument list

Returned Value

  • Some methods return data back to the statement that called the method after the method has completed its task, other methods don’t
  •  If data is returned by the method, you must include the data type of that data in the method header.
  •  If data isn’t returned by the method, you must use void as the data type
  •  The data type of the data returned by the method is placed to the left of the method name

Examples


double calcGrade (int correctAn, int numberTestQuestions)

void display ( )

 

Method Body

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 return value is placed to the right of the keyword return in a statement
  • A return value can be a variable, an object, or an expression (the expression is evaluated and that value is returned)
  •  A return statement can be placed anywhere in the body of a method
  •  Keep in mind that no statement below the return statement is executed once Java executes the return statement
  • Multiple return statements can be used to return different values depending on conditions within the body of the method.

The following method accepts two real valued numbers and returns the average of those two numbers as a real valued number

 

IB Computer Science Java methods average

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

IB Computer Science Java distance between points method

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

IB Computer Science Java Even Number Method

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)

Calling Methods

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.

  • Don’t forget to put a semicolon at the end and if your method returns a value that value must be stored in a variable

There are basically 3 things to remember when calling methods:

  • The data passed to a method must be compatible with the data type of the methods argument list
  •  The order in which data is passed to a method must be the same order as the methods argument list
  • You must pass the method the correct number of arguments

 

Example:


The following program shows the 3 methods written above in use by a program

IB Computer Science Java Calling Methods

How Data is Passed to Methods in Java

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

IB Computer Science Java Pass by Value

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

IB Computer Science Practice Questions

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

Looking to Learn More about Computer Science and Coding?

Check out our programing in python courses that focus on high school level coding.  

  • Grade 11 Computer Science ICS3U
  • Grade 12 Computer Science ICS4U

These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions

  • You can find more practice questions on writing programs with methods at the bottom of the page

Return To International Baccalaureate Computer Science Main Page