0% found this document useful (0 votes)
8 views6 pages

CS110T Lab6 Student 1446 Wed

The document outlines a lab exercise for CS110T focusing on control statements in programming, specifically using if, nested if, and switch statements. It includes exercises for understanding program outputs, converting switch statements to if-else structures, and writing Java programs based on user input for various scenarios. Additionally, it presents assignment problems related to a simple calculator and a phone banking system, both requiring the use of switch statements.

Uploaded by

a4lla2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

CS110T Lab6 Student 1446 Wed

The document outlines a lab exercise for CS110T focusing on control statements in programming, specifically using if, nested if, and switch statements. It includes exercises for understanding program outputs, converting switch statements to if-else structures, and writing Java programs based on user input for various scenarios. Additionally, it presents assignment problems related to a simple calculator and a phone banking system, both requiring the use of switch statements.

Uploaded by

a4lla2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS110T: Programming Language1

Lab 6: Control Statements

Lab Objectives:
In this lab, the student will practice:
✔ Reading data values from the user.
✔ Using if and nested if statements
✔ Using switch.

1st Semester 2024-2025


Lab Exercise 1: Program Output
1) What is the output of the following code? What will be the output if num is 1? What if it is 6?

public class SimpleSwitch {


public static void main(String[] args) {
int num = 2;
switch (num) {
case 1: System.out.println("One"); break;
case 2: System.out.println("Two"); break;
case 3: System.out.println("Three"); break;
default: System.out.println("Invalid number"); }}}

2) What is the output of the following code? Explain

public class Numbers {


public static void main(String[] args) {
int num = 2;
switch (num) {
case 1: System.out.println("One");
case 2: System.out.println("Two");
case 3: System.out.println("Three"); break;
default: System.out.println("Invalid number");
} } }

3) What is the output of the following code? Explain

public class CombinedCases {


public static void main(String[] args) {
int day = 5;
switch (day) {
case 1:
case 7:
System.out.println("Weekend");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
System.out.println("Weekday");
break;
default:
System.out.println("Invalid day");
}
}
}

2
Lab Exercise 2: Code Writing (1)
Problem Description: Convert the following switch statement into an if-else structure
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
default:
System.out.println("Needs improvement");
}

Lab Exercise 3: Code writing (2)


a) Problem description: Write a Java program that takes a student's academic standing as
input and prints the corresponding year:
• "freshman": "First year"
• "sophomore": "Second year"
• "junior": "Third year"
• "senior": "Fourth year"
• If the user enters an invalid choice, print "Invalid choice”.

You should do the following:


- Write a program that uses a switch statement.

Output sample :
Enter the academic standing: freshman
First year

b) Problem description: Write a Java program that takes a BMI category as input and
prints the corresponding BMI range:
• "underweight": "BMI less than 18.5"
• "normal": "BMI between 18.5 and 24.9"
• "overweight": "BMI between 25 and 29.9"
• "obese": "BMI 30 or above"
• If the user enters an invalid choice, print "Invalid choice"

3
You should do the following:
- Write a program that uses a switch statement.

Output sample :
Enter the BMI category: normal
BMI between 18.5 and 24.9

c) Problem description: Write a Java program that takes a weather condition as input and
prints a suggestion based on the weather:
• "sunny": "Wear sunglasses"
• "cloudy": "Take an umbrella just in case"
• "rainy": "Wear a raincoat"
• "snowy": "Wear a heavy coat"
• If the user enters an invalid choice, print "Invalid choice"

You should do the following:


- Write a program that uses a switch statement.

Output sample :
Enter the weather condition: sunny
Wear sunglasses

4
LAB6 Assignment Problems
Problem 1 Description:
Elementary students would like your help to solve their simple mathematical operations.
They asked you to create a simple calculator which can solve ( + , - , * , / ) .
Write a java program that gets two integers from the user and the operation they would like
to use, then display the result according to the operation they entered. (Using switch)
A sample Output
Enter the first number:
15
Enter the second number:
3
Enter the arithmetic operation:
/
The result is 5

Problem 2 Description: Phone Banking System


A phone banking system has four different types of options. Each option has its own requirements from the
caller:
The list of options:
1: Inquiring about account type
• The system should prompt the caller to enter his account number.
• If the account number is an odd number, the account is “Checking” account; otherwise it is a “Savings”
account.
2: Inquiring about bank’s working hours
• The system should ask the user if he is inquiring about “weekdays” by entering D or “weekends” by
entering E.
• If ‘D’, then “Bank working hours during weekdays are from 9 AM to 5PM”
• If ‘E’, then “Bank working hours during weekends are from 10 AM to 2PM”
3: Withdraw
• The system should ask for amount of money to be withdrawn.
• If the callers account balance after deduction is less than 0, the caller should be informed that “No
available funds to perform the transaction” and the transaction should be aborted; otherwise the
system should proceed with the withdraw and show available funds after deduction.

5
4: Deposit
• The system should ask for amount of money to be deposit. Assume the amount in Integer.
• The system adds the amount to the caller’s account balance and displays the available funds after
deposit.

Your program should use a switch statement to display the menu (4 different options). You should also
have a variable of type Integer to store the callers account balance. You can assume the caller has (SAR
12000).

You might also like