The program in Figure A-16 computes the circumference and radius of a circle. Two of the methods make use of the mathematical constant p. This program works, but it is bad style for an unidentified magic number like this to appear in several places in the code. If we have to type it several times, we might make a mistake. If we later want to change the value (for example, to specify more digits of precision), we have to find every occurrence of the number in the file. Finally, it might not be obvious to someone reading the code what this number means.

Figure A-16. The number 3.14159 appears in several places in the code. It is best to replace such a magic number with a constant.

1 /** Compute the circumference and area of a circle. */ 2 public class Circle { 3 4 /** Return the area of a circle with the specified radius. */ 5 public static double area(double radius) { 6 return 3.14159 * radius * radius; 7 } 89 /** Return the circumference of a circle with the specified radius. */ 10 public static double circumference(double radius) { 11 return 3.14159 * 2 * radius; 12 } 13 14 /** 15 * Read the radius from the user and print the circumference and 16 * area. 17 */ 18 public static void main(String[] args) { 19 java.util.Scanner input = new java.util.Scanner(System.in); 20 System.out.print("Enter the radius of the circle: "); 21 double radius = input.nextDouble(); 22 System.out.println("Circumference: " + circumference(radius)); 23 System.out.println("Area: " + area(radius)); 24 } 25 26 } |

A better solution is to declare a constant, a variable which cannot change. In Java, this is done with the keyword `final`:

final double pi = 3.14159;

Where should we put this line? If we put it in `circumference()`, it will not be visible in `area()`, and vice versa. If we put it in `main()`, neither of the other methods will be able to see it.

The solution is to put it in the class, but not inside any particular method. Like a method, we declare it public and static (Figure A-17). A variable like this is called a class field, static field, or class variable. This is different from an instance field, which is not declared static and will be discussed in Chapter 1.

Just as we can invoke a method from another class, we can refer to a class field from another class. Two particularly useful constants appear in the built-in Math class: `Math.PI` (roughly 3.14159, the ratio of the circumference to the diameter of a circle) and `Math.E` (roughly 2.71828, the base of the natural logarithms).

We can now understand `System.out.println()` slightly more precisely. `System` is a class. There is a constant `out` within that class. The method `println()` is invoked on the object `System.out`. This is a nonstatic method, as will be explained in Section 1.2.

Figure A-17. The magic number has been replaced with a constant.

1 /** Compute the circumference and area of a circle. */ 2 public class Circle { 3 4 /** Ratio of the circumference to the diameter of a circle. */ 5 public static final double PI = 3.14159; 6 7 /** Return the area of a circle with the specified radius. */ 8 public static double area(double radius) { 9 return PI * radius * radius; 10 } 11 12 /** 13 * Return the circumference of a circle with the specified 14 * radius. 15 */ 16 public static double circumference(double radius) { 17 return PI * 2 * radius; 18 } 19 20 // See Figure A-16, lines 1421, for the main() method 21 22 } |

It is often a good idea to create a single constant instance of the java.util.Scanner class:

public static final java.util.Scanner INPUT = new java.util.Scanner(System.in);

Exercises

A.16 |
Define a constant String specifying your favorite color. |

## A 8 Operators |

Part I: Object-Oriented Programming

Encapsulation

Polymorphism

Inheritance

- Inheritance
- Extending a Class
- The Object Class
- Packages and Access Levels
- Summary
- Vocabulary
- Problems
- Projects

Part II: Linear Structures

Stacks and Queues

- Stacks and Queues
- The Stack Interface
- The Call Stack
- Exceptions
- The Queue Interface
- Summary
- Vocabulary
- Problems
- Projects

Array-Based Structures

- Array-Based Structures
- Shrinking and Stretching Arrays
- Implementing Stacks and Queues
- The List Interface
- Iterators
- The Java Collections Framework: A First Look
- Summary
- Vocabulary
- Problems
- Projects

Linked Structures

- Linked Structures
- List Nodes
- Stacks and Queues
- The LinkedList Class
- The Java Collections Framework Revisited
- Summary
- Vocabulary
- Problems
- Projects

Part III: Algorithms

Analysis of Algorithms

- Analysis of Algorithms
- Timing
- Asymptotic Notation
- Counting Steps
- Best, Worst, and Average Case
- Amortized Analysis
- Summary
- Vocabulary
- Problems
- Projects

Searching and Sorting

- Searching and Sorting
- Linear Search
- Binary Search
- Insertion Sort
- The Comparable Interface
- Sorting Linked Lists
- Summary
- Vocabulary
- Problems
- Projects

Recursion

- Recursion
- Thinking Recursively
- Analyzing Recursive Algorithms
- Merge Sort
- Quicksort
- Avoiding Recursion
- Summary
- Vocabulary
- Problems
- Projects

Part IV: Trees and Sets

Trees

Sets

- Sets
- The Set Interface
- Ordered Lists
- Binary Search Trees
- Hash Tables
- The Java Collections Framework Again
- Summary
- Vocabulary
- Problems
- Projects

Part V: Advanced Topics

Advanced Linear Structures

- Advanced Linear Structures
- Bit Vectors
- Sparse Arrays
- Contiguous Representation of Multidimensional Arrays
- Advanced Searching and Sorting
- Summary
- Vocabulary
- Problems
- Projects

Strings

Advanced Trees

- Advanced Trees
- Heaps
- Disjoint Set Clusters
- Digital Search Trees
- Red-Black Trees
- Summary
- Vocabulary
- Problems
- Projects

Graphs

- Graphs
- Terminology
- Representation
- Graph Traversal
- Topological Sorting
- Shortest Paths
- Minimum Spanning Trees
- Summary
- Vocabulary
- Problems
- Projects

Memory Management

Out to the Disk

- Out to the Disk
- Interacting with Files
- Compression
- External Sorting
- B-Trees
- Summary
- Vocabulary
- Problems
- Projects

Part VI: Appendices

A. Review of Java

- A. Review of Java
- A.1. The First Program
- A.2. Variables and Types
- A.3. Loops
- A.4. Interacting with the User
- A.5. Branching
- A.6. Methods and Breaking Out
- A.7. Constants
- A.8. Operators
- A.9. Debugging
- A.10. Coding Conventions

B. Unified Modeling Language

C. Summation Formulae

- C. Summation Formulae
- C.1. Sum Notation
- C.2. Sum of Constants
- C.3. Sum of First n Integers
- C.4. Sums of Halves and Doubles
- C.5. Upper Limit on Sum of a Function
- C.6. Constant Factors

D. Further Reading

Index

Data Structures and Algorithms in Java

ISBN: 0131469142

EAN: 2147483647

EAN: 2147483647

Year: 2004

Pages: 216

Pages: 216

Authors: Peter Drake

Similar book on Amazon

- Using SQL Data Definition Language (DDL) to Create Data Tables and Other Database Objects
- Working with SQL JOIN Statements and Other Multiple-table Queries
- Understanding SQL Subqueries
- Understanding Transaction Isolation Levels and Concurrent Processing
- Working with SQL Database Data Across the Internet

Flylib.com © 2008-2020.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net