Вы находитесь на странице: 1из 3

IAS1313: Object Oriented Programming

Lab 5 and 6: Method- Control Structure


1. The Selection Control Structure- Simply way
You can use the selection control structure in syntax to illustrate a choice between two or more
actions, depending on whether a condition is true or false.
General Syntax:
if (condition)
statement;
else
statement

Question 1:

Method named calculates salary with the following condition:


Salary Range Interest Rate
More than 20,000 5%
Less than 20,000 2%

2. The Selection Control Structure- Nested


A non-linear nested IF occurs when a number of different conditions need to be satisfied
before a particular action can occur. It is termed non-linear because the ELSE statement may
be separated from the IF statement with which it is paired.
General Syntax:

if (condition)
{
if(condition)
statement;
else
statement;
}
else
statement
Question 2:

Employee Type Hours Worked Salary


Salaried Employee Salary= 4000
Hourly Employee Hours <= 40 Hourly Rate 30
Hourly Employee Hours > 40 8 hours 30 per hour
OT 40 per hour

Answer:
if(Employee_Code == H)
{
salary=4000;
}
else
{
if(Hours_Worked <= 40)
salary= Hours_Worked*30;
else
salary= 8*30+( Hours_Worked-8)*40;
}

3. The Selection Control Structure- Switch


The CASE control structure in coding is another way of expressing a linear nested IF
statement.
General Syntax:

switch (condition)
{
case value :
// statement
break;

case value :
// statement
break;

default: // This is optional and works like else in if statement


//statement;
}

Question 3:

Day Number Day


1 Mon
2 Tue
3 Wed
4 Thu
5 Fri
switch (daynumber){
case 1 :{
System.out.println("Mon");
break;
}
case 2 :{
System.out.println("Tue");
break;
}
case 3 :{
System.out.println("Wed");
break;
}
case 4 :{
System.out.println("Thu");
break;
}

case 5 :{
System.out.println("Fri");
break;
}
}

More Exercises:

Exercise 1:
Write an application for a furniture company (using scanner); the program determines
the price of furniture. Program will prompt the user to choose the wood id (which is
representing the wood type); price and the wood type of user choice will be displayed.
Refer the following table for the price listing of wood. Save the file as furniture.java
Wood ID Wood Type Price
1 Pine RM 100
2 Oak RM 225
3 Mahogany RM 310
any other ID Invalid wood RM 0
Hint: use ifelse

Exercise 2:
Write an application for UNISEL admission office (using JOptionPane). Prompt user for a
students grade point average (GPA for example 3.2) and an admission test score from 0 to
100. Print the message ACCEPTS if the student has any of the following:
GPA of 3.0 or above and an admission test score at least 60
GPA of below 3.0 and an admission test score of at least 80
If the student does not meet either qualification criteria, print REJECT. Save the file as
Admission.java. Repeat the program as long as user wishes to continue to use the system.
Hint: use ifelse and looping

Вам также может понравиться