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

No comments:

Post a Comment