Java Lesson 4: Methods

Java Lesson 4: Methods featured illustration

πŸ“Ž This article includes 1 downloadable practice file ↓

⏱ 1 min read

πŸ“˜ Java Beginner Course Β· Lesson 4 of 8

Advertisement
static double gst(double amount, double rate) {
    return Math.round(amount * rate * 100) / 100.0;
}
static double gst(double amount) { return gst(amount, 0.18); }   // overload with a default rate

The return type comes before the name (double); void returns nothing. Methods with the same name but different parameters are overloads.

πŸ’‘ A method should do one thing; if its name needs ‘and’, split it.

Code for this course follows standard, current syntax; we test JavaScript and PowerShell scripts before publishing, and R/Java/Rust examples are kept to core language features. Tell us if anything fails on your setup.

Practice

Add an overloaded gst() method to Basics.java.

Advertisement

πŸ“Ž Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong