Wednesday, November 28, 2018

Lab 8 (Week 10 22/11/2018)


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)

Lab 7 (Week 9 15/11/2018)



Hello, today is our lab7 which about Repetition topic. 



The first question is asking about the output. The loop will be kept repeated until the maximum number of type int. The answer is 2147483648.
 




The second question is asked to trace the coding with the selection statement inside the loops statements. The output for the boolean type will show either true or false depend on the statement in the Boolean. When the s is greater than 5, the statement will straight print the s and break and end the program.



The third question is asked to onvert the while loop into a do-while loop. The differences between while loop and do-while loop is while loop will do the loop body if the condition is true and do-while loop will execute the body of the loop first before checking the condition.



The forth question is asked to write a for loop that prints the numbers from 1 to 100. The initial- action is int number = 1, loop continuation condition number <= 100 and action after each iteration number++


The fifth question is asked to convert the for loop statement to a while and do-while loop.
The first picture is the while loop while the second picture is the do-while loop.




The question 6 is about nested loop. The first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes.




The question 7 is asked to write a program that prompts the user to enter a string and then displays the number of vowels in the string. Due to the question is about string, we must use the string type to prompt the user to enter the word in cmd. Then, we need to use if-else statement in solving this problem.



The question 8 is requested to write a program that prompts the user to enter student’s name and the student’s test 1 score and finally display the name of the student with the highest score. The main point for this question is to declare the highestScore as 0 and keep to be compared with other score under the loops statement.



The question 9 is asked to write a program that plays the popular rock-paper-scissor game. The program prompts the user to enter a number 0, 1 or 2 and displays a message indicating whether the user or the computer wins, loses or draws. Let the user continuously play until either the user or the computer wins three times more than their opponent. The coding is written in which program will end  either user or computer wins for three times.





The question 10 is to ask for finding the minimum point on the function f(x) = (x – 2 )2 + 1 where x = [-1, 0, … , 4] using random optimization. This question is quite complicate, but finally I am able to run the program with the help from friends and google.


That's all for today's lab. From lab7, I have more understanding about repetition for loops. The basic in loops must be understood in very detail way so that the question being asked able to be solved. Thank you. See you all again next week.

LIM XUE TING
183615
SSK3100 (Group15)
Next blog: hennyabigail.blogspot.com (Henny Abigailwillyen Sinjus)

Lab 10 (Week 13 13/12/2018)

Heyyy, we had lab 10 which is our last lab. It is about two dimensional array. What is two dimensional array?? Let me explain it ! The e...