Wednesday, 28 November 2018

Lab 5 Switch Statement, Logical Operators and Conditional Operator

Good morning!! This is my reflection for Lab 5 session. I am quite happy for this lab session as it is quite easy to do after having a revision on the lecture slides. Let us begin our journey~~


QUESTION 1
Assuming that x is 1, show the result of the following Boolean expressions:

a.  True
b. True
c. False
d. False
e. False
f. True






QUESTION 2
What is x after the following if-else statement is executed? Use a switch statement to rewrite it and draw the flowchart for the new switch statement.

Execute using if-else statement

Rewrite using switch statement

Getting the same result:

 

 Flowchart:


QUESTION 3
What is y after the following switch statement is executed? Rewrite the code using an if-else statement.

Execute the code:

Rewrite using if-else statement:

Getting the same result:


QUESTION 4
Write a switch statement that displays Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, if day is 0, 1, 2, 3, 4, 5, 6 respectively.



QUESTION 5
Write a program that randomly generates an integer between 1 and 12 and displays the month names January,...,December for the numbers 1,... ,12 accordingly.


We can use "Math.random()" method to generate random numbers of the double type and it can be use with or without parameters. For this question, I am using a standard expression which is "min + Math.random()*(max-min) +1" in order to generate numbers within a range.


QUESTION 6
Rewrite the following statements using the conditional operator.

Result:


QUESTION 7
Rewrite the following codes using if-else statement:

Result:
a)
 b)
 c)


QUESTION 8
Write two programs to read in three numbers from the console and output if the numbers are sorted or not using: 1. if-else statement and 2. Using conditional operator.



QUESTION 9
Write a program to compute personal income tax. Your program should prompt the user to enter the filing status and taxable income and compute the tax. Enter 0 for single filers, 1 for married filing jointly or qualified widow(er), 2 for married filing separately, 3 for head of household. Use switch statement to handle these cases. Your program computes the tax for the taxable income based on the filing status. The table below shows the rates for a certain year. If you are single with a taxable income of $10,000, the first $8,350 is taxed at 10% and the remaining $1650 is taxed at 15%, so your total tax is $1082.50.

Answer:



Output:


Henny Abigailwillyen Sinjus
183640
SSK3100 (Group 15)


Next blog: nurnadhirahzulkepli.blogspot.com

Sunday, 11 November 2018

Lab 4 Multi-way and Nested Selections

Hi everyone. I'm back😆. 
For today lab session, we need to do our own text adventure game and I cannot wait to do it! We need to write a program to get the user instructions so that the player can move from one room to another by typing in commands of direction. The adventure game should has at least ten rooms to explore and two ways for the game to end. The objective is that we can practice how to use multi-way and nested selection statement to specify alternative paths of a program's execution.

I plan to do a game where a player needs to find a mystic flower by choosing correct paths. It is quite easy to do the program at first but I cannot run it when it comes to Room 1. I mean I still can run it but after I put a direction, there is no instruction come out after that. Then I use "direction.equals (west)" instead of "directions = west" and change "String direction1 =input1.next();" to "String direction1 = input.nextLine();". 

The next() method can read the input only till space or in other words, it cannot read two words separated by a space. It also places the cursor in the same line after reading the input. While the nextLine() method reads everything including space between the words until the end of the line. Once the input is read it places the cursor in the next line.

After that, I try to run it again and success except for the last part where it non-stop shows "Argon:Congratulations! You have found the flower." repeatedly till my laptop hang for awhile. Therefore, I put a "break" to solve the problem.

So yeah, I have finished my own adventure game with a lot of hassle😭 but at least I am satisfied with my work. See you guys again in my next lab session. Bye~~







Henny Abigailwillyen Sinjus
183640
SSK3100 (Group 15)


Next blog: nurnadhirahzulkepli.blogspot.com

Friday, 2 November 2018

Lab 3 Problem Solving, Data Types and Selection

We are having our fourth lab session today. Programming is quite interesting but the more I learn the harder it is to understand.   

QUESTION 1
Trace the programs and show their output.

Test 1




Test 2


Even or Odd Number







QUESTION 2
Uncomment the line of Test 2 in question 1, compile and run it again.






QUESTION 3
Fix the error in Even Or Odd Number class from question 1.



I put “else” so that it can choose only one of the choices.


QUESTION 4
Find and fix the errors in Test3 class.



Correction: 
I put a bracket for “cheeseIsYellow” and “moonIsYellow” for a proper language.




QUESTION 5
Write a program that prompts the user to enter a letter alphabets. The program then displays the letter as well as the letter before and after the specified letter.



I just know that the scanner class in Java has nextInt() and nextDouble() but there is no nextChar(). We need to use next().charAt(0) to read a character.


QUESTION 6
Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance. Math.pow(a, 0.5) can be used to compute the square root formula.



The formula is the hardest to write and solve. I get errors every time I try to write a new formula. It took me a day to solve it until I finally ask my classmates how. Math.pow(double a, double b) is for two parameters where it is used to calculate a number raised to the power of the second number.



QUESTION 7

Write a program that prompts the user to enter the exchange rate from Malaysian Ringgit (RM) to US Dollar (USD). Prompt the user to enter 0 to convert from USD to RM and 1 to convert from RM to USD. Prompt the user to enter the amount in USD or RM to convert it to USD or RM respectively. 





"Break” statement is necessary to avoid passing through to the code under the next label.


QUESTION 8
Write a program that plays the popular rock-paper-scissor game. 






It is quite easy to do for this question.


Henny Abigailwillyen Sinjus
183640
SSK3100 (Group 15)


Next blog: nurnadhirahzulkepli.blogspot.com