O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4
Methods and Formatted Output
4.1 Introduction to Methods ................................................................ 61
4.2 Formatting Output ..................................................................... 64
4.3 Co de Refactoring ....................................................................... 66
4.4 Do cumenting Methods Using JavaDoc ................................................. 68
4.5 Sending Data between Methods ........................................................ 69
4.6 The Trading Game ..................................................................... 72
4.7 Summary ................................................................................ 78
4.8 Syntax .................................................................................. 78
4.9 Important Points ....................................................................... 78
4.10 Exercises ................................................................................ 79
4.11 Lab ...................................................................................... 80
4.12 Project .................................................................................. 80
So far, we have written all the code in the main method. Although this is fine for small
programs, it is impractical for larger ones. Consider a program that consists of ten thousand
lines of code. Scrolling through all the lines to find an error is not a viable option. To avoid
this scenario, Java allows us to modularize our code into methods. Good software practices
call for every method to perform a single task. For example, a method that reads an integer
from a file and multiplies it be a constant will not be very reusable. The chance that one
will require these two operations in the same order again is very low. However, a method
that reads an integer from a file has a good chance of being reused. Similarly, a method
that multiplies an integer by a constant also has a high probability of being reused. As a
rule of thumb, a method should not be very complicated and it should rarely take more
than 50 lines of code. If a method is longer than that, we should consider breaking it down
into smaller methods.
Formatted printing allows us to print the result in any format that we want. For example,
we can justify the output to the right or to the left. We can also specify the number of digits
after the decimal dot that will be displayed for doubles.
4.1 Introduction to Methods
You can think of a method as a “friend” that does you a favor. During the execution
of a program, you can ask your friend to do something for you. When you do that, you
can send your friend some information. Your friend will perform the required task and may
return some result. After calling a method, the program usually waits for the method to do
its job before continuing. For example, suppose you have two friends called Bob and Peter.
Bob is a good painter and Peter is an excellent carpenter. If you want a room remodeled,
then you can call Bob to paint the walls. You will call Peter to put in the hardwood floor
only after Bob has finished painting. Similar to your friends, methods also have names. Our
fictitious example can be implemented as follows.
61

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required