1. Trang chủ
  2. » Công Nghệ Thông Tin

Java Concepts 5th Edition and 6th phần 8 pdf

111 374 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Java Concepts 5th Edition and 6th phần 8 pdf
Trường học Standard University
Chuyên ngành Computer Science
Thể loại Tài liệu
Thành phố City Name
Định dạng
Số trang 111
Dung lượng 0,99 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Suppose your task is to write a program that reads in such a file and prints • The country with the largest area • The country with the largest population • The country with the largest

Trang 1

100 assert state == TRANSACT;

  118      Gets the current state of this ATM

119 @return the current state

126 private int state;

127 private int customerNumber;

128 private Customer currentCustomer;

129 private BankAccount currentAccount;

130 private Bank theBank;

131

132 public static final int START = 1;

133 public static final int PIN = 2;

134 public static final int ACCOUNT = 3;

135 public static final int TRANSACT = 4;

136

137 public static final int CHECKING = 1;

138 public static final int SAVINGS = 2;

139 }

566 567

Trang 2

  20      Reads the customer numbers and pins

  21      and initializes the bank accounts

22 @param filename the name of the customer file

30 int number = in.nextInt();

31 int pin = in.nextInt();

32 Customer c = new Customer(number, pin);

Trang 3

  39      Adds a customer to the bank.

40 @param c the customer to add

  48      Finds a customer in the bank

49 @param aNumber a customer number

50 @param aPin a personal identification number

51 @return the matching customer, or null if no customer

  7      Constructs a customer with a given number and PIN

8 @param aNumber the customer number

Trang 4

9 @param aPinthe personal identification number

15 checkingAccount = new BankAccount();

16 savingsAccount = new BankAccount();

22 @param aNumber a customer number

23 @param aPin a personal identification number

24 @return true if the customer number and PIN match

  32      Gets the checking account of this customer

33 @return the checking account

  41      Gets the savings account of this customer

42 @return the checking account

Trang 5

50 private int pin;

51 private BankAccount checkingAccount;

52 private BankAccount savingsAccount;

53 }The following class implements a console user interface for the ATM

Trang 6

31 System.out.print(“Enter customer number: ”);

32 int number = in.nextInt();

33 theATM.setCustomerNumber(number);

34 }

35 else if (state == ATM.PIN)

36 {

37 System.out.print(“Enter PIN: ”);

38 int pin = in.nextInt();

44 String command = in.next();

53 }

54 else if (state == ATM.TRANSACT)

55 {

56 System.out.println(“Balance=” + theATM.getBalance());

57 System.out.print(“A=Deposit, B=Withdrawal, C=Cancel: ”);

58 String command = in.next();

59 if (command.equalsIgnoreCase(“A”))

60 {

61 System.out.print(“Amount: ”);

62 double amount = in.nextDouble();

63 theATM.deposit(amount);

64 theATM.back();

65 }

Trang 7

67 {

68 System.out.print(“Amount: ”);

69 double amount = in.nextDouble();

Amount: 1000

A=Checking, B=Savings, C=Quit: C

.Here are the user interface classes for the GUI version of the user interface

Trang 8

27 JFrame frame = new ATMFrame(theATM);

28 frame.setTitle(“First National Bank of Java”);

Trang 10

  53      Updates display message.

61 else if (state == ATM.PIN)

62 display.setText(“Enter PIN\nA = OK”);

63 else if (state == ATM.ACCOUNT)

64 display.setText(“Select Account\n”

65 + “A = Checking\nB = Savings\nC = Exit”);

66 else if (state == ATM.TRANSACT)

80 else if (state == ATM.PIN)

81 theATM.selectCustomer((int) pad.getValue());

82 else if (state == ATM.ACCOUNT)

Trang 11

122 private JButton aButton;

123 private JButton bButton;

124 private JButton cButton;

125

126 private KeyPad pad;

573 574

Trang 12

127 private JTextArea display;

128

129 private ATM theATM;

130

131 private static final int FRAME_WIDTH = 300;

132 private static final int FRAME_HEIGHT =

300;

133 }This class uses layout managers to arrange the text field and the keypad buttons

See Chapter 18 for more information about layout managers

  10       A component that lets the user enter a number, using

  11       a keypad labeled with digits

Trang 13

29 buttonPanel = new JPanel();

66 /**

575 576

Trang 14

  67      Adds a button to the button panel.

68 @param label the button label

77 // Don't add two decimal points

84 }

85 } 86

87 JButton button = new JButton (label);

93 /**

  94       Gets the value that the user entered

95 @return the value in the text field of the keypad

Trang 15

110 private JPanel buttonPanel;

111 private JButton clearButton;

112 private JTextField display;

immediately home in on a good solution and that you need to go back and

reorganize your classes and responsibilities That is normal and only to be expected The purpose of the object-oriented design process is to spot these problems in the

design phase, when they are still easy to rectify, instead of in the implementation

phase, when massive reorganization is more difficult and time consuming

RANDOM FACT 12.2: Software Development–Art or

Science?

There has been a long discussion whether the discipline of computing is a

science or not We call the field “computer science”, but that doesn't mean much

576 577

Trang 16

Except possibly for librarians and sociologists, few people believe that library

science and social science are scientific endeavors

A scientific discipline aims to discover certain fundamental principles dictated

by the laws of nature It operates on the scientific method: by posing hypotheses

and testing them with experiments that are repeatable by other workers in the

field For example, a physicist may have a theory on the makeup of nuclear

particles and attempt to confirm or refute that theory by running experiments in a particle collider If an experiment cannot be confirmed, such as the “cold fusion” research in the early 1990s, then the theory dies a quick death

Some software developers indeed run experiments They try out various methods

of computing certain results or of configuring computer systems, and measure

the differences in performance However, their aim is not to discover laws of

nature

Some computer scientists discover fundamental principles One class of

fundamental results, for instance, states that it is impossible to write certain

kinds of computer programs, no matter how powerful the computing equipment

is For example, it is impossible to write a program that takes as its input any two Java program files and as its output prints whether or not these two programs

always compute the same results Such a program would be very handy for

grading student homework, but nobody, no matter how clever, will ever be able

to write one that works for all input files However, the majority of computer

scientists are not researching the limits of computation

Some people view software development as an art or craft A programmer who

writes elegant code that is easy to understand and runs with optimum efficiency

can indeed be considered a good craftsman Calling it an art is perhaps

far-fetched, because an art object requires an audience to appreciate it, whereas

the program code is generally hidden from the program user

Others call software development an engineering discipline Just as mechanical

engineering is based on the fundamental mathematical principles of statics,

computing has certain mathematical foundations There is more to mechanical

engineering than mathematics, such as knowledge of materials and of project

planning The same is true for computing A software engineer needs to know

about planning, budgeting, design, test automation, documentation, and source

577 578

Trang 17

algorithm design, and database technologies.

In one somewhat worrisome aspect, software development does not have the

same standing as other engineering disciplines There is little agreement as to

what constitutes professional conduct in the computer field Unlike the scientist,

whose main responsibility is the search for truth, the software developer must

strive to satisfy the conflicting demands of quality, safety, and economy

Engineering disciplines have professional organizations that hold their members

to standards of conduct The computer field is so new that in many cases we

simply don't know the correct method for achieving certain tasks That makes it

difficult to set professional standards

What do you think? From your limited experience, do you consider software

development an art, a craft, a science, or an engineering activity?

CHAPTER SUMMARY

1 The life cycle of software encompasses all activities from initial analysis until

obsolescence

2 A formal process for software development describes phases of the

development process and gives guidelines for how to carry out the phases

3 The waterfall model of software development describes a sequential process of

analysis, design, implementation, testing, and deployment

4 The spiral model of software development describes an iterative process in

which design and implementation are repeated

5 Extreme Programming is a development methodology that strives for simplicity

by removing formal structure and focusing on best practices

6 In object-oriented design, you discover classes, determine the responsibilities

of classes, and describe the relationships between classes

7 A CRC card describes a class, its responsibilities, and its collaborating classes

Trang 18

8 Inheritance (the is-a relationship) is sometimes inappropriately used when the

has-a relationship would be more appropriate

9 Aggregation (the has-a relationship) denotes that objects of one class contain

references to objects of another class

10 Dependency is another name for the “uses” relationship

11 You need to be able to distinguish the UML notations for inheritance, interface

implementation, aggregation, and dependency

12 Use javadoc comments (with the method bodies left blank) to record the

behavior of classes

FURTHER READING

1 Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, Addison-Wesley, 1999

2 Kent Beck, Extreme Programming Explained, Addison-Wesley, 1999

3 W H Sackmann, W J Erikson, and E E Grant, “Exploratory

Experimental Studies Comparing Online and Offline Programming Performance”, Communications of the ACM, vol 11, no 1 (January 1968),

pp 3–11

4 F Brooks, The Mythical Man-Month, Addison-Wesley, 1975

REVIEW EXERCISES

★ Exercise R12.1 What is the software life cycle?

★★ Exercise R12.2 List the steps in the process of object-oriented design that

this chapter recommends for student use

★ Exercise R12.3 Give a rule of thumb for how to find classes when

designing a program

★ Exercise R12.4 Give a rule of thumb for how to find methods when

designing a program

578 579

Trang 19

★★ Exercise R12.5 After discovering a method, why is it important to

identify the object that is responsible for carrying out the action?

★ Exercise R12.6 What relationship is appropriate between the following

classes: aggregation, inheritance, or neither?

★★ Exercise R12.8 Some books on object-oriented programming recommend using inheritance so that the class Circle extends the class Point Then the Circle class inherits the setLocation method from the Point superclass Explain why the setLocation method need not be redefined

in the subclass Why is it nevertheless not a good idea to have Circle inherit from Point? Conversely, would inheriting Point from Circle fulfill the is-a rule? Would it be a good idea?

★ Exercise R12.9 Write CRC cards for the Coin and CashRegister

classes described in Section 8.2

★ Exercise R12.10 Write CRC cards for the Bank and BankAccount

classes in Section 7.2

579 580

Trang 20

★★ Exercise R12.11 Draw a UML diagram for the Coin and

CashRegister classes described in Section 8.2

★★★ Exercise R12.12 A file contains a set of records describing countries

Each record consists of the name of the country, its population, and its area Suppose your task is to write a program that reads in such a file and prints

• The country with the largest area

• The country with the largest population

• The country with the largest population density (people per square kilometer)

Think through the problems that you need to solve What classes and methods will you need? Produce a set of CRC cards, a UML diagram, and a set of javadoc comments

★★★ Exercise R12.13 Discover classes and methods for generating a student

report card that lists all classes, grades, and the grade point average for a semester Produce a set of CRC cards, a UML diagram, and a set of javadoc comments

★★★ Exercise R12.14 Consider a quiz grading system that grades student

responses to quizzes A quiz consists of questions There are different types of questions, including essay questions and multiple-choice questions Students turn in submissions for quizzes, and the grading system grades them Draw a UML diagram for classes Quiz, Question, EssayQuestion, MultipleChoiceQuestion, Student, and Submission

Additional review exercises are available in WileyPLUS

PROGRAMMING EXERCISES

★★ Exercise P12.1 Enhance the invoice-printing program by providing for

two kinds of line items: One kind describes products that are purchased in

Trang 21

fixed charge (such as “shipping: $5.00”) Hint: Use inheritance Produce a UML diagram of your modified implementation.

★★ Exercise P12.2 The invoice-printing program is somewhat unrealistic

because the formatting of the LineItem objects won't lead to good visual results when the prices and quantities have varying numbers of digits

Enhance the format method in two ways: Accept an int[] array of column widths as a parameter Use the NumberFormat class to format the currency values

★★ Exercise P12.3 The invoice-printing program has an unfortunate flaw—it

mixes “business logic”, the computation of total charges, and

“presentation”, the visual appearance of the invoice To appreciate this flaw, imagine the changes that would be necessary to draw the invoice in HTML for presentation on the Web Reimplement the program, using a separate InvoiceFormatter class to format the invoice That is, the Invoice and LineItem methods are no longer responsible for formatting However, they will acquire other responsibilities, because the InvoiceFormatter class needs to query them for the values that it requires

★★★ Exercise P12.4 Write a program that teaches arithmetic to your younger brother The program tests addition and subtraction In level 1 it tests only addition of numbers less than 10 whose sum is less than 10 In level

2 it tests addition of arbitrary one-digit numbers In level 3 it tests subtraction of one-digit numbers with a non-negative difference

Generate random problems and get the player input The player gets up

to two tries per problem Advance from one level to the next when the player has achieved a score of five points

★★★ Exercise P12.5 Design a simple e-mail messaging system A message

has a recipient, a sender, and a message text A mailbox can store messages Supply a number of mailboxes for different users and a user interface for users to log in, send messages to other users, read their own messages, and log out Follow the design process that was described in this chapter

580 581

Trang 22

★★ Exercise P12.6 Write a program that simulates a vending machine

Products can be purchased by inserting coins with a value at least equal to the cost of the product A user selects a product from a list of available products, adds coins, and either gets the product or gets the coins returned

if insufficient money was supplied or if the product is sold out The machine does not give change if too much money was added Products can

be restocked and money removed by an operator Follow the design process that was described in this chapter Your solution should include a class VendingMachine that is not coupled with the Scanner or PrintStream classes

★★★ Exercise P12.7 Write a program to design an appointment calendar An

appointment includes the date, starting time, ending time, and a description; for example,

Dentist 2007/10/1 17:30 18:30CS1 class 2007/10/2 08:30 10:00Supply a user interface to add appointments, remove canceled appointments, and print out a list of appointments for a particular day

Follow the design process that was described in this chapter Your solution should include a class AppointmentCalendar that is not coupled with the Scanner or PrintStream classes

★★★ Exercise P12.8 Airline seating Write a program that assigns seats on an airplane Assume the airplane has 20 seats in first class (5 rows of 4 seats each, separated by an aisle) and 90 seats in economy class (15 rows of 6 seats each, separated by an aisle) Your program should take three commands: add passengers, show seating, and quit When passengers are added, ask for the class (first or economy), the number of passengers traveling together (1 or 2 in first class; 1 to 3 in economy), and the seating preference (aisle or window in first class; aisle, center, or window in economy) Then try to find a match and assign the seats If no match exists, print a message Your solution should include a class Airplane that is not coupled with the Scanner or PrintSream classes Follow the design process that was described in this chapter

581 582

Trang 23

★★ Exercise P12.9 Modify the implementations of the class in the ATM

example so that the bank manages a collection of bank accounts and a separate collection of customers Allow joint accounts in which some accounts can have more than one customer

★★★ Exercise P12.10 Write a program that administers and grades quizzes

A quiz consists of questions There are four types of questions: text questions, number questions, choice questions with a single answer, and choice questions with multiple answers When grading a text question, ignore leading or trailing spaces and letter case When grading a numeric question, accept a response that is approximately the same as the answer

A quiz is specified in a text file Each question starts with a letter indicating the question type (T, N, S, M), followed by a line containing the question text The next line of a non-choice question contains the answer Choice questions have a list of choices that is terminated by a blank line Each choice starts with + (correct) or − (incorrect) Here is a sample file:

TWhich Java keyword is used to define a subclass?

extendsS

What is the original name of the Java language?

- *7

- + Oak

C - GoslingM

Which of the following types are supertypes of Rectangle?

- PrintStream+ Shape

+ RectangularShape+ Object

- StringN

What is the square root of 2?

1.41421356

Trang 24

Your program should read in a quiz file, prompt the user for responses to all questions, and grade the responses Follow the design process that was described in this chapter.

★★★G Exercise P12.11 Implement a program to teach your baby sister

to read the clock In the game, present an analog clock, such as the one in

Figure 12 Generate random times and display the clock Accept guesses from the player Reward the player for correct guesses After two

incorrect guesses, display the correct answer and make a new random time Implement several levels of play In level 1, only show full hours

In level 2, show quarter hours In level 3, show five-minute multiples, and in level 4, show any number of minutes After a player has achieved five correct guesses at one level, advance to the next level

Figure 12

An Analog Clock

★★★G Exercise P12.12 Write a program that can be used to design a suburban scene, with houses, streets, and cars Users can add houses and cars of various colors to a street Write more specific requirements that include a detailed description of the user interface Then, discover classes and methods, provide UML diagrams, and implement your program

★★★G Exercise P12.13 Write a simple graphics editor that allows users to add a mixture of shapes (ellipses, rectangles, and lines in different colors) to a panel Supply commands to load and save the

582 583

Trang 25

Additional programming exercises are available in WileyPLUS

PROGRAMMING PROJECTS

★★★ Project 12.1 Produce a requirements document for a program that

allows a company to send out personalized mailings, either by e-mail or through the postal service Template files contain the message text, together with variable fields (such as Dear [Title] [Last Name] …) A database (stored as a text file) contains the field values for each recipient Use HTML as the output file format Then design and implement the program

★★★ Project 12.2 Write a tic-tac-toe game that allows a human player to play against the computer Your program will play many turns against a human opponent, and it will learn When it is the computer's turn, the computer randomly selects an empty field, except that it won't ever choose a losing combination For that purpose, your program must keep

an array of losing combinations Whenever the human wins, the immediately preceding combination is stored as losing For example, suppose that X = computer and O = human Suppose the current combination is

Now it is the human's turn, who will of course choose

583 584

Trang 26

The computer should then remember the preceding combination

as a losing combination As a result, the computer will never again choose that combination from

or

Discover classes and supply a UML diagram before you begin to program

ANSWERS TO SELF-CHECK QUESTIONS

1 It is unlikely that the customer did a perfect job with the requirements

document If you don't accommodate changes, your customer may not like the outcome If you charge for the changes, your customer may not like the cost

584 585

Trang 27

2 An “extreme” spiral model, with lots of iterations.

3 To give frequent feedback as to whether the current iteration of the product fits customer needs

4 FileWriter

5 To produce the shipping address of the customer

6 Reword the responsibilities so that they are at a higher level, or come up

with more classes to handle the responsibilities

7 Through aggregation The bank manages bank account objects

8 Through inheritance

9 The BankAccount, System, and PrintStream classes

10 The Invoice class is responsible for computing the amount due It

collaborates with the LineItem class

11 This design decision reduces coupling It enables us to reuse the classes

when we want to show the invoice in a dialog box or on a web page

12 The bank needs to store the list of customers so that customers can log in

We need to locate all bank accounts of a customer, and we chose to simply store them in the customer class In this program, there is no further need to access bank accounts

13 The Bank class needs to have an additional responsibility: to load and save the accounts The bank can carry out this responsibility because it has access to the customer objects and, through them, to the bank accounts

Trang 28

Chapter 13 Recursion

CHAPTER GOALS

• To learn about the method of recursion

• To understand the relationship between recursion and iteration

• To analyze problems that are much easier to solve by recursion than by

iteration

• To learn to “think recursively”

• To be able to use recursive helper methods

• To understand when the use of recursion affects the efficiency of an algorithm

The method of recursion is a powerful technique to break up complex

computational problems into simpler ones The term “recursion” refers to the fact

that the same computation recurs, or occurs repeatedly, as the problem is solved

Recursion is often the most natural way of thinking about a problem, and there are

some computations that are very difficult to perform without recursion This chapter

shows you simple and complex examples of recursion and teaches you how to “think

recursively”

13.1 Triangle Numbers

We begin this chapter with a very simple example that demonstrates the power of

thinking recursively In this example, we will look at triangle shapes such as the ones

from Section 6.3 We'd like to compute the area of a triangle of width n, assuming

that each [] square has area 1 This value is sometimes called the nth triangle number

For example, as you can tell from looking at

Trang 29

You may know that there is a very simple formula to compute these numbers, but you should pretend for now that you don't know about it The ultimate purpose of this

section is not to compute triangle numbers, but to learn about the concept of recursion

in a simple situation

Here is the the class that we will develop:

public class Triangle

If the width of the triangle is 1, then the triangle consists of a single square, and its

area is 1 Let's take care of this case first

public int getArea()

Suppose we knew the area of the smaller, colored triangle Then we could easily

compute the area of the larger triangle as

smallerArea + width

How can we get the smaller area? Let's make a smaller triangle and ask it!

Triangle smallerTriangle = new Triangle(width - 1);

588 589

Trang 30

int smallerArea = smallerTriangle.getArea();

Now we can complete the getArea method:

public int getArea()

{

if (width == 1) return 1;

Triangle smallerTriangle = new Triangle(width

- 1);

int smallerArea = smallerTriangle.getArea();

return smallerArea + width;

}

A recursive computation solves a problem by using the solution of the same

problem with simpler values

Here is an illustration of what happens when we compute the area of a triangle of

width 4

• The getArea method makes a smaller triangle of width 3

• It calls getArea on that triangle

• That method makes a smaller triangle of width 2

• It calls getArea on that triangle

• That method makes a smaller triangle of width 1

• It calls getArea on that triangle

• That method returns 1

• The method returns smallerArea + width = 1 + 2 = 3

• The method returns smallerArea + width = 3 + 3 = 6

• The method returns smallerArea + width = 6 + 4 = 10

This solution has one remarkable aspect To solve the area problem for a triangle of a

given width, we use the fact that we can solve the same problem for a lesser width

This is called a recursive solution

Trang 31

The call pattern of a recursive method looks complicated, and the key to the

successful design of a recursive method is not to think about it Instead, look at the

getArea method one more time and notice how utterly reasonable it is If the width

is 1, then, of course, the area is 1 The next part is just as reasonable Compute the

area of the smaller triangle and don't think about why that works Then the area of the larger triangle is clearly the sum of the smaller area and the width

There are two key requirements to make sure that the recursion is successful:

• Every recursive call must simplify the computation in some way

• There must be special cases to handle the simplest computations directly

The getArea method calls itself again with smaller and smaller width values

Eventually the width must reach 1, and there is a special case for computing the area

of a triangle with width 1 Thus, the getArea method always succeeds

For a recursion to terminate, there must be special cases for the simplest values

Actually, you have to be careful What happens when you call the area of a triangle

with width −1? It computes the area of a triangle with width −2, which computes the

area of a triangle with width −3, and so on To avoid this, the getArea method

should return 0 if the width is ≤ 0

Recursion is not really necessary to compute the triangle numbers The area of a

triangle equals the sum

Many simple recursions can be computed as loops However, loop equivalents for

more complex recursions—such as the one in our next example—can be complex

Actually, in this case, you don't even need a loop to compute the answer The sum of

the first n integers can be computed as

589 590

Trang 32

1 + 2 + … + n = n × ( n + 1) / 2Thus, the area equals

width * (width + 1) / 2

Therefore, neither recursion nor a loop is required to solve this problem The

recursive solution is intended as a “warm-up” to introduce you to the concept of

11      Constructs a triangular shape

12 @param aWidth the width (and height) of the triangle

20       Computes the area of the triangle

21 @return the area

Trang 33

5 Triangle t = new Triangle(10);

6 int area = t.getArea();

7 System.out.println(“Area: ” + area);

1 Why is the statement if (width == 1) return 1; in the

getArea method unnecessary?

2 How would you modify the program to recursively compute the area of

a square?

COMMON ERROR 13.1: Infinite Recursion

A common programming error is an infinite recursion: a method calling itself over

and over with no end in sight The computer needs some amount of memory for

bookkeeping for each call After some number of calls, all memory that is

591 592

Trang 34

available for this purpose is exhausted Your program shuts down and reports a

“stack fault”

Infinite recursion happens either because the parameter values don't get simpler or

because a special terminating case is missing For example, suppose the getArea

method com putes the area of a triangle with width 0 If it wasn't for the special

test, the method would have constructed triangles with width −1, −2, −3, and so on

13.2 Permutations

We will now turn to a more complex example of recursion that would be difficult to

program with a simple loop We will design a class that lists all permutations of a

string A permutation is simply a rearrangement of the letters For example, the string

“eat” has six permutations (including the original string itself):

As in the preceding section, we will define a class that is in charge of computing the

answer In this case, the answer is not a single number but a collection of permuted

strings Here is our class:

public class PermutationGenerator

Trang 35

Now we need a way to generate the permutations recursively Consider the string

"eat" Let's simplify the problem First, we'll generate all permutations that start

with the letter ’e’, then those that start with ’a’, and finally those that start with ’

t’ How do we generate the permutations that start with ’e’? We need to know the

permutations of the substring “at” But that's the same problem—to generate all

permutations—with a simpler input, namely the shorter string "at" Thus, we can

use recursion Generate the permutations of the substring "at" They are

"at"

"ta"

For each permutation of that substring, prepend the letter ’e’ to get the permutations

of “eat” that start with ’e’, namely

"eat"

"eta"

592 593

Trang 36

Now let's turn our attention to the permutations of "eat" that start with ’a’ We

need to produce the permutations of the remaining letters, "et" They are:

We generate the permutations that start with ’t’ in the same way

That's the idea The implementation is fairly straightforward In the

get-Permutations method, we loop through all positions in the word to be

permuted For each of them, we compute the shorter word that is obtained by

removing the ith letter:

String shorterWord = word.substring(0, i) +

for (String s : shorterWordPermutations)

{

result.add(word.charAt(i) + s);

}

As always, we have to provide a special case for the simplest strings The simplest

possible string is the empty string, which has a single permutation—itself

Here is the complete PermutationGenerator class

593 594

Trang 37

9 Constructs a permutation generator.

10 @param aWord the word to permute

31        // Loop through all character positions

32 for (int i = 0; i < word.length(); i++)

Trang 38

44       // Add the removed character to the front of

45       // each permutation of the simpler word

Compare the PermutationGenerator and Triangle classes Both of them

work on the same principle When they work on a more complex input, they first

solve the problem for a simpler input Then they combine the result for the simpler

input with additional work to deliver the results for the more complex input There

really is no particular complexity behind that process as long as you think about the

solution on that level only However, behind the scenes, the simpler input creates

even simpler input, which creates yet another simplification, and so on, until one

input is so simple that the result can be obtained without further help It is interesting

to think about this process, but it can also be confusing What's important is that you

can focus on the one level that matters—putting a solution together from the slightly

simpler problem, ignoring the fact that it also uses recursion to get its results

Trang 39

SELF CHECK

3 What are all permutations of the four-letter word beat?

4 Our recursion for the permutation generator stops at the empty string

What simple modification would make the recursion stop at strings of length 0 or 1?

COMMON ERROR 13.2: Tracing Through Recursive

Methods

Debugging a recursive method can be somewhat challenging When you set a

breakpoint in a recursive method, the program stops as soon as that program line is encountered in any call to the recursive method Suppose you want to debug the

recursive getArea method of the Triangle class Debug the

TriangleTester program and run until the beginning of the getArea

method Inspect the width instance variable It is 10

Remove the breakpoint and now run until the statement return

smallerArea + width; (see Figure 1) When you inspect width again, its

value is 2! That makes no sense There was no instruction that changed the value

of width Is that a bug with the debugger?

No The program stopped in the first recursive call to getArea that reached the

return statement If you are confused, look at the call stack (top left in the

figure) You will see that nine calls to getArea are pending

You can debug recursive methods with the debugger You just need to be

particularly careful, and watch the call stack to understand which nested call you

currently are in

595 596

Trang 40

Figure 1

Debugging a Recursive Method

HOW TO 13.1: Thinking Recursively

To solve a problem recursively requires a different mindset than to solve it by

programming loops In fact, it helps if you are, or pretend to be, a bit lazy and like

others to do most of the work for you If you need to solve a complex problem,

pretend that “someone else” will do most of the heavy lifting and solve the

596 597

Ngày đăng: 12/08/2014, 19:21

TỪ KHÓA LIÊN QUAN