Monday, October 8, 2018

Lab3 (Week4 08/10/2018)

Hello, we had our fourth lab on 8 Oct 2018. Programming? Oh no, make me gonna crazy.
However, I felt lucky because I have some smart friends. With the help of them, I am able to done the exercise given by lecturer successfully.

Hence, I would like to share some experience in doing programming.

First of all, I would like to explain about increment and decrement operators. The increment (++) and decrement (--) operators are for incrementing and decrementing a variable (i) by 1. i++ and i--  are known as postincrement and postdecrement while ++i and --i are known as preincrement and predecrement. Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. For example, int x = 34 , int y = ++x . The value of x is 35. y is also 35 because the value of x is first incremented to 34 and is then assigned to y. 

Moreover, boolean it is a data type to declares a variable with the value either true or false. Java provides six comparison operators (also known as relational operators) such as less than, less than or equal to, greater than, greater than or equal to, equal to and not equal to. For example, Is 18 divisible by 2 and 3 (( num % 2 == 0 ) && (num %3 == 0)) The statement will come out with true.

Besides, Java has several types of selection statements which are one-way if statements, two-way if-else statements, nested if statements, switch statements and conditional expressions. Let's look at some examples. Today our lab covers one-way, two-ways and multi-ways statements.

There were several examples shown as below:
* a is 7  because 6 is incremented by 1 . b is still 7 because  a++ is postincrement.
*a is 5 because 5 is decremented by 1. b is also 5 because --a is predecrement.

* if cheese is yellow and moon is yellow, thus the output will show moon is made of cheese.
The code must be written in correct format to eliminate the syntax errors.

*If number divisible by 2 , the output will show number (for example 20) is even. Otherwise, if number is indivisible by 2 , the output will show number (for example 9) is odd.

*We asked to write a program that prompts the user to enter a letter in the alphabets. The program then displays the letter as well as the letters before and after the specified letter. This question is solved by using increment and decrement operator and char datatype. The answers are as following.
*We used scanner to prompt the user so that we can enter number in cmd for x1, x2, y1 and y2. Therefore, we typed formula by using Math.sqrt(Math.pow((x2-x1,2) + Math.pow((y2-y1),2)) in order to count distance between two points.
* We use if-else statement in order to solve this problem. We use 0 in if-else statement to convert from RM to USD , 1 to convert from USD to RM and any other number than 0 and 1 will comes out with output "incorrect input".

* RockPaperScissor question can be solved by using if-else stament with multi-way. First, rock, paper or scissor is generated randomly by using formula given by lecturer int computer = min + (int)(Math.random() * (max-min)+1). We can enter 0 for rock, 1 for paper and 2 for scissor to play with computer.


In short, I am able to write the program to prompt the user to enter the amount in more understandable way and know how to use formula in getting answer. Moreover, the most important in this lab is I get to know what is selection and know how to write program for selection. If-else statement is useful in order to solve the problem which have several choices. The exercise which I did today is quite challenging and I trust there still have more complicated question are waiting us.

Thank you ^^

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