Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Monday 4/4
Announcements:
▪ Lab 6 due Today
▪ Homework 8 due Friday, 4/8
▪ Lab 7 posted  - Client View of a Queue
For Next Time
▪ Do Lab 7 Before Lab
▪ Read Section 4.7 – Queue Implementation
Today
▪ Client View of Queue
▪ In-class Exercise
▪ Lab 7 Before Lab
Queue ADT
▪ Queue Abstract Data Type
➢Data: List of elements 
➢Operations:
• Add elements at the rear/back of the queue
– add(elem)     [returns true]
– offer(elem)   [returns true]
• Remove from the front of the queue
– remove()    [throws exception, if empty]
– poll() [returns null, if empty]
• Look at first element, but do not remove
– element( )   [throws exception, if empty]
– peek( ) [returns null, if empty]
▪ First-in, First-out (FIFO)
Applications
▪ Print Queue
▪ Job/Process Queue
➢CPU
➢Web Server 
▪ Simulations of real world events
➢Waiting Lines (Airport ticket line)
➢ Synchronization of Traffic Lights
▪ Graph Algorithms / Game Theory Search Alg.
➢Breadth-first search
Queue Interface
Note: Because the Queue interface extends the 
Collection interface it includes the methods: add, 
iterator, isEmpty, and size
Client View
▪ Assume you have a Queue class that implements the 
PureQueue interface
➢Methods:  add(e), offer(e), poll(), remove(), peek(), 
element(), isEmpty(), size()
▪ Write code to solve these problems
➢Create a Queue of Strings called, myQueue, and Add 
“Barb”, “Mary”, and “Liz” to the Queue
➢Print all the Elements (one per line)  in myQueue, and 
return myQueue to its original state
➢Write a method that is passed Queue of Strings, myQueue,  
and a String, target, and removes all occurrences of target 
from myQueue
8-QueuePractice
▪ There is Queue interface but no Queue class.
➢ How does Java implement a Queue?
➢ LinkedList implements the interface Queue
Queue myQueue = new LinkedList<>();  //ArrayDeque<>()
▪ Method count(myQueue, target): Using size() to examine all elements 
in the Queue and leaving the Queue unchanged.
A B C myQueue
loop size() times
remove/poll
process current
add/offer
B C A myQueue
C A B myQueue
A B C myQueue
In-class Exercise
1. You can work together but each student must write 
their own code
2. Start eclipse and create JavaProject PracticeQueue
3. Add QueuePractice.java to your JavaProject
Lab 7: Queues and Mazes
Before Lab
▪ Create an Eclipse Project for Lab 7 and add files
➢MazeGUI.java
➢Maze.java
➢MazeSolver.java
▪ Download the maze data files maze-17x17.txt, …
▪ Create the class QueueMazeSolver.java as described in 
the lab
▪ Create the class QueueMazeSolverMain.java as 
described in the lab – and run this program
▪ Review Section 4.5 and read the documentation in the 
MazeSolver.java interface
Solving the Maze
Model View Controller (MVC)
▪ Maze classes
➢Maze.java (Model char[ ][ ] maze)
➢MazeGUI.java (View)
➢MazeSolver.java (interface for the Controller)
➢QueueMazeSolver ( solve method is the Controller)
➢QueueMazeSolverMain
• Contains a main method
▪ Execute (run) QueueMazeSolverMain
➢ This will start the GUI program
➢ The Load button will function
➢ The Start Button will not function
Running the Program
▪ Enter a file name
▪ Click  Load button
Running the Program
▪Click on Start button
▪Calls the solve method
▪QueueMazeSolver
Controller
public class QueueMazeSolver implements MazeSolver {
private MazeGUI gui;
public QueueMazeSolver() {
gui = new MazeGUI(this);
}
@Override
public void solve(char[][] maze, int startR, int startC, 
int endR, int endC) {
//code executed when start button is clicked
}
}
Controller & solve() method
public class QueueMazeSolver implements MazeSolver {
...
public void solve(char[][] maze, int startR, int startC, 
int endR, int endC) {
...
}
}
The parameter maze has all of  
the information about the Maze
(Model)
Method solve is called when start 
button is pressed.   
(Controller)
You can use these two methods to update the view
gui.drawMaze(maze)      redraw the maze
gui.setStatusText(“ …”)   update Status
(View – MazeGUI.java)