Lab Sheet 8: Method Declaration and Parameter Passing


Lab Practical 1: Basic Method Creation
Objective: Create and call a simple method that prints a message.
public class MethodPractice {
// Task 1: Create a method called printMessage that prints "I want to draw a circle."
// Your code here
public static void main(String[] args) {
// Task 2: Call the printMessage method from main
// Your code here
}
}
Expected Output:
I want to draw a circle.
Lab Practical 2: Method with Parameters and Return Value
Objective: Create a method that takes parameters and returns a value.
public class Calculator {
// Task 1: Create a method called addNumbers that takes two integers and returns their sum
// Your code here
public static void main(String[] args) {
// Task 2: Call the addNumbers method with values 5 and 7, then print the result
// Your code here
// Task 3: Call the addNumbers method with values 10 and 15, then print the result
// Your code here
}
}
Expected Output:
12
25
Lab Practical 3: Access Specifiers Practice
Objective: Demonstrate different access specifiers.
public class AccessDemo {
// Task 1: Create a public method called publicMethod that prints "This is public"
// Your code here
// Task 2: Create a private method called privateMethod that prints "This is private"
// Your code here
// Task 3: Create a protected method called protectedMethod that prints "This is protected"
// Your code here
// Task 4: Create a default access method called defaultMethod that prints "This is default"
// Your code here
public static void main(String[] args) {
AccessDemo demo = new AccessDemo();
// Call all methods that are accessible from here
// Your code here
}
}
Lab Practical 4: Static vs Instance Methods
Objective: Demonstrate the difference between static and instance methods.
public class MethodTypes {
// Task 1: Create a static method called staticGreeting that prints "Hello from static method"
// Your code here
// Task 2: Create an instance method called instanceGreeting that prints "Hello from instance method"
// Your code here
public static void main(String[] args) {
// Task 3: Call the static method directly
// Your code here
// Task 4: Create an object of MethodTypes and call the instance method
// Your code here
}
}
Expected Output:
Hello from static method
Hello from instance method
Lab Practical 5: Using Predefined Methods
Objective: Practice using Java's predefined methods.
public class PredefinedMethods {
public static void main(String[] args) {
String message = "Java Programming";
// Task 1: Use the length() method and print the length of the message
// Your code here
// Task 2: Use the toUpperCase() method and print the message in uppercase
// Your code here
// Task 3: Use the Math.sqrt() method to calculate and print the square root of 25
// Your code here
// Task 4: Use the Math.random() method to generate and print a random number
// Your code here
}
}
Lab Practical 6: Comprehensive Method Practice
Objective: Combine all concepts into a more complex program.
public class CircleOperations {
// Task 1: Create a private static constant PI with value 3.14159
// Your code here
// Task 2: Create a public static method calculateArea that takes radius as parameter and returns area
// Your code here
// Task 3: Create a public instance method calculateCircumference that takes radius and returns circumference
// Your code here
// Task 4: Create a private method displayResult that takes a description and value and prints them formatted
// Your code here
public static void main(String[] args) {
double radius = 5.0;
// Task 5: Call the static method to calculate area and print result
// Your code here
// Task 6: Create instance and call instance method to calculate circumference
// Your code here
// Bonus: Try to call the private displayResult method (what happens?)
// Your code here
}
}
Expected Output (approximate):
Area: 78.53975
Circumference: 31.4159
Subscribe to my newsletter
Read articles from Arzath Areeff directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Arzath Areeff
Arzath Areeff
I co-founded digizen.lk to promote online safety and critical thinking. Currently, I’m developing an AI app to fight misinformation. As Founder and CEO of ideaGeek.net, I help turn startup dreams into reality, and I share tech insights and travel stories on my YouTube channels, TechNomad and Rz Omar.