Hello, today is our
lab8 which is Method topic.
Methods can be used to
define reusable code and organize and simplify code.
Firstly, we need to
define a method. How method being defined?
A method definition
consists of its method name, parameters, return value type and body.
For example, public
static int max (int num1, int num2)
Int is return value
type, max is method name, int num1 and int num2 are formal parameters
Next, we need to know
how to call or invoke a method. To execute the method, we have to call it.
There are two types to call a method, depending on whether the method returns a
value or not. If the method does not return a value, the return value type is the keyword void.
The below example is the first question for lab8.
For this question, we are asked to write
a program that assign grades to students in a course. The program prompts the
user to input all first and second tests, laboratory works and final exam marks
and print out the grade for each of the student. Write the method assignGrade()
that receives the mark of a student and display the grade based on the mark.
Firstly, we declare number of student as 5 which means the grade for five students will be shown at the end by using the while loops. Next, we define a method name "marks" in order to count the total marks for students. A method named "assignGrade" is also defined in order to get the grade for each student by using selection statement(if-else). The method is called / invoked in getting the grade for five students at the end.
* Instead of declaring the number of students as 5, we also can use scanner input to prompt the user to enter the number of students.
That's all for today's lab. I am just able to do this question for this lab because other questions other than question 1 are confusing. I will try to investigate and post the answer for the following lab time.
From this week, I have more understanding about method. For example, I am able to know how to define a method and how to call it. Last but not least, I know the differences about a method may return a value or without return value. Thank you. See you again ^^
LIM XUE TING
183615
SSK3100
(Group15)
Next blog: hennyabigail.blogspot.com (Henny
Abigailwillyen Sinjus)