For the first question, it is about comparison of two statement i by combining two statements using Boolean Expressions. This symbol "&&" is referring to "and" while the two straight line is referring to "or". In this situation if using "and" but there is one statement is false, it means that the whole statements are false. It is different with "or", if one statement is true meaning that all statements are true. Here I provided the solution to make it clear.
Answer:
1.
Assuming
that x is 1, show the result of the following Boolean expressions:
BIL.
|
Boolean Expressions
|
Result
|
a.
|
( true ) && ( 3 > 4 )
|
False
|
b.
|
! ( x > 0 ) && ( x > 0 )
|
False
|
c.
|
( x > 0 ) || ( x < 0 )
|
True
|
d.
|
( x != 0 ) || ( x = = 0 )
|
True
|
e.
|
( x >= 0 ) || ( x < 0 )
|
False
|
f.
|
( x != 1 ) = = !( x = = 1 )
|
True
|
Answer:
int
x = 1, a = 3;
Switch (a)
{
case 1:System.out.println(“x
is” + (x + = 5));
break;
case 2:System.out.println(“x
is” + (x + = 10));
break;
case 3:System.out.println(“x
is” + (x + = 16));
break;
case 4:System.out.println(“x
is” + (x + = 34));
break;
default:
System.out.println("Hi!");
int day = input.nextInt();
Switch (day)
{
case
0:System.out.println(“Sunday”);
break;
case
1:System.out.println(“Monday”);
break;
case
2: System.out.println(“Tuesday”);
break;
case
3:System.out.println(“Wednesday”);
break;
case 4:
System.out.println(“Thursday”);
break;
case
5: System.out.println(“Friday”);
break;
default:
System.out.println(“Saturday”);
break;
}
System.out.println ( ( age == 6)? “Ticket price is RM20” : “Ticket price is RM10”)
a. score = ( x > 10 ) ? 3 * scale : 4 * scale;
if (x > 10)
{
System.out.println(“score is” +(3 * scale));
}
else
{
System.out.println(“score is” +(4 * scale));
}
b. tax = ( income > 10000 ) ? income * 0.2 : income * 0.17 + 1000;
if ( income > 10000 )
{
System.out.println(“tax is” +( income * 0.2));
}
else
{
System.out.println(“tax is” +(: income * 0.17 + 1000));
}
c. System.out.println( ( number % 3 == 0 ) ? i : j );
If ( number % 3 == 0 )
{
System.out.println(“i”);
}
else
{
System.out.println(“j”);
}
So, the learning outcome for this task is I am able to use switch statement to specify alternative paths of program's execution. Not only that, after done this lab, I had learned how to use logical operators to combine Boolean expressions and know how to use conditional operators for concise and elegant code for decision making.
Nursaidatul Sakinah Binti Saharuddin
184881
Enjoy reading my blog!
Feel free to share any fruitful knowledge with me as I am eager to learn something new and we can learn together!
Thank you :)
This is my friend's blog. Check him out!
hongjing96.blogspot.com (lLim Hong Jing )
No comments:
Post a Comment