Lab Sheet 5 - Methods
Lab Sheet 5 - Methods
Lab sheet 5
Methods
PART A
1. Write a static method that calculates the square number of an integer and displays the
answer.
2. Write a static method that takes a Celsius value as a parameter. Then calculate and display
the Fahrenheit value of the Celsius value.
3. Write a static method that takes weight (kg) and height (m) as parameters and returns BMI
value.
4. Write non-static methods for question 1, 2 and 3.
5. Design a Java program for a simple bank account system. The class should have methods to
perform the following operations:
• Deposit:
o Create a method that allows a user to deposit a specified amount into the account.
o The method should update the account balance accordingly.
• Withdraw:
o Implement a method that enables a user to withdraw a specified amount from the
account.
o Ensure that the withdrawal does not exceed the available balance.
• Check Balance:
o Develop a method to print the current balance of the account.
6. Create a menu-driven Java program that allows the user to perform various geometric calculations.
Include the following options:
a. Calculate Area of a Circle:
a. Prompt the user for the radius, calculate and display the calculated area.
b. Calculate Area of a Rectangle:
a. Prompt the user for the length and width of a rectangle, calculate and display the
calculated area.
c. Calculate Area of a Triangle:
a. Take the base and height of a triangle as input, calculate and display the calculated area.
d. Exit:
o Allow the user to exit the program.
Implement a function for option a, b and c. Also, a function for displaying the menu to the user. Using the
functions implemented complete the program.
PART B
1. Write non-static methods to do the following tasks. According to the task explained
identify return type, passing parameters of the methods.
a. To compute and display an average of three numbers that are passed to the
method.
b. To compute and return the annual interest amount when annual interest rate is
4.3% and deposit amount is Rs. 500,000.00.
c. To display the item name, quantity, unit price and total price when they are passed
to the method.
d. To print the relevant weight status when the BMI value is passed to the method.
Refer the table below for the BMI value ranges and the corresponding weight
status.