Home /
Expert Answers /
Computer Science /
sample-implement-stack-and-queue-3-to-implement-a-stack-start-with-drawing-some-examples-draw-a-s-pa690
(Solved): sample implement Stack and Queue 3. To implement a Stack, start with drawing some examples. Draw a S ...
sample
implement Stack and Queue
3. To implement a Stack, start with drawing some examples. Draw a Stack and Queue and see how you can simulate the behavior of a Stack using a queue. I draw an example in the class. a) This class contains two instance variables: one queue to store data and one int variable to store the maximum size of the stack. You may want to look at your notes. b) Create a constructor that takes an int as the maximum size of the stack and initialize the instance variables. c) The Stack class must contain the following methods: - void push(T data) - T pop0 - int getSize0 4. Add a class called student with two instance variables nane and id both with type String. a) Create a constructor that takes two string parameters to initialize name and id of the Student. b) Copy the following code in this class to override the toString method
Cuverride public String toString 0 f return this.name \( + \) " \( \left[+\right. \) this id \( +7^{*} \) : 3 5. Add a class called plate with one boolean instance variable isclean a) Create a constructor that takes a boolean parameter to initialize the instance variable. b) Create a setter method for the isclean variable. 6. Add a class called Cafe with the following private instance variables: a) a Queue to store students' information. b) Two Stacks one to keep the dirty plates and one to keep the clean plates. 7. Add a constructor to the class Cafe with one int parameter called maxCleanPlatesitumber. a) Initialize the queue. Initialize the clean plates with the given parameter and push naxCleanPlatesiturber plates on top of the clean Plates stack. For example, if naxCleanPlatesikuber \( =5 \), it means there are 5 plates on the clean plates stack. b) Initialize the dirty plates stack with the maxCleanplatesNumber parameter. Do not push any plate on top of this stack here as at the very beginning all plates are clean. 8. add the following public methods to the class Cafe: a) void addstudentToLine(Student \( s \) ): add a student to the queue b) int gethuncleanPlates (): returns the number of clean plates c) int getNunoirtyplates (): returns the number of dirty plates d) boolean givePlates (int nunstudents): If the given parameter is greater than the number of clean plates it returns false otherwise it should remove clean plates and add them to the dirty plates stack. For example, if nums tudents \( =5 \), then 5 plates will be removed from the clean plates stack and 5 plates will be added to the dirty plates stack. Keep it in mind that plates must be dirty in order to be added to the dirty plates stack. Note that if the number of students in the queue is less than nunstudents, you should only remove the actual number of only 3 students in the queue, you should remove only 3 clean plates and not 7. 9. Add a class called YourLas thaneAssignnent3 (use your actual last name) that contains the nain method. Inside the main method do the following: a) Show the welcome message (use sample output message) b) Prompt the user for the number of clean plates c) Create an object of the class Cafe with the prompted value d) Prompt the user to choose a number between 1 and 5 .
- If the option is 1, prompt the user to add a student to the queue. Then add the student information to the queue. - If the option is 2, prompt the user for the number of students. Then. Call the givePlates method of the Cafe class to give the plates to the students. If the process is successful, it shows a proper message otherwise an error will be shown (see sample output for the exact message). - If the option is 3 , display number of clean plates. - If the option is 4, display number of dirty plates. - If the option is 5 , exit the program. e) Repeat step 9-d until the user chooses eption 5 . What to turn in. Zip up your Java files and upload it on D2L. Noether file other then a rip. fle will be accepted. Your zip file must contain the following files. - Cafe.java - Plate.java - LinkedList.java - Mode.java - YourlastuaneAssignent3. Java - Student.Java - Queue.Java
Please enter the nuber of Clean plates: 10 Choose an option from 1 to 5 1.Add a student to the Hine 2.6ive clean plates to the students 3. Nuber of clean plictes 4. Nuber of dirty plites 5.Quit 1 Student first and last nase: Robert De wiro Student ID 11111111 Choose an option from 1 to 5 1.Add a student to the line 2.Give clean plates to the students 3. Muber of clean plates 4. Weber of dirty plates 5.Guit