Practical Exercises: Advanced Object Oriented Programming - Java For each of the following problems, start by...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Practical Exercises: Advanced Object Oriented Programming - Java For each of the following problems, start by developing the program logic, (Flowchart, or algorithm (pseudocode), then develop each solution in Java code. 1. Eight Queens A puzzler for chess buffs is the Eight Queens problem, which asks: Is it possible to place eight queens on an empty chessboard so that no queen is "attacking" any other (i.e., no two queens are in the same row, in the same column or along the same diagonal)? For example, if a queen is placed in the upper-left corner of the board, no other queens could be placed in any of the marked squares shown in Fig. 1. Solve the problem recursively. [Hint: Your solution should begin with the first column and look for a location in that column where a queen can be placed initially, place the queen in the first row. The solution should then recursively search the remaining columns. In the first few columns, there will be several locations where a queen may be placed. Take the first available location. If a column is reached with no possible location for a queen, the program should return to the previous column, and move the queen in that column to a new row. This continuous backing up and trying new alternatives is an example of recursive backtracking.] 2. Recursive File and Directory Manipulation Using the String-processing capabilities of textbook Chapter 14, the file and directory capabilities of Section 15.3 and a Map Section 16.10_, create an application that recursively walks a directory structure supplied by the user and reports the number of files of each file type (such as .java, .txt, .class, .docx, etc.) that exist in the specified directory path. Display the filename extensions in sorted order. Next, investigate method walk of class the Files. This method returns a stream that walks a directory and its subdirectories and returns the contents to you as a stream. Then, reimplement the first part of this exercise, using lambdas and streams, rather than recursion. 3. Calculating Factorials with Lambdas and Streams Reimplement the factorial methods of Figs. 18.3 -18.4 in the textbook page 761 to calculate factorials using lambdas and streams, rather than recursion. Submission requirements: Your submission must include a document with the logic for each problem in this assignment as well as each problem's source code. Due Session 12 * * * * * * * * * * * * * * * * * * * * * * Fig.1 Squares eliminated by placing a queen in the upper-left corner of a chessboard. 1 // Fig. 18.3: FactorialCalculator.java 2 // Recursive factorial method. 3 4 public class FactorialCalculator { 5 6 // recursive method factorial (assumes its parameter is >= 0) public static long factorial (long number) { 7 if (number 21; counter++) { 18 = System.out.printf("%d! %d%n", counter, factorial (counter)); 19 } 20 21 } 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 ... } 12! = 479001600 - 12! causes overflow for int variables ... 20! = 21! = 2432902008176640000 -4249290049419214848 - 21! causes overflow for long variables Practical Exercises: Advanced Object Oriented Programming - Java For each of the following problems, start by developing the program logic, (Flowchart, or algorithm (pseudocode), then develop each solution in Java code. 1. Eight Queens A puzzler for chess buffs is the Eight Queens problem, which asks: Is it possible to place eight queens on an empty chessboard so that no queen is "attacking" any other (i.e., no two queens are in the same row, in the same column or along the same diagonal)? For example, if a queen is placed in the upper-left corner of the board, no other queens could be placed in any of the marked squares shown in Fig. 1. Solve the problem recursively. [Hint: Your solution should begin with the first column and look for a location in that column where a queen can be placed initially, place the queen in the first row. The solution should then recursively search the remaining columns. In the first few columns, there will be several locations where a queen may be placed. Take the first available location. If a column is reached with no possible location for a queen, the program should return to the previous column, and move the queen in that column to a new row. This continuous backing up and trying new alternatives is an example of recursive backtracking.] 2. Recursive File and Directory Manipulation Using the String-processing capabilities of textbook Chapter 14, the file and directory capabilities of Section 15.3 and a Map Section 16.10_, create an application that recursively walks a directory structure supplied by the user and reports the number of files of each file type (such as .java, .txt, .class, .docx, etc.) that exist in the specified directory path. Display the filename extensions in sorted order. Next, investigate method walk of class the Files. This method returns a stream that walks a directory and its subdirectories and returns the contents to you as a stream. Then, reimplement the first part of this exercise, using lambdas and streams, rather than recursion. 3. Calculating Factorials with Lambdas and Streams Reimplement the factorial methods of Figs. 18.3 -18.4 in the textbook page 761 to calculate factorials using lambdas and streams, rather than recursion. Submission requirements: Your submission must include a document with the logic for each problem in this assignment as well as each problem's source code. Due Session 12 * * * * * * * * * * * * * * * * * * * * * * Fig.1 Squares eliminated by placing a queen in the upper-left corner of a chessboard. 1 // Fig. 18.3: FactorialCalculator.java 2 // Recursive factorial method. 3 4 public class FactorialCalculator { 5 6 // recursive method factorial (assumes its parameter is >= 0) public static long factorial (long number) { 7 if (number 21; counter++) { 18 = System.out.printf("%d! %d%n", counter, factorial (counter)); 19 } 20 21 } 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 ... } 12! = 479001600 - 12! causes overflow for int variables ... 20! = 21! = 2432902008176640000 -4249290049419214848 - 21! causes overflow for long variables
Expert Answer:
Related Book For
Business Communication Developing Leaders for a Networked World
ISBN: 978-9814714655
2nd edition
Authors: Peter Cardon
Posted Date:
Students also viewed these programming questions
-
CANMNMM January of this year. (a) Each item will be held in a record. Describe all the data structures that must refer to these records to implement the required functionality. Describe all the...
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
Gabriele Enterprises has bonds on the market making annual payments, with seven years to maturity, a par value of $1,000, and selling for $974. At this price, the bonds yield 7.2 percent. What must...
-
Compare the confidence intervals in Exercise 8.29. What effect does each of these actions have on the width of a confidence interval? a. Double the sample size b. Quadruple the sample size
-
Last year, Tammy opened an investment account with $6200 . At the end of the year, the amount in the account had decreased by 6.5% . How much is this decrease in dollars? How much money was in her...
-
What are the distinct parts of an insurance policy?
-
The following selected transactions occurred during 2018 and 2019 for Baltic Importers. The company ends its accounting year on September 30. 2018 Jul. 1 Loaned $16,000 cash to Bud Shyne on a...
-
1/3 + + + + + A B D E The points A, B, C,..., G, and H are equally spaced along the rational number line to the right. Complete parts (a) through (d) + below. (a) What rational number corresponds to...
-
The sales volume variance is the difference between the: static budget (based on planned volume) and actual revenue or cost. flexible budget (based on actual volume) and actual revenue or cost....
-
Show how the Prisoners Dilemma led to massive doping in international track and field. What can authorities do to prevent doping from becoming a dominant strategy?
-
A destitute homeless man steals food from a convenience market. When prosecuted for theft, he pleads the defense of necessity. The prosecutor urges the trial judge to strike the defense on the ground...
-
An NFL running back comes to you, claiming that black running backs are the victims of racial discrimination by teams. Devise an econometric model that would test his claim. What finding would prove...
-
Sally Shopper drives a 1995 gray Honda Accord. She parks it in a shopping center lot while she shops. Two hours later, Shopper enters a car parked nearby that is almost identical to hers. As she...
-
The International Olympic Committee now allows athletes to selfidentify as male or female. What do you think of this policy?
-
A marketing executive once said, If the price elasticity of demand for your product is inelastic, then your price is probably too low. What is this executive saying in terms of the economic...
-
Should we separate the debt and equity features of convertible debt? Team 1: Pro separation: Present arguments in favor of separating the debt and equity features of convertible debt. Team 2: Against...
-
What methods can a company use to raise capital?
-
Does higher expected inflation increase, decrease, or have no effect on the required rate of return?
-
What are some of the advantages of equity financing?
Study smarter with the SolutionInn App