- •CONTENTS
- •1.1 Introduction
- •1.2 What Is a Computer?
- •1.3 Programs
- •1.4 Operating Systems
- •1.5 Java, World Wide Web, and Beyond
- •1.6 The Java Language Specification, API, JDK, and IDE
- •1.7 A Simple Java Program
- •1.8 Creating, Compiling, and Executing a Java Program
- •1.9 (GUI) Displaying Text in a Message Dialog Box
- •2.1 Introduction
- •2.2 Writing Simple Programs
- •2.3 Reading Input from the Console
- •2.4 Identifiers
- •2.5 Variables
- •2.7 Named Constants
- •2.8 Numeric Data Types and Operations
- •2.9 Problem: Displaying the Current Time
- •2.10 Shorthand Operators
- •2.11 Numeric Type Conversions
- •2.12 Problem: Computing Loan Payments
- •2.13 Character Data Type and Operations
- •2.14 Problem: Counting Monetary Units
- •2.15 The String Type
- •2.16 Programming Style and Documentation
- •2.17 Programming Errors
- •2.18 (GUI) Getting Input from Input Dialogs
- •3.1 Introduction
- •3.2 boolean Data Type
- •3.3 Problem: A Simple Math Learning Tool
- •3.4 if Statements
- •3.5 Problem: Guessing Birthdays
- •3.6 Two-Way if Statements
- •3.7 Nested if Statements
- •3.8 Common Errors in Selection Statements
- •3.9 Problem: An Improved Math Learning Tool
- •3.10 Problem: Computing Body Mass Index
- •3.11 Problem: Computing Taxes
- •3.12 Logical Operators
- •3.13 Problem: Determining Leap Year
- •3.14 Problem: Lottery
- •3.15 switch Statements
- •3.16 Conditional Expressions
- •3.17 Formatting Console Output
- •3.18 Operator Precedence and Associativity
- •3.19 (GUI) Confirmation Dialogs
- •4.1 Introduction
- •4.2 The while Loop
- •4.3 The do-while Loop
- •4.4 The for Loop
- •4.5 Which Loop to Use?
- •4.6 Nested Loops
- •4.7 Minimizing Numeric Errors
- •4.8 Case Studies
- •4.9 Keywords break and continue
- •4.10 (GUI) Controlling a Loop with a Confirmation Dialog
- •5.1 Introduction
- •5.2 Defining a Method
- •5.3 Calling a Method
- •5.4 void Method Example
- •5.5 Passing Parameters by Values
- •5.6 Modularizing Code
- •5.7 Problem: Converting Decimals to Hexadecimals
- •5.8 Overloading Methods
- •5.9 The Scope of Variables
- •5.10 The Math Class
- •5.11 Case Study: Generating Random Characters
- •5.12 Method Abstraction and Stepwise Refinement
- •6.1 Introduction
- •6.2 Array Basics
- •6.3 Problem: Lotto Numbers
- •6.4 Problem: Deck of Cards
- •6.5 Copying Arrays
- •6.6 Passing Arrays to Methods
- •6.7 Returning an Array from a Method
- •6.8 Variable-Length Argument Lists
- •6.9 Searching Arrays
- •6.10 Sorting Arrays
- •6.11 The Arrays Class
- •7.1 Introduction
- •7.2 Two-Dimensional Array Basics
- •7.3 Processing Two-Dimensional Arrays
- •7.4 Passing Two-Dimensional Arrays to Methods
- •7.5 Problem: Grading a Multiple-Choice Test
- •7.6 Problem: Finding a Closest Pair
- •7.7 Problem: Sudoku
- •7.8 Multidimensional Arrays
- •8.1 Introduction
- •8.2 Defining Classes for Objects
- •8.3 Example: Defining Classes and Creating Objects
- •8.4 Constructing Objects Using Constructors
- •8.5 Accessing Objects via Reference Variables
- •8.6 Using Classes from the Java Library
- •8.7 Static Variables, Constants, and Methods
- •8.8 Visibility Modifiers
- •8.9 Data Field Encapsulation
- •8.10 Passing Objects to Methods
- •8.11 Array of Objects
- •9.1 Introduction
- •9.2 The String Class
- •9.3 The Character Class
- •9.4 The StringBuilder/StringBuffer Class
- •9.5 Command-Line Arguments
- •9.6 The File Class
- •9.7 File Input and Output
- •9.8 (GUI) File Dialogs
- •10.1 Introduction
- •10.2 Immutable Objects and Classes
- •10.3 The Scope of Variables
- •10.4 The this Reference
- •10.5 Class Abstraction and Encapsulation
- •10.6 Object-Oriented Thinking
- •10.7 Object Composition
- •10.8 Designing the Course Class
- •10.9 Designing a Class for Stacks
- •10.10 Designing the GuessDate Class
- •10.11 Class Design Guidelines
- •11.1 Introduction
- •11.2 Superclasses and Subclasses
- •11.3 Using the super Keyword
- •11.4 Overriding Methods
- •11.5 Overriding vs. Overloading
- •11.6 The Object Class and Its toString() Method
- •11.7 Polymorphism
- •11.8 Dynamic Binding
- •11.9 Casting Objects and the instanceof Operator
- •11.11 The ArrayList Class
- •11.12 A Custom Stack Class
- •11.13 The protected Data and Methods
- •11.14 Preventing Extending and Overriding
- •12.1 Introduction
- •12.2 Swing vs. AWT
- •12.3 The Java GUI API
- •12.4 Frames
- •12.5 Layout Managers
- •12.6 Using Panels as Subcontainers
- •12.7 The Color Class
- •12.8 The Font Class
- •12.9 Common Features of Swing GUI Components
- •12.10 Image Icons
- •13.1 Introduction
- •13.2 Exception-Handling Overview
- •13.3 Exception-Handling Advantages
- •13.4 Exception Types
- •13.5 More on Exception Handling
- •13.6 The finally Clause
- •13.7 When to Use Exceptions
- •13.8 Rethrowing Exceptions
- •13.9 Chained Exceptions
- •13.10 Creating Custom Exception Classes
- •14.1 Introduction
- •14.2 Abstract Classes
- •14.3 Example: Calendar and GregorianCalendar
- •14.4 Interfaces
- •14.5 Example: The Comparable Interface
- •14.6 Example: The ActionListener Interface
- •14.7 Example: The Cloneable Interface
- •14.8 Interfaces vs. Abstract Classes
- •14.9 Processing Primitive Data Type Values as Objects
- •14.10 Sorting an Array of Objects
- •14.11 Automatic Conversion between Primitive Types and Wrapper Class Types
- •14.12 The BigInteger and BigDecimal Classes
- •14.13 Case Study: The Rational Class
- •15.1 Introduction
- •15.2 Graphical Coordinate Systems
- •15.3 The Graphics Class
- •15.4 Drawing Strings, Lines, Rectangles, and Ovals
- •15.5 Case Study: The FigurePanel Class
- •15.6 Drawing Arcs
- •15.7 Drawing Polygons and Polylines
- •15.8 Centering a String Using the FontMetrics Class
- •15.9 Case Study: The MessagePanel Class
- •15.10 Case Study: The StillClock Class
- •15.11 Displaying Images
- •15.12 Case Study: The ImageViewer Class
- •16.1 Introduction
- •16.2 Event and Event Source
- •16.3 Listeners, Registrations, and Handling Events
- •16.4 Inner Classes
- •16.5 Anonymous Class Listeners
- •16.6 Alternative Ways of Defining Listener Classes
- •16.7 Problem: Loan Calculator
- •16.8 Window Events
- •16.9 Listener Interface Adapters
- •16.10 Mouse Events
- •16.11 Key Events
- •16.12 Animation Using the Timer Class
- •17.1 Introduction
- •17.2 Buttons
- •17.3 Check Boxes
- •17.4 Radio Buttons
- •17.5 Labels
- •17.6 Text Fields
- •17.7 Text Areas
- •17.8 Combo Boxes
- •17.9 Lists
- •17.10 Scroll Bars
- •17.11 Sliders
- •17.12 Creating Multiple Windows
- •18.1 Introduction
- •18.2 Developing Applets
- •18.3 The HTML File and the <applet> Tag
- •18.4 Applet Security Restrictions
- •18.5 Enabling Applets to Run as Applications
- •18.6 Applet Life-Cycle Methods
- •18.7 Passing Strings to Applets
- •18.8 Case Study: Bouncing Ball
- •18.9 Case Study: TicTacToe
- •18.10 Locating Resources Using the URL Class
- •18.11 Playing Audio in Any Java Program
- •18.12 Case Study: Multimedia Animations
- •19.1 Introduction
- •19.2 How is I/O Handled in Java?
- •19.3 Text I/O vs. Binary I/O
- •19.4 Binary I/O Classes
- •19.5 Problem: Copying Files
- •19.6 Object I/O
- •19.7 Random-Access Files
- •20.1 Introduction
- •20.2 Problem: Computing Factorials
- •20.3 Problem: Computing Fibonacci Numbers
- •20.4 Problem Solving Using Recursion
- •20.5 Recursive Helper Methods
- •20.6 Problem: Finding the Directory Size
- •20.7 Problem: Towers of Hanoi
- •20.8 Problem: Fractals
- •20.9 Problem: Eight Queens
- •20.10 Recursion vs. Iteration
- •20.11 Tail Recursion
- •APPENDIXES
- •INDEX
5.6 Modularizing Code 165
Note
For simplicity, Java programmers often say passing an argument x to a parameter y, which actually means passing the value of x to y.
5.6 Modularizing Code
Methods can be used to reduce redundant code and enable code reuse. Methods can also be used to modularize code and improve the quality of the program.
Listing 4.8 gives a program that prompts the user to enter two integers and displays their greatest common divisor. You can rewrite the program using a method, as shown in Listing 5.6.
Video Note
Modularize code
LISTING 5.6 GreatestCommonDivisorMethod.java
1 import java.util.Scanner;
2
3 public class GreatestCommonDivisorMethod {
4/** Main method */
5 public static void main(String[] args) {
6// Create a Scanner
7 Scanner input = new Scanner(System.in);
8
9 // Prompt the user to enter two integers
10System.out.print("Enter first integer: ");
11int n1 = input.nextInt();
12System.out.print("Enter second integer: ");
13int n2 = input.nextInt();
14 |
|
|
|
|
|
15 |
|
System.out.println("The greatest common divisor for " + n1 + |
|
||
16 |
|
" and " + n2 + " is " + |
gcd(n1, n2) |
); |
invoke gcd |
17 |
} |
|
|
|
|
18 |
|
|
|
|
|
19 |
/** Return the gcd of two integers */ |
|
|||
20 |
|
public static int gcd(int n1, int n2) { |
|
compute gcd |
21int gcd = 1; // Initial gcd is 1
22int k = 2; // Possible gcd
23
24while (k <= n1 && k <= n2) {
25if (n1 % k == 0 && n2 % k == 0)
26gcd = k; // Update gcd
27k++;
28}
29 |
|
|
30 |
return gcd; // Return gcd |
return gcd |
31}
32}
Enter first integer: 45
Enter second integer: 75
The greatest common divisor for 45 and 75 is 15
By encapsulating the code for obtaining the gcd in a method, this program has several advantages:
1.It isolates the problem for computing the gcd from the rest of the code in the main method. Thus, the logic becomes clear and the program is easier to read.
166 Chapter 5 Methods
2.The errors on computing gcd are confined in the gcd method, which narrows the scope of debugging.
3.The gcd method now can be reused by other programs.
Listing 5.7 applies the concept of code modularization to improve Listing 4.14, PrimeNumber.java.
LISTING 5.7 PrimeNumberMethod.java
1 public class PrimeNumberMethod {
2public static void main(String[] args) {
3System.out.println("The first 50 prime numbers are \n");
invoke printPrimeNumbers |
4 |
|
|
printPrimeNumbers(50); |
|
|
|
|
|||||||||
|
|
|
|
|
5 |
} |
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
printPrimeNumbers |
7 |
|
public static void printPrimeNumbers(int numberOfPrimes) { |
||||||||||||||
method |
8 |
|
|
final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line |
|||||||||||||
|
|
|
|
|
9 |
|
|
int count = 0; // Count the number of prime numbers |
|||||||||
|
|
|
|
|
10 |
|
|
int number = 2; // A number to be tested for primeness |
|||||||||
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
|
// Repeatedly find prime numbers |
|
|
|||||||
|
|
|
|
|
13 |
|
|
while (count < numberOfPrimes) { |
|
|
|||||||
|
|
|
|
|
14 |
|
|
// Print the prime number and increase the count |
|||||||||
invoke isPrime |
15 |
|
|
if |
(isPrime(number) |
) { |
|
|
|
|
|||||||
|
|
|
|
|
16 |
|
|
count++; // Increase the count |
|
|
|||||||
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { |
|||||||||
|
|
|
|
|
19 |
|
|
|
// Print the number and advance to the new line |
||||||||
|
|
|
|
|
20 |
|
|
|
System.out.printf("%-5s\n", number); |
||||||||
|
|
|
|
|
21 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
else |
|
|
|
|
|
|
|
||
|
|
|
|
|
23 |
|
|
|
System.out.printf("%-5s", number); |
|
|||||||
|
|
|
|
|
24 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
|
// Check whether the next number is prime |
|||||||||
|
|
|
|
|
27 |
|
|
number++; |
|
|
|
|
|
|
|
||
|
|
|
|
|
28 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
} |
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
/** Check whether number is prime */ |
|
|
||||||||
isPrime method |
32 |
|
public static boolean isPrime(int number) { |
|
|||||||||||||
|
|
|
|
|
33 |
|
|
for (int divisor = 2; divisor <= number / 2; divisor++) { |
|||||||||
|
|
|
|
|
34 |
|
|
if (number % divisor == 0) { // If true, number is not prime |
|||||||||
|
|
|
|
|
35 |
|
|
return false; // number is not a prime |
|||||||||
|
|
|
|
|
36 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
|
return true; // number is prime |
|
|
|
||||||
|
|
|
|
|
40 |
} |
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
41 |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
The first 50 prime numbers are |
|
|
|
|
||||||
|
|
|
|
|
|
|
2 |
|
3 |
5 |
7 |
11 |
13 |
17 |
19 |
23 |
29 |
|
|
|
|
|
|
|
|
||||||||||
|
|
|
|
|
|
|
|
||||||||||
|
|
|
|
|
|
|
31 |
37 |
41 |
43 |
47 |
53 |
59 |
61 |
67 |
71 |
|
|
|
|
|
|
|
|
73 |
79 |
83 |
89 |
97 |
101 |
103 |
107 |
109 |
113 |
|
|
|
|
|
|
|
|
127 |
131 |
137 |
139 |
149 |
151 |
157 |
163 |
167 |
173 |
|
|
|
|
|
|
|
|
179 |
181 |
191 |
193 |
197 |
199 |
211 |
223 |
227 |
229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5.7 Problem: Converting Decimals to Hexadecimals 167
We divided a large problem into two subproblems. As a result, the new program is easier to read and easier to debug. Moreover, the methods printPrimeNumbers and isPrime can be reused by other programs.
5.7 Problem: Converting Decimals to Hexadecimals
Hexadecimals are often used in computer systems programming. Appendix F introduces number systems. This section presents a program that converts a decimal to a hexadecimal.
To convert a decimal number d to a hexadecimal number is to find the hexadecimal digits hn, hn- 1, hn- 2, Á , h2, h1, and h0 such that
d= hn * 16n + hn- 1 * 16n- 1 + hn- 2 * 16n- 2 + Á
+h2 * 162 + h1 * 161 + h0 * 160
These numbers can be found by successively dividing d by 16 until the quotient is 0. The
remainders are h0, h1, h2, Á , hn- 2, hn- 1, and hn.
For example, the decimal number 123 is 7B in hexadecimal. The conversion is done as follows:
0 |
7 |
|
Quotient |
|
16 7 16 123
0112
711 Remainder
h1 h0
Listing 5.8 gives a program that prompts the user to enter a decimal number and converts it into a hex number as a string.
LISTING 5.8 Decimal2HexConversion.java
1 import java.util.Scanner;
2
3 public class Decimal2HexConversion {
4/** Main method */
5 public static void main(String[] args) {
6// Create a Scanner
7 |
Scanner input = new Scanner(System.in); |
|
||
8 |
|
|
|
|
9 |
// Prompt the user to enter a decimal integer |
|
||
10 |
System.out.print("Enter a decimal number: "); |
|
||
11 |
int decimal = input.nextInt(); |
input string |
||
12 |
|
|
|
|
13 |
System.out.println("The hex number for decimal " + |
|
||
14 |
decimal + " is " + |
decimalToHex(decimal) |
); |
hex to decimal |
15 |
} |
|
|
|
16 |
|
|
|
|
17/** Convert a decimal to a hex as a string */
18public static String decimalToHex(int decimal) {
19String hex = "";
20
21while (decimal != 0) {
22int hexValue = decimal % 16;
23hex = toHexChar(hexValue) + hex;
24decimal = decimal / 16;
168 Chapter 5 |
|
Methods |
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
25 |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
hex char to decimal |
27 |
|
|
return hex; |
|
|
|
|
|
|
|
|
|
|
|
||||||
to uppercase |
28 |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
30 |
|
|
/** Convert an integer to a single hex digit in a character */ |
|
||||||||||||||||
|
31 |
|
|
public static char toHexChar(int hexValue) { |
|
|
|
|
|||||||||||||
|
32 |
|
|
if (hexValue <= 9 && hexValue >= 0) |
|
|
|
|
|
||||||||||||
|
33 |
|
|
return |
(char)(hexValue + '0') |
; |
|
|
|
|
|
||||||||||
|
34 |
|
|
else |
// hexValue <= 15 && hexValue >= 10 |
|
|
|
|||||||||||||
|
35 |
|
|
return |
(char)(hexValue - 10 + 'A') |
; |
|
|
|
|
|||||||||||
|
36 |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
37 |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Enter a decimal number: |
1234 |
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
The hex number for decimal 1234 is 4D2 |
|
|
|
|
|
||||||||||
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
line# |
decimal |
hex |
hexValue |
toHexChar(hexValue) |
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
1234 |
|
“” |
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
22 |
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
iteration 1 |
23 |
|
|
|
“2” |
|
|
2 |
|
|
||||
|
|
|
|
|
|
|
|
|
24 |
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
|
|
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
iteration 2 |
23 |
|
|
|
“D2” |
|
|
D |
|
|||||
|
|
|
|
|
|
|
|
|
24 |
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
|
|
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
iteration 3 |
23 |
|
|
|
“4D2” |
|
|
4 |
|
|
||||
|
|
|
|
|
|
|
|
|
24 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The program uses the decimalToHex method (lines 18–28) to convert a decimal integer to a hex number as a string. The method gets the remainder of the division of the decimal integer by 16 (line 22). The remainder is converted into a character by invoking the toHexChar method (line 23). The character is then appended to the hex string (line 23). The hex string is initially empty (line 19). Divide the decimal number by 16 to remove a hex digit from the number (line 24). The method repeatedly performs these operations in a loop until quotient becomes 0 (lines 21–25).
The toHexChar method (lines 31–36) converts a hexValue between 0 and 15 into a hex character. If hexValue is between 0 and 9, it is converted to (char)(hexValue + '0') (line 33). Recall when adding a character with an integer, the character’s Unicode is used in the evaluation. For example, if hexValue is 5, (char)(hexValue + '0') returns '5'. Similarly, if hexValue is between 10 and 15, it is converted to (char)(hexValue + 'A') (line 35). For example, if hexValue is 11, (char)(hexValue + 'A') returns 'B'.
5.8 Overloading Methods
The max method that was used earlier works only with the int data type. But what if you need to determine which of two floating-point numbers has the maximum value? The solution
5.8 Overloading Methods 169
is to create another method with the same name but different parameters, as shown in the following code:
public static double max(double num1, double num2) { if (num1 > num2)
return num1; else
return num2;
}
If you call max with int parameters, the max method that expects int parameters will be invoked; if you call max with double parameters, the max method that expects double para-
meters will be invoked. This is referred to as method overloading; that is, two methods have method overloading the same name but different parameter lists within one class. The Java compiler determines
which method is used based on the method signature.
Listing 5.9 is a program that creates three methods. The first finds the maximum integer, the second finds the maximum double, and the third finds the maximum among three double values. All three methods are named max.
LISTING 5.9 TestMethodOverloading.java
1 public class TestMethodOverloading {
2/** Main method */
3public static void main(String[] args) {
4// Invoke the max method with int parameters
5 System.out.println("The maximum between 3 and 4 is "
6 + max(3, 4) );
7
8// Invoke the max method with the double parameters
9 System.out.println("The maximum between 3.0 and 5.4 is "
10 + max(3.0, 5.4));
11
12// Invoke the max method with three double parameters
13System.out.println("The maximum between 3.0, 5.4, and 10.14 is "
14+ max(3.0, 5.4, 10.14) );
15}
16 |
|
|
|
|
17 |
/** Return the max between two int values */ |
|
||
18 |
|
public static int max(int num1, int num2) |
{ |
overloaded max |
19if (num1 > num2)
20return num1;
21else
22return num2;
23}
24 |
|
|
|
|
25 |
/** Find the max between two double values */ |
|
||
26 |
|
public static double max(double num1, double num2) |
{ |
overloaded max |
27if (num1 > num2)
28return num1;
29else
30return num2;
31}
32 |
|
|
|
|
|
|
33 |
/** Return the max among three double |
values |
*/ |
|
|
|
34 |
|
public static double max(double num1, |
double |
num2, double num3) |
{ |
overloaded max |
35return max(max(num1, num2), num3);
36}
37}
170 Chapter 5 Methods
The maximum between 3 and 4 is 4
The maximum between 3.0 and 5.4 is 5.4
The maximum between 3.0, 5.4, and 10.14 is 10.14
When calling max(3, 4) (line 6), the max method for finding the maximum of two integers is invoked. When calling max(3.0, 5.4) (line 10), the max method for finding the maximum of two doubles is invoked. When calling max(3.0, 5.4, 10.14) (line 14), the max method for finding the maximum of three double values is invoked.
Can you invoke the max method with an int value and a double value, such as max(2, 2.5)? If so, which of the max methods is invoked? The answer to the first question is yes. The answer to the second is that the max method for finding the maximum of two double values is invoked. The argument value 2 is automatically converted into a double value and passed to this method.
You may be wondering why the method max(double, double) is not invoked for the call max(3, 4). Both max(double, double) and max(int, int) are possible matches for max(3, 4). The Java compiler finds the most specific method for a method invocation. Since the method max(int, int) is more specific than max(double, double), max(int, int) is used to invoke max(3, 4).
Note
Overloaded methods must have different parameter lists. You cannot overload methods based on different modifiers or return types.
Note
Sometimes there are two or more possible matches for an invocation of a method, but the com- ambiguous invocation piler cannot determine the most specific match. This is referred to as ambiguous invocation.
Ambiguous invocation causes a compile error. Consider the following code:
public class AmbiguousOverloading { public static void main(String[] args) {
System.out.println(max(1, 2) );
}
public static double max(int num1, double num2) { if (num1 > num2)
return num1; else
return num2;
}
public static double max(double num1, int num2) { if (num1 > num2)
return num1; else
return num2;
}
}
Both max(int, double) and max(double, int) are possible candidates to match max(1, 2). Since neither is more specific than the other, the invocation is ambiguous, resulting in a compile error.