6.3 Writing Methods

It is common sense to take a method and try it. If it fails, admit it frankly and try another. But above all, try something.

—FRANKLIN DELANO ROOSEVELT, Address at Oglethorpe University (MAY 22, 1932)

In this section we discuss some basic techniques that will help you to design, code, and test methods. We start with a case study.

Case Study Formatting Output

If you have a variable of type double that stores some amount of money, your programs should display the amount in a nice format. However, you are likely to get output that looks like this:

Your cost, including tax, is $19.98123576432

Instead, we would like the output to look like this:

Your cost, including tax, is $19.98

In this case study, we will define a class called ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.