CSBP221

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Programming Lab II (CSBP221)

Fall 23

Lab 1

Objective and Overview:


 To read and write from text files.
 To use selection statements.
 Exception handeling.
CLO: This lab maps to CLO #1
Instructions:
 The first set of exercises is to be solved together in the lab with the instructor.
 The second set of exercises is to be solved by students. Students may work in groups of 2.
 All lab exercises must be submitted by the end of each lab (one submission per group).

SET: 1

Exercise #1: Write a java program that calculates the squares and cubes of the numbers from
1 to 5 and prints the resulting values in a table format, as shown below.

Exercise #2: Write a program which calculates and prints the monthly paycheck for an
employee. The net salary is calculated after the following deductions and additions:

Deducations: Additions:
Retirement Plan: 5% Housing Allowence: 20%
Health Insurance: 750 AED Transportation Allowence: 200

Your program should prompt the user to input the gross amount. The output should be like this:

Gross Amount: 10000.00


**************************************
Retirement Plan: - 500.00 AED
Health Insurance: - 750.00 AED
Housing Allowence: + 2000.00 AED
Transportation Allowence: + 200.00 AED
1/4
**************************************
Net Salary: 10950.00 AED

Exercise #3: Redo exercise #1 but read the input (Gross Amount) from a file and write the
output to a file.

Exercise#4: The sum of the interior angles of a triangle is 180 degrees. Write a program that
reads two interior angles of a triangle in degrees, and then calculates the third angle and print its
value. The program should then print a message telling whether the triangle is:
 Equilateral (all angles equal).
 Isoceles (exactly two angles equal)
 Scalene (no two angles equal).
Sample input-output (2):
Sample input-output (1):
Enter the value of two angles: 60 60 Enter the value of two angles: 60 50
The value of the third angle is: 60 The value of the third angle is: 70
The triangle is equilateral The triangle is scalene

:Exercise #5: Write a program which performs the following (Using Switch statement)
a) Prompts the user to enter a number (a temperature).
b) Prompts the user to enter a character (one of the following: F, or K) which
indicates the user’s choice of conversion.
•If the user’s choice is F, your program converts the temperature from Fahrenheit to
Celsius.
•If the user’s choice is K, your program converts the temperature from Kelvin to
Celsius.
c) Displays the result of the conversion. The conversion formulas are given below:
 Celsius = (Fahrenheit - 32) * 5 / 9
 Celsius = Kelvin - 273.15

2/4
SET: 2

Exercise #1: Write an algorithm to calculate and display a person’s BMI (Body Mass Index).

Weight (kg)
BMI =
Height ( m ) x Height ( m )

Exercise #2: Write a Java program that reads a month and a year and find how many days are
in a given month. Check if the year is a leap year. The following will help in defining the
solution:

 Every year divisible by 4 is a leap year with 29 days in February.(in the case the year is
divisible by 100 it has to be divisible by 400 as well to be a leap year) (use if-else)
 Use number from 1 to 12 (i.e., 1: January, 2: February, 3: March, etc) to indicate a
given month. (Use switch)
 Use 4 digits to indicate a year (i.e., 2005, 1996).

Here are two samples of input/output:

Sample input-output (1): Sample input-output (2):


Enter a Month: 9 Enter a Month: 2
Enter a Year:: 2005 Enter a Year: 2008
2005 is not a leap year and September has 30 days 2008 is a leap year and February has 29 days

Exercise#3: (DO IT BY HAND) What is the output of the following program for:
a) option = 3
b) option = 1, amount = 700
c) option = 2, amount = 850

package lab2;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner console= new Scanner(System.in);
int option;
double amount, balance = 23500.0;
System.out.println("1: Withdrawal\n2: Deposit\n3: Check balance ");
System.out.print( "Please enter you selection (1, 2, or 3): ");
option=console.nextInt();
if (option == 1) {
System.out.println( "Enter amount to withdraw: ");
amount=console.nextDouble();
balance -= amount;
System.out.println( "Your balance is: " + balance ); }
else if (option == 2) {
System.out.println( "Enter amount to deposit: ");
amount=console.nextDouble();
balance += amount;
System.out.println( "Your balance is: " + balance ); }
else if (option == 3)
System.out.println( "Your balance is: " + balance );
3/4
else
System.out.println("Remove card " );
}
}

Exercise#4: Modify the code in exercise#7 to use the switch statement.

4/4

You might also like