Practical 05: Methods
Aside from being able to modularize code, using methods is integral to when we start covering Classes and Objects in the next session.
Info
Methods and functions tend to be used interchangably, however methods have a deeper meaning than functions. Functions are used to carry out a process that changes input into output. Whether these functions are useful in a certain context is dependent on when and/or where they are used.
Within the OOP context, these functions are sometimes called methods (sometimes called operations). You can view methods as like a way how objects do things. For example, the method a person speaks involves opening their mouth and letting out their voice. In this case, unlike functions, methods are meant to have meaningful purposes.
Activity: Decomposing a Complex Program into Methods
Recall the simple calculator application you created in Practical 03 Task 6. The prepared solution can be split into several methods carrying out different purposes.
- Obtaining the option to choose the type of arithmetic operation to be carried out
- Obtaining user input of two integer values
- Displaying results from user inputs
Consider each of the following methods and determine what return type best fits the solution at hand.
Method 1: Choosing Arithmetic Operation
The discussed solution involved obtaining an option with the use of a while loop like as follows:
Let's encapsulate this part of the solution into a method called selectOperation
.
The end result is the integer variable option
.
Therefore, we use the int
return type for the method.
At the end of this method, we ought to put in a return statement to return option
back to the method that called it in the first place.
In this case, it would be the main method.
selectOperation()
should look as follows:
Notice that we also added the private
and static
keywords in front of the method declaration.
The use of the private
keyword makes a method or variable such that it's only accessible within the class itself.
As for the static
keyword, for the time being just know that you'll need to use static
whenever you're linking methods in a class where a main method is involved.
In the main
method, enter this in place of the code you just moved into the newly created method.
This will retrieve the returned value from the selectOperation()
method and keep it in a local variable called option
.
Method 2: User Input of 2 Integer Values
This method should retrieve 2 values as according to the code snippet below:
Let's name the method containing this code snippet getUserInput()
.
Apart from using primitive data types, one can also return arrays of such data types from a method. We can combine these two numbers into an integer array like as follows:
Here, we are assuming that the first value entered corresponds to results[0]
and the second value corresponds to results[1]
.
Like the previous method, let's call the getUserInput()
method from the main
method and save the array in a local array variable in the main
method.
However, note that we do not have a Scanner object (let alone one called input
) in this method.
That Scanner object now belongs to the selectOperation()
method.
If you run the program now, it will throw an exception stating something along the lines of input
being non-existent within this newly created method.
In order to fix this, we will make the Scanner object global within the class.
Remove the Scanner object declaration from selectOperation()
and redeclare it as follows at the beginning right after the class declaration:
Method 3: Displaying Results
Now all that's left is to print out the results from the switch statement. The code snippet from the discussed solution is as follows:
Here, we are only printing statements; printing statements do not constitute as returning anything to the method that called it in the first place.
Since we are not expecting to returning any value at all, this new method we call printResults()
will have a void
return type.
Note here that this time, we are passing parameters into this method. This is because unlike the last 2 methods which did not require any input from the method that called it, this method requires those 3 variables in order to function. This method is assuming that the first value is to be regarded as the operator option selected, followed by the first and second input numbers from the user respectively.
There is no set rule as to when and where a method should require parameters. Whether or not a method should require parameter(s), in the declaration, how many of them are required, and/or consequently return value(s) is dependent on how you design your solution.
In this case, since printResults()
does not return any value to the method that called it, we do not need to store anything into a local variable.
In the main
method, invoke this method as is without equating it to a local variable like as follows.
However, since it requires 3 parameters for it to work, when invoking the method you will need to enter the required values (being option
and userInput
).
Putting them altogether
And voila, you now have a solution that's more modular by splitting a working solution into 3 different methods that work separately from each other. The main method is now relegated to only invoking methods as and when they are needed.
Tasks
Task 1
Write a method that computes the multiplication of the digits in an integer. Use the following method header:
For exmaple, mulDigit(234)
returns 24 (i.e., 2 × 3 × 4).
Hint
Use the %
operator to extract digits, and the /
operator to remove the extracted digit.
For instance, to extract 4 from 234, use 234 % 10
(= 4).
To remove 4 from 234, use 234 / 10
(= 23).
Use a loop to repeatedly extract and remove the digit until all the digits are extracted.
Write a test program that prompts the user to enter an integer and displays the multiplication of all its digits.
Write a test program that
- prompts the user to enter an integer, then displays the result of all its digits multiplied together
- randomly generates a number ranging between (and inclusive of) 100 and 10,000, then displays the result of all its digits multiplied together
Task 2
Create a class called PrimeNumber
which contains 3 methods:
isPrimeNumber(int num)
This will return true ifnum
is prime, and false otherwise.getListOfPrimeNumbers(int start, int end)
This will return the list of prime numbers between thestart
andend
number.getDivisible(int num)
This will return the list of divisible numbers for the provided number.
Task 3
Write a method that converts a hexadecimal number into a decimal number. For example,
ABCD16 = A × 163 + B × 162 + C × 161 + D × 160 = 43981
You may use the following reference:
Hexadecimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
Once you have completed your method, you can verify your result by calling the following function:
Test your program with the following values: 2AC, AB3F, FF99
Task 4
Write a class that contains the following two methods:
/* Convert miles to kilometers */
public static double mileToKilometer(double mile)
/* Convert kilometers to miles */
public static double kilometerToMile(double kilometer)
The formula for the conversion is: 1 mile = 1.609 kilometers
Write a test program that invokes these methods when necessary after prompting to enter either a value in kilometers or miles (ask for choice first).
Task 5
Write a class that contains the following two methods:
/* Convert pounds to kilograms */
public static double poundToKilogram(double pound)
/* Convert kilograms to pounds */
public static double kilogramToPound(double kilogram)
The formula for the conversion is:
pound = 0.453 × kilograms
kilogram = 2.204 × pound
Write a test program that invokes these methods when necessary after prompting to enter either a value in kilograms or pounds (ask for choice first).
Task 6
Utilize methods to solve this multidimensional array problem.
Write a program that randomly fills in 0s and 1s into a Tic-tac-toe board, prints the board, and find the rows, columns, or diagonals with all 0s or 1s. Use a two-dimensional array to represent a Tic-tac-toe board.
Hint
Use Math.random() * 2
to randomly generate the 0s and 1s.
Here are some sample runs of the program:
TicTacToe board:
0 1 0
1 1 1
1 1 0
Result(s):
All 1's on row 1
All 1's on column 1
TicTacToe board:
0 1 1
0 0 0
0 0 1
Result(s):
All 0's on row 1
All 0's on column 0
TicTacToe board:
0 1 0
1 0 1
0 0 1
Result(s):
All 0's on subdiagonal
TicTacToe board:
1 1 1
0 1 0
1 1 1
Result(s):
All 1's on row 0
All 1's on row 2
All 1's on column 1
All 1's on major diagonal
All 1's on subdiagonal