Tutorial 7 TTTK1114 UKM

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

TUTORIAL 7

STATIC VARIABLES AND METHODS

Section A

1. Describe the difference between predefined method and programmer-defined method.

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.

Predefined method : System.out.println(...) , Math.pow(radius, 2);

Programmer defined method : public static double calculateArea() {

b) What do lines 6 and 7 mean?

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.

c) Can identifiers PI and sc being used in the main method?

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

a) Identify the formal parameter and actual parameter.

formal parameter : public static char toGrade (int mark) {


actual parameter : toGrade (mark) ;

b) Note the declaration for variable mark in line 4 and line 24. Can we have two
variables with same name in one program?

Yes , because each variable has different addresses

c) Discuss the scope of variable mark in (d).

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?

Your integer is 100


Your integer is 300
Your integer is 100

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)]

line 11; System.out.print("Your integer is "+numOne*3+"\n");

2. Given the following program:

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

c) What is the output of the following program if the input is 4.0 3 55 9 1?

error, because the input 4.0 is mismatch with the data type int.

Section C
1. Complete the following methods:

a) public static double toCelcius(double fahrenheit) {


// convert temperature from Fahrenheit to Celsius

double result = (fahrenheit*9/5)+32;


return result;

}
b) public static int toSecond(int hour, int min, int sec) {
// convert time to second

int x = hour * 60 * 60;


int y = min * 60;
int result = x + y + sec;
return result;

c) public static String getMonthName(int month) {


// return the month name for month
// e.g. if month is 1, return "January"
public static int getMonthName(int month){
public static int getMonthName (int month)
if(month ==1) { String monthName = " " ;
System.out.println("January"); switch (month) {
} else if (month == 2) { case 1 :
System.out.println("February"); monthName = " January ";
} else if (month == 3) { break;
System.out.println("March");
} else if (month == 4) { case 2 :
System.out.println("April"); monthName = " February " ;
} else if (month == 5) { break;
} System.out.println("May");
} else if (month == 6) { case 3 :
System.out.println("June"); monthName = " March " ;
} else if (month == 7) { break;
System.out.println("July");
} else if (month == 8) { case 4 :
System.out.println("August"); monthName = " April " ;
} else if (month == 9) { break;
System.out.println("September"); .
} else if (month == 10) { .
System.out.println("October"); .
} else if (month == 11) { .
System.out.println("November"); }
} else if (month == 12) { System.out.print(" Month : "+month+ "
System.out.println("December"); \nMonth Name : " +monthName) ;

}
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();

System.out.print("The maximum is " +


maxNumber(numOne, numTwo, numThree));
}
public static int maxNumber(int a, int b, int c){
int max=0;
if(a>b) {
max=a;
}else if(b>c) {
max=b;
}else
max=c;
return max;
}

3. Define a method is_even() to complete the following program. Method


is_even()
is to determine whether the number is even or odd.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer
"); int a = sc.nextInt();
System.out.printf("%d is %s", a, is_even(a));
}

public static String is_even(int a){


String result;
if (a%2==0)
result="even";
else
result="odd";
return result;

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

All the data should display on the screen.

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.

You might also like