Do My Java assignment using applets and solve door puzzle problem
Question - 1. British puzzlemaker 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†at 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 passe
...Read More
s at 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. Door class below to simulate the activity of the peevish postman. Door class: { private String state; // Open or closed private String message; Constructor for objects of class Door. Pass in "open" or "closed" to represent the state of the Door object. @param aState the initial state of the Door object */ public Door(String aState) { // Initialize the state of the Door with one of two states: open, closed state = aState; message = ""; } /** A Boolean method that reports whether the state variable is "open". @return true if state equals "open" */ public boolean isOpen() { return state.equals("open"); } A Boolean method that reports whether the state variable is "closed". @return true if state equals "closed" !1
*/ public boolean isClosed() { return state.equals("closed"); } A mutator method that sets the state variable. @param newState the new value for the state variable */ public void setState(String newState) { state = newState; } An accessor method that gets the door's message variable. */ public String getMessage() { return message; } Sets the state of the Door to open if the door is closed. */ public void open() { if (state.equals("open")) { message = "The door is already open."; } else { state = "open"; message = "The door has been opened"; } } Sets the state of the Door to closed if the door is open. */ public void close() { if (state.equals("open")) { state = "closed"; message = "The door has been closed."; } else { message = "The door is already closed."; } } { return "The door is " + state + "."; !2
} } Door class for doing this. The methods should use the following signatures: public void drawClosedDoor(Graphics page) Graphics object is passed as a parameter to each method. Review the available methods in the Graphics class and use its methods to draw representations of open and closed doors. (Hint: public void drawRect(int x, int y, int width, int height) might get you started.) Door object has to be associated with a JPanel. Then the paintComponent method of the JPanel can be used to invoke drawOpendoor and drawClosedDoor. TempDoorPanel which can be used as is to create and display a door inside a JFrame. You will still need a main method, which is supplied in the next step, in order to use the JPanel below. import java.awt.event.*; import javax.swing.*; public class TempDoorPanel extends JPanel { private Door door; public TempDoorPanel () { door = new Door("closed"); setBackground (Color.blue); setPreferredSize (new Dimension(360, 400)); } { super.paintComponent(page); //door.drawOpenDoor(page); door.drawClosedDoor(page); } } JFrame object needs to be instantiated, and a TempDoorPanel needs to be instantiated and added to the JFrame. The JFrame is then packed and made visible. Here is the main program code which can be used to finally display your door. public class TempDoorRunner { !3
/** Creates the main frame of the program. */ public static void main (String[] args) { JFrame frame = new JFrame ("Door"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); TempDoorPanel panel = new TempDoorPanel(); frame.add(panel); frame.pack(); frame.setVisible(true); } } 2 We can begin to make the application interactive by adding buttons. The buttons can be clicked to open and close the door visually. DoorPanel, based on TempDoorPanel that adds two JButton references to the panel: one to open and one to close the door. Also add a JTextField reference to the panel so the message inside the Door class can be displayed. Modify the constructor to instantiate the JButton objects and the JTextField. Set the JTextField to the current message in the door. (Hint: use getMessage in Door and setText in JTextField.) Organize the buttons and text field on a separate panel. Choose a suitable layout manager to build the DoorPanel. paintComponent in DoorPanel so that it uses conditional logic to test the state of the door. Invoke drawOpenDoor if the door is currently in an open state. Otherwise invoke drawClosedDoor. DoorRunner class based on TempDoorRunner to display the new JPanel. The buttons will not yet be responsive to clicks but you should be able to use the code to display an open or closed door, and you should see the buttons and text field. In the next part of this lab we will add code to activate button clicks. 3 In order to make the buttons responsive to clicks, we need to add an ActionListener class to DoorPanel. Use the code below as a guide. { /** Updates the counter and label when the button is pushed. */ public void actionPerformed (ActionEvent event) { // Add your code here repaint(); } } 4
Each time a button is clicked, the actionPerformed method is invoked. We plan to create a single listener object that will listen for both the open and close buttons. Because there are two buttons, the single listener needs to be able to determine which button was clicked by interrogating the ActionEvent object. (Hint: event.getSource returns a reference to the button that was clicked.) If the open button was clicked, we need to invoke open on the door object. If the close button was clicked, we need to invoke close on the door object. setText on the JTextField to capture the most current message inside the door. All of this logic occurs inside the actionPerformed method. ButtonListener class, we need to modify the DoorPanel constructor by creating a ButtonListener object and registering the listener with each button. We register a listener by invoking addActionListener on a button. For example, the following code creates a listener and adds it to a button called openButton. openButton.addActionListener(bl); main inside DoorRunner. 4 And now for something completely different… Write a calculator application. Use a grid layout to arrange buttons for the digits and for the + - × ÷ operations. Add a text field to display the result. !5 ...Read Less
Solution Preview - No Solution Preview Available