Peevish_Postman.java import java.util.*; /* * CPS 150 * Algorithms & Programming I * * Lab Project: The Peevish Postman Problem (Java Boolean Arrays) * * Name: */ public class Peevish_Postman { public static void main(String[] args) { boolean[] doors; final int NUM_DOORS = 100; final boolean OPEN = true; final boolean CLOSED = false; // Initialize the doors doors = new
Peevish_Postman.java import java.util.*; /* * CPS 150 * Algorithms & Programming I * * Lab Project: The Peevish Postman Problem (Java Boolean Arrays) * * Name: */ public class Peevish_Postman { public static void main(String[] args) { boolean[] doors; final int NUM_DOORS = 100; final boolean OPEN = true; final boolean CLOSED = false; // Initialize the doors doors = new
Peevish_Postman.java import java.util.*; /* * CPS 150 * Algorithms & Programming I * * Lab Project: The Peevish Postman Problem (Java Boolean Arrays) * * Name: */ public class Peevish_Postman { public static void main(String[] args) { boolean[] doors; final int NUM_DOORS = 100; final boolean OPEN = true; final boolean CLOSED = false; // Initialize the doors doors = new
// Peevish_Postman.java import java.util.*; /* * CPS 150 * Algorithms & Programming I * * Lab Project: The Peevish Postman Problem (Java Boolean Arrays) * * Name: */ public class Peevish_Postman { public static void main(String[] args) { boolean[] doors; final int NUM_DOORS = 100; final boolean OPEN = true; final boolean CLOSED = false; // Initialize the doors doors = new boolean[NUM_DOORS+1]; // We will not use doors[0] for (int i = 1; i <= NUM_DOORS; i++) { doors[i] = CLOSED; } // Print the initial state of each door (1-100) for (int i = 1; i <= NUM_DOORS; i++) { System.out.print("Door " + i + " is "); if (doors[i]) { System.out.println("open."); } else { System.out.println("closed."); } } // ADD YOUR CODE BETWEEN THIS LINE ... // ... AND THIS LINE // Print the final state of each door (1-100) for (int i = 1; i <= NUM_DOORS; i++) { System.out.print("Door " + i + " is "); if (doors[i]) { System.out.println("open."); } else { System.out.println("closed."); } } } // end main method } // end class Peevish_Postman
Transcribed Image Text:CPS 150 Lab Project 24: The
Peevish Postman Problem (Java
Boolean Arrays)
This lab project is due on Wednesday,
April 6, 2022, 12:30 pm.
British puzzle maker H. E. Dudeney
concocted an interesting puzzle about a
bored postman called the "Peevish Postman
Problem". According to Dudeney, the
postman worked in a small post office with
consecutive letter boxes numbered 1 to 100.
Each box was equipped with a door that
could be opened and closed. Late one
evening the postman made a "pass" through
the boxes and opened every door. Still
bored, he walked back to the beginning and
made a second pass, this time visiting boxes
2, 4, 6, .., 100. Since those doors were now
open, he closed them. On the third pass he
visited boxes 3, 6, 9, 12, ..., 99 and if a door
was open he closed it, and if the door was
closed he opened it. He continued to make
passes through the boxes and always
followed the same rule: On each pass i from
1 to 100, he visited only boxes that were
multiples of i, ... and changed the state of
each door he visited. After making 100
passes at the doors, he surveyed the results
and was surprised by the pattern of doors
that he saw.
The code below uses a boolean array to
represent the doors. A true value in the
array represents an open door, and a false
value represents a closed one.
Create a new Eclipse project named
Lab_Project_24, and download and import
the Peevish_Postman.java source code file.
Then, complete the code so that it simulates
the Peevish Postman problem as described
above. You will have to write two nested
loops in order to manipulate the array as
described above. The inner loop will control
the door number visited on a single pass,
and the outer loop will control the number
of passes.
The puzzle was conceived as a paper and
pencil entertainment. Can you
describe/explain the final pattern of doors?
Note: Because the doors are numbered
starting at one, we will waste the first
position (i.e., index 0) in the array. In this
case, the default value will be set to false. By
ignoring the first position, the door numbers
match their index positions in the array.
What Do I Hand In?
• Enter your explanation of the door
pattern in the space provided.
• Attach your completed source code
(Peevish Postman.java) file below.
Then click Submit.
Process or set of rules that allow for the solving of specific, well-defined computational problems through a specific series of commands. This topic is fundamental in computer science, especially with regard to artificial intelligence, databases, graphics, networking, operating systems, and security.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.