📖 Scenario: Imagine you are organizing a small library. You need to keep track of books and perform actions like adding new books and showing all books. Doing these tasks repeatedly in the main program can get messy and confusing.
🎯 Goal: You will create a simple Java program that uses methods to organize tasks like adding books and displaying the list. This will show why methods are helpful to keep code clean and reusable.
📋 What You'll Learn
Create a method to add a book to the list
Create a method to display all books
Use methods in the main program to manage books
💡 Why This Matters
🌍 Real World
In real software, methods help break down big tasks into smaller steps, making code easier to manage and reuse.
💼 Career
Understanding methods is essential for any programming job because they are the building blocks of clean, efficient code.
Progress0 / 4 steps
1
Create the initial book list
Create an ArrayList of String called books and add these exact books: "Java Basics", "Data Structures", "Algorithms".
Java
Hint
Use ArrayList<String> and the add method to add books.
2
Create a method to add a book
Add a static method called addBook that takes an ArrayList<String> books and a String book as parameters and adds the book to the list.
Java
Hint
Define a method with public static void addBook(ArrayList books, String book) and use books.add(book); inside.
3
Create a method to display all books
Add a static method called displayBooks that takes an ArrayList<String> books and prints each book on a new line using a for loop.
Java
Hint
Use a for-each loop to print each book inside the method.
4
Use methods to add and display books
In the main method, use addBook(books, "Design Patterns") to add a new book, then call displayBooks(books) to print all books.
Java
Hint
Call addBook(books, "Design Patterns") and then displayBooks(books) inside main.
Practice
(1/5)
1. Why do we use methods in Java programs?
easy
A. To organize code into reusable blocks
B. To make the program run faster
C. To avoid using variables
D. To write code without errors
Solution
Step 1: Understand the purpose of methods
Methods group code that performs a specific task so it can be reused easily.
Step 2: Compare options with method benefits
Only organizing code into reusable blocks matches the main reason for methods.
Final Answer:
To organize code into reusable blocks -> Option A
Quick Check:
Methods = reusable code blocks [OK]
Hint: Methods help reuse code easily [OK]
Common Mistakes:
Thinking methods make code run faster
Believing methods remove the need for variables
Assuming methods fix all errors automatically
2. Which of the following is the correct way to declare a method in Java?
easy
A. greet void() { System.out.println("Hello"); }
B. void greet() System.out.println("Hello");
C. void greet[] { System.out.println("Hello"); }
D. void greet() { System.out.println("Hello"); }
Solution
Step 1: Check method declaration syntax
In Java, a method declaration starts with return type, method name, parentheses, and braces.
Step 2: Identify the correct syntax
void greet() { System.out.println("Hello"); } follows this pattern correctly: 'void greet() { ... }'. Others have syntax errors.
Final Answer:
void greet() { System.out.println("Hello"); } -> Option D
Quick Check:
Method syntax = returnType name() { } [OK]
Hint: Method syntax: returnType name() { } [OK]
Common Mistakes:
Placing return type after method name
Using brackets [] instead of parentheses ()
Omitting curly braces {}
3. What will be the output of this Java code?
public class Test {
static void sayHi() {
System.out.println("Hi there!");
}
public static void main(String[] args) {
sayHi();
sayHi();
}
}
medium
A. !ereht iH
!ereht iH
B. Hi there!
Hi there!
C. Hi there! Hi there!
D. Hi there!
Hi there!
Hi there!
Solution
Step 1: Understand method calls in main
The method sayHi() prints "Hi there!" once per call. It is called twice.
Step 2: Determine output format
Each call prints "Hi there!" followed by a new line. Two calls print two lines.
Final Answer:
Hi there!
Hi there!
-> Option B
Quick Check:
Two calls print two lines [OK]
Hint: Each println adds a new line [OK]
Common Mistakes:
Ignoring new lines after println
Thinking output is on one line
Confusing escape sequences with actual output
4. Find the error in this method declaration:
public void printMessage {
System.out.println("Hello");
}
medium
A. Missing parentheses after method name
B. Missing return type
C. Missing semicolon after println
D. Method name is invalid
Solution
Step 1: Check method declaration syntax
Methods must have parentheses after the name, even if no parameters are used.
Step 2: Identify the missing part
The code misses parentheses after printMessage, causing a syntax error.
Final Answer:
Missing parentheses after method name -> Option A
Quick Check:
Method() needs parentheses [OK]
Hint: Always add () after method name [OK]
Common Mistakes:
Forgetting parentheses in method declaration
Thinking semicolon needed after method header
Assuming method name can have spaces
5. You want to write a Java program that calculates the area of a rectangle multiple times with different sizes. Why is it better to use a method for this task?
hard
A. Because methods automatically print the result without extra code
B. Because methods make the program run faster by skipping calculations
C. Because methods allow you to write the area calculation once and reuse it with different inputs
D. Because methods prevent the program from crashing
Solution
Step 1: Understand the problem of repeated calculations
Calculating area multiple times means repeating the same code with different values.
Step 2: See how methods help
Methods let you write the calculation once and call it with different inputs, avoiding repetition.
Final Answer:
Because methods allow you to write the area calculation once and reuse it with different inputs -> Option C
Quick Check:
Methods = reuse code with different inputs [OK]
Hint: Use methods to avoid repeating code [OK]
Common Mistakes:
Thinking methods speed up calculations automatically
Believing methods print results without code
Assuming methods fix runtime errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.