Week 4 Lab Tutorial (Step-by-Step)
Course: BIC1224 – Object-Oriented Programming (Java)
Topic: Methods — defining, calling, parameters, return values, overloading, input validation,
modular problem solving
Learning Outcomes (Week 4 Lab)
By the end of this lab, students will be able to:
● Create and call methods in Java
● Use parameters and return values correctly
● Apply method overloading
● Organize a program into reusable functions (modular design)
● Build a menu-driven program using methods
● Write clean, readable code using best practices
Part A — Setup & Starter Template
Step 1: Create a New Project
1. Open your IDE.
2. Create a new Java project:
○ Project Name: BIC1224_Week4
○ Main Class: Main
Step 2: Use This Starter Template
import [Link];
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
// Call methods from here
// Example: greet();
[Link]();
}
// Define methods below
}
Part B — Method Basics (Step-by-Step
Tasks)
Task 1: Create a Simple Void Method
Step 3: Write a greet() Method
1. Create a method named greet() that prints a welcome message.
2. Call it from main().
public static void greet() {
[Link]("Welcome to Week 4: Methods in Java!");
}
Call it:
greet();
✅ Practice: method definition + method call
Task 2: Method with Parameters
Step 4: Create printStudentInfo(name, id)
1. Take name and id from user input.
2. Pass them to a method and print neatly.
public static void printStudentInfo(String name, String id) {
[Link]("----- Student Info -----");
[Link]("Name: " + name);
[Link]("ID: " + id);
}
Call it:
[Link](); // ensure clean input
[Link]("Enter name: ");
String name = [Link]();
[Link]("Enter ID: ");
String id = [Link]();
printStudentInfo(name, id);
✅ Practice: passing parameters
Task 3: Method with Return Value
Step 5: Create add(a, b) That Returns an int
public static int add(int a, int b) {
return a + b;
}
Call it:
[Link]("Enter a: ");
int a = [Link]();
[Link]("Enter b: ");
int b = [Link]();
int result = add(a, b);
[Link]("Sum = " + result);
✅ Practice: returning values
Part C — Real Utility Methods
Task 4: Build a maxOfThree(a, b, c) Method
Step 6:
public static int maxOfThree(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
Test it:
[Link]("Enter three numbers: ");
int x = [Link]();
int y = [Link]();
int z = [Link]();
[Link]("Max = " + maxOfThree(x, y, z));
Task 5: Build a isPrime(n) Method
Step 7:
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
Test:
[Link]("Enter number: ");
int p = [Link]();
[Link](isPrime(p) ? "Prime" : "Not Prime");
✅ Practice: returning boolean
Part D — Method Overloading
Task 6: Overload an area() Method
Step 8: Create 3 versions
public static double area(double radius) {
return 3.1416 * radius * radius;
}
public static int area(int length, int width) {
return length * width;
}
public static double area(double base, double height, boolean triangle) {
return 0.5 * base * height;
}
Test:
[Link]("Circle area = " + area(5.0));
[Link]("Rectangle area = " + area(4, 6));
[Link]("Triangle area = " + area(10.0, 6.0, true));
✅ Practice: same method name, different parameters
Part E — Modular Menu Program Using
Methods
Task 7: Build a Menu and Call Methods
Step 9: Create Methods First
public static int subtract(int a, int b) { return a - b; }
public static int multiply(int a, int b) { return a * b; }
public static double divide(int a, int b) {
if (b == 0) return [Link];
return (double) a / b;
}
Step 10: Build Menu Loop in main()
while (true) {
[Link]("\n--- Calculator Menu ---");
[Link]("1. Add");
[Link]("2. Subtract");
[Link]("3. Multiply");
[Link]("4. Divide");
[Link]("5. Exit");
[Link]("Choose: ");
int choice = [Link]();
if (choice == 5) break;
[Link]("Enter first number: ");
int n1 = [Link]();
[Link]("Enter second number: ");
int n2 = [Link]();
switch (choice) {
case 1 -> [Link]("Result = " + add(n1, n2));
case 2 -> [Link]("Result = " + subtract(n1, n2));
case 3 -> [Link]("Result = " + multiply(n1, n2));
case 4 -> {
double ans = divide(n1, n2);
if ([Link](ans)) [Link]("Error: divide by zero");
else [Link]("Result = " + ans);
}
default -> [Link]("Invalid choice!");
}
}
✅ Practice: modular design + method reuse + menu loop
Exercises (Week 4)
Exercise 1 — GCD and LCM Methods
Create:
● gcd(int a, int b)
● lcm(int a, int b) using:
lcm = (a*b)/gcd
Exercise 2 — Palindrome Checker Method
Create:
● isPalindrome(String s)
Return true/false.
Hint:
● Compare original string with reversed.
Exercise 3 — Grade Calculation Method
Create:
● calculateGrade(int marks)
Return grade as String.
Rules:
● 80–100 A
● 70–79 B
● 60–69 C
● 50–59 D
● else F
Also validate marks range.
Exercise 4 — Array Sum Method (Preview for Week 5)
Create:
● sumArray(int[] arr)
Return sum.
Test with an array of 5 user inputs.
Exercise 5 (Challenge) — Simple Banking Menu Using
Methods
Balance starts at 1000.
Create methods:
● deposit(balance, amount)
● withdraw(balance, amount)
● checkBalance(balance)
Menu:
1. Deposit
2. Withdraw
3. Balance
4. Exit