Professional Documents
Culture Documents
Tutorial 7 TTTK1114 UKM
Tutorial 7 TTTK1114 UKM
Tutorial 7 TTTK1114 UKM
Section A
Predefined method is built-in methods in java package that is ready to use anytime while programmer
defined method is a method that is created and defined by programmer.
2. Describe the difference between a void method and a value-returning method. Give one
example for each.
Void method : perform task and then terminates (does not return value)
Example : public static void (...) {
Value-returning method : perform task then returns a value back to the code to call it.
Example : static boolean (...) {
//statement
return true;
}
3. Given the following program:
Figure 1: Program
6.1
a) Identify the predefined method and programmer-defined method used in the program
above.
line 6 : declare variable PI as a constant with a data type double that will never change
(global variable)
line 7 : creates new object of Scanner type from the standard input program.
No, because it is declared as local variable for main method which can only be used in
main method
4. Given the following program:
// call method
Figure 2: Program
6.2
b) Note the declaration for variable mark in line 4 and line 24. Can we have two
variables with same name in one program?
The variable mark in line 4 is within char toGrade method access from line 5 until 16
The variable mark in line 4 is within char toGrade method access from line 21 until 26
d) Discuss the scope of variable grade in line 5 and line 25.
char
Section B (Hands-tracing)
1. Given the following program:
// method call
100
300 100
300
Figure 3: Program
6.3
a) What is the output of Program 6.3?
b) Explain why the value of numOne is still 100 at line 11 after passing it to
method
tripleInt() at line 9?
In method tripleInt, the original values are not getting modified. So, after calling the
tripleInt method, when you try to print the numOne variable inside the method, the
originial value will be printed instead.
c) Which syntax(s) need to modify for the value of numOne to be 300 after passing it
to method tripleInt() at line 9? [Identify the line code and write the correct
syntax(s)]
Figure 4: Program
6.4
a) State the name of method, method header and method declaration in the program.
Briefly explain what the method does:
method name : hamonic
- name that is descriptive of what the method does
method header : public static double harmonic (int n) {
- comprises the access modifiers (public static), return type (int), method name and
parameters
the method will take the argument from main method (arg) and count sum by 1.0
divided with i for N length
b) What is the output of the following program if the input is 5 2 5 1 0 10?
1.5
2.283333333333333
1.0
0.0
2.9289682539682538
error, because the input 4.0 is mismatch with the data type int.
Section C
1. Complete the following methods:
}
b) public static int toSecond(int hour, int min, int sec) {
// convert time to second
}
return month;
2. Define a method maxNumber()to complete the following program. Method
maxNumber() is to find the largest of the three integers.
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in); int numOne =
sc.nextInt();
int numTwo = sc.nextInt();
int numThree = sc.nextInt();
}
4. A food catering company has determined that for every 1000 of guests, 900 kg of
rice and 5 hours of cooking time are needed to cook the rice. The company also
charges RM25.00 per hour for labor. Write a program that allows the user to enter
the number of guests, and the price of the rice per kg. The program should have
the following methods that return the following data:
• The total weight (kilograms) of rice required
• The hours of labor required
• The cost of the rice
• The labor charges
• The total cost of cooking the rice.
5. Write a method named display() whose return type is void and accepts an array and size
of array as input parameters. The method displays the content of the array in reverse
form by passing an array and its length.
4.
5.