Lesson Plan

advertisement
Lesson Plan
Course Title: Advanced Computer Programming
Session Title: Data Structures
Lesson Duration: Weeks/Days/Hours/Minutes
Should take between 4-8 days. (Typing speed seems to be most significant criteria)
Performance Objective:
Upon completion of this assignment, the student will be able to:
 know when it is appropriate to use a data structure,
 select an appropriate data structure; and
 correctly implement an appropriate data structure.
Specific Objectives:
 The student will be able to identify the key terms, functions, benefits, and consequences
of each of the data structures (ArrayList, Stack, and Queue).
 The student will be able to implement a program applying the data structures (ArrayList,
Stack, and Queue).
 The student will understand the requirements and syntax for using each of the data
structures (ArrayList, Stack, and Queue).
 The student will understand when it is appropriate to utilize each of the data structures.
Preparation
TEKS Correlations: 130.277 (c)









3A – identify input and output requirements
4A – identify key functions and subsystem capabilities of modern software products
5C – apply language specific programming techniques
5D – develop programs using appropriate language
7F – explain algorithmic and data structure concepts
7M – employ interface techniques
8A – apply programming language concepts
8C – articulate the concept of data representation
8G – translate data structures and program design into code in an appropriate language
Instructor/Trainer
References:
 Drake, Peter. Data Structures and Algorithms in Java. Upper Saddle River, NJ:
Pearson/Prentice Hall, 2006. Print.
 Horstmann, Cay S. Big Java. Hoboken, NJ: Wiley, 2010. Print.
 Hubbard, John Rast, and Anita Huray. Data Structures with Java. Upper Saddle River,
NJ: Pearson Prentice Hall, 2004. Print.
 Java API. Program documentation. Vers. 1.6. Web.
<http://download.oracle.com/javase/6/docs/api>.
 Rosetta Code. Web. 26 May 2011. <http://rosettacode.org/>.
 "Dictionary of Algorithms and Data Structures." Web. 26 May 2011.
<http://xlinux.nist.gov/dads/>.


HP CodeWars - Home. Web. 26 May 2011. <http://hpcodewars.org/>.
Content developer Knowledge
Instructional Aids:
Data Structures Presentation
Data Structures Collections Presentation
Labs (KEYS are provided for PreLabs)
PreLab1 and Lab 1 Handout (ArrayList)
PreLab2 and Lab 2 Handout (Stack)
Student Files
Stars.java (Lab 1)
Maze.java (Lab 2)
Solution Files
Stars.java (Lab 1)
Maze.java (Lab 2)
Quizzes (Quiz and Key provided for each)
Data Structures Basics Quiz
Data Structures Collections Quiz
Example Source Code
CollectionTest.java (example of how each of the Collections work)
Polynomial.java (example of using an ArrayList to represent Polynomials)
DelimiterMatcher.java (a production example of one way to use a stack)
StoreSim.java (a production example of using a Queue for a store simulation)
Materials Needed:
JavaDocs (either through internet access or saved to student’s workstation)
Copies of PreLab1, PreLab2, PreLab3
Copies of Lab1, Lab2 and Lab3.
Copies of Basics Quiz, Collections Quiz
Copies of example source code (having the students type in the documents is preferred)
CollectionTest.java
DelimiterMatcher.java
Student Files should be loaded on student computers, or provided in some manner.
Equipment Needed:
Each student will require a computer with a java compiler with a java JDK version of 1.5 or
newer.
Learner




Students should have observation journals to record common errors and other
observations
Students should be comfortable with the concept of Objects and primitive data types and
how they differ
Prior to lesson, students should look at the JavaDocs for ArrayList, Stack and Queue
Prior to attempting either of the prelabs, students should look over the Java
Documentation for java.awt.Point and be very familiar with it.
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
2
Introduction
MI
Introduction (LSI Quadrant I):
The instructor should present the students with the following problem:
You have been asked to write a program to keep track of the products in a store.
The inventory should keep track of prices, inventory numbers and the description of
each product as well as the quantity of product stocked. Discuss how you would
design a program to keep track of so many different pieces of data and the different
ways that you might want to access that information.
Outline
MI
Outline (LSI Quadrant II):
Instructor Notes:
I. Data Structures: (an overview of terms and
ideas)
a. Why you should use data structures?
b. Collections
c. Purpose of Data Structures
d. Common Data Structures
e. Basic Terms
This is only a surface level
overview of data structures.
This lesson only covers how
to use the data structures
built into the language library;
there is no in-depth coverage
of how they work.
II. Collections
a. Types
i. ArrayList
ii. Stack
iii. Queue (LinkedList)
b. When to use
c. Pros and Cons
d. Usage (declaration and basic
functions)
It is recommended that the
students look over the
example source code
“CollectionsTest” and
manually type in the code.
This will help them learn what
kinds of mistakes they will
make when typing in this type
of code.
III. Implementation
a. How to implement and use an
ArrayList
b. How to implement and use a Stack
c. How to implement and use a Queue
There is a lab for each of the
Data Structures as well as a
complete example program
for Stacks and Queues.
Students should examine the
examples prior to attempting
labs.
Labs are intended to be
executed in this manner:
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
3
* The students complete the
PreLab assignment in class.
For the next class meeting,
they receive a corrected
PreLab so that they will be
ready for the lab.
* The student will then
receive a copy of the Lab
Document and the student
file for the Lab to complete
the assignment.
When completing labs and/or
case studies, students should
keep a written journal of
errors and problems they
encounter along with
observations they make of
their code executing.
There is no lab for Queues
as they are similar enough to
ArrayLists that they shouldn’t
require extra practice.
Application
MI
Guided Practice (LSI Quadrant III):
The example code files are intended to be read by the students to get examples of
how to use the code. There is no assignment to change or modify, but to read and
ask questions. It is strongly encouraged that the students enter the code and
observe the results. Seeing working code can greatly increase their success in
understanding future code.
Suggestion: Ask students to comment about the examples to see if they can
understand what is going on. Sometimes they can and sometimes they can't, but in
the end, they get a deeper understanding of what is going on.
Students should enter the example code, run it and record observations about what
the code does. If students are using an IDE that allows line by line debugging (such
as BlueJ or Eclipse), it would be a good idea to trace the code, line by line. (Please
see the documentation for your IDE on how to do this).
MI
Independent Practice (LSI Quadrant III):
Following the presentation, students should be provided with the labs to complete on
their own.
Summary
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
4
MI
Review (LSI Quadrants I and IV):
Students will incorporate the concepts introduced by completing the hands on labs.
It is also recommended that students keep an observation journal to record
observations of common errors they encounter and what results they get.
Evaluation
MI
Informal Assessment (LSI Quadrant III):
There are periodic questions in the presentations that the instructor can use to
gauge some level of understanding.
The instructor may present students with a scenario. In small groups, have the
students determine which is the most appropriate data structure to represent the
scenario and explain why they chose it.
The teacher may also use the PreLabs to assess whether or not the students are
progressing well. The PreLabs are important in determining whether or not the
students are ready to complete the labs on their own.
MI
Formal Assessment (LSI Quadrant III, IV):
Instructor may use any or all of the following instruments for formal assessment:

Basics Quiz

Collections Quiz

ArrayList

LabStack Lab
Extension
MI
Extension/Enrichment (LSI Quadrant IV):
Students interested in knowing the deeper functions of how the data structures are
built and how they work may want to implement the data structures themselves.
Stacks and Queues are the simplest to implement, Linked Lists the next most
difficult with Trees and Hashtables being the most advanced.
Students should be encouraged to look at contest problems, such as those from the
ACM, TCEA, HP Codewars, or other programming competitions, and determine how
these data structures could simplify the solutions to those problems.
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
5
Icon
MI
Verbal/
Linguistic
Logical/
Mathematical
Visual/Spatial
Musical/
Rhythmic
Bodily/
Kinesthetic
Intrapersonal
Interpersonal
Naturalist
Existentialist
Teaching Strategies
Personal Development
Strategies
Lecture, discussion, journal
writing, cooperative learning,
word origins
Reading, highlighting, outlining,
teaching others, reciting information
Problem solving, number
games, critical thinking,
classifying and organizing,
Socratic questioning
Mind-mapping, reflective
time, graphic organizers,
color-coding systems,
drawings, designs, video,
DVD, charts, maps
Use music, compose songs
or raps, use musical
language or metaphors
Organizing material logically, explaining
things sequentially, finding patterns,
developing systems, outlining, charting,
graphing, analyzing information
Developing graphic organizers, mindmapping, charting, graphing, organizing
with color, mental imagery (drawing in
the mind’s eye)
Use manipulatives, hand
signals, pantomime, real life
situations, puzzles and board
games, activities, roleplaying, action problems
Reflective teaching,
interviews, reflective listening,
KWL charts
Cooperative learning, roleplaying, group brainstorming,
cross-cultural interactions
Natural objects as
manipulatives and as
background for learning
Socratic questions, real life
situations, global
problems/questions
Creating rhythms out of words, creating
rhythms with instruments, playing an
instrument, putting words to existing
songs
Moving while learning, pacing while
reciting, acting out scripts of material,
designing games, moving fingers under
words while reading
Reflecting on personal meaning of
information, studying in quiet settings,
imagining experiments, visualizing
information, journaling
Studying in a group, discussing
information, using flash cards with
other, teaching others
Connecting with nature, forming study
groups with like minded people
Considering personal relationship to
larger context
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
6
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – PreLab 1
In this lab, you are going to create a “star field simulation.”
When you are done, it will look something like the figure below.
In order to do this, we need to consider the design of such a program.
To create this program, you will need:
A Window to draw to (this will be provided to you)
A way to draw to the screen and animate it (this will be provided to you)
A collection of Stars (you will create this)
An algorithm to move the stars (you will create this)
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
7
Before you get started, you need to know something about how computer graphics are
generated. The computer uses a coordinate system much like the Cartesian Coordinate system
you use in algebra. The only thing is, the y axis is topsy-turvy.
As you go down the y axis, you would expect the numbers
to get smaller, but in fact, they will get larger, as shown on
the left.
The x axis works as you would expect it to: the values
increase as you go to the right, and decrease as you go left.
Also, the top left of the screen (your window) is at the
location ( 0, 0). That means that if you have negative
values, they will not show up in the window. In addition, if
your x value exceeds the width you have provided, or the y
value exceeds the height you have provided, then the
drawing at those points will not be displayed either. So you
need to be careful to check your numbers to ensure that
they are within the boundaries of where you can draw.
One last quirk -- we are drawing directly to the window, and
the window is drawing its own title bar. That means we can’t see what is drawn in the area
where the title bar is. This area is about 40 pixels tall and the entire width of the window on
most systems. (Just in case you wanted to draw spaceships or something…this should prevent
any surprises…)
Before you get started, answer these questions:
1. The stars are going to be stored as Points from the java.awt.Point class. The Point
class offers two variables for you to use, x and y, which are used like this:
Point p = new Point(0,0); // x, y
p.x = 20;
p.y = 40;
This creates a point whose x is 20 and y is 40. You can access the x and y as p.x and
p.y
Given this information, what is the code to move the point DOWN the screen 2 pixels?
(hint: you may want to use the += operator)
2. We want to store the stars as a collection of Points. We don’t’ care in which order the
stars are processed, but we do need to access all of the points multiple times (to draw
them and to move them). Which data structure do you think would be most appropriate
in which to store the collection of Points? (Circle your answer)
ArrayList
Stack
Queue
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
8
3. What is the code to declare and initialize the appropriate data structure to contain
elements of the Point class with a variable name of stars?
4. In order to draw each of the points to the screen, you will need to access each of the
points from the collection. If the function to draw a point looks like this:
Point p = new Point(40,80);
g.drawLine(p.x,p.y);
What would the for each loop look like to draw all the points in the collection?
5. From question 1, you determined how to move a point down 2 spaces on the screen.
What would the for each loop look like to move each point in the collection this way. Also, in
the loop, you should check to see if the point is beyond the bottom of the screen and move it
back to location 0. The instruction that will give you the location of the bottom of the screen
is getHeight().
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
9
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – PreLab 1 KEY
In this lab, you are going to create a “star field simulation”.
When you are done, it will look something like the figure below.
In order to do this, we need to consider the design of such a program.
To create this program, you would need:
A Window to draw to (this will be provided to you)
A way to draw to the screen and animate it (this will be provided to you)
A collection of Stars (you will create this)
An algorithm to move the stars (you will create this)
Before you get started, you need to know something about how computer graphics are
generated. The computer uses a coordinate system much like the Cartesian Coordinate system
you use in algebra. The only thing is, the y axis is topsy-turvy.
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
10
As you go down the y axis, you would expect the numbers
to get smaller, but in fact, they will get larger, as shown on
the left.
The x axis works as you would expect it to: the values
increase as you go to the right, and decrease as you go left.
Also, the top left of the screen (your window) is at the
location ( 0, 0). That means that if you have negative
values, they will not show up in the window. In addition, if
your x value exceeds the width you have provided, or the y
value exceeds the height you have provided, then the
drawing at those points will not be displayed either. So you
need to be careful to check your numbers to ensure that
they are within the boundaries of where you can draw.
One last quirk -- we are drawing directly to the window, and
the window is drawing its own title bar. That means we can’t see what is drawn in the area
where the title bar is. This area is about 40 pixels tall and the entire width of the window on
most systems. (Just in case you wanted to draw spaceships or something…this should prevent
any surprises…)
Before you get started, answer these questions:
1. The stars are going to be stored as Points from the java.awt.Point class. The Point
class offers two variables for you to use, x and y, which are used like this:
Point p = new Point(0,0); // x, y
p.x = 20;
p.y = 40;
This creates a point whose x is 20 and y is 40. You can access the x and y as p.x and
p.y
Given this information, what is the code to move the point DOWN the screen 2 pixels?
p.y += 2;
// remember that to move DOWN you need to ADD
2. We want to store the stars as a collection of Points. We don’t care in which order the
stars are processed, but we do need to access all of the points multiple times (to draw
them and to move them). Which data structure do you think would be most appropriate
in which to store the collection of Points? (Circle your answer)
ArrayList
Stack
Queue
3. What is the code to declare and initialize the appropriate data structure to contain
elements of the Point class with a variable name of stars?
ArrayList<Point> stars = new ArrayList<Point>();
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
11
4. In order to draw each of the points to the screen, you will need to access each of the
points from the collection. If the function to draw a point looks like this:
Point p = new Point(40,80);
g.drawLine(p.x,p.y);
What would the for each loop look like to draw all the points in the collection?
for(Point p : stars)
{
g.drawLine(p.x,p.y);
}
5. From question 1, you determined how to move a point down 2 spaces on the screen.
What would for each loop look like to move each point in the collection this way. Also, in
the loop, you should check to see if the point is beyond the bottom of the screen and
move it back to location 0. The instruction that will give you the location of the bottom of
the screen is getHeight().
for(Point p : stars)
{
p.y += 2;
if(p.y > getHeight())
{
p.y = 0;
}
}
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
12
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – Lab 1: ArrayList
To complete this lab, you should have already completed PreLab 1. The answers from the
PreLab will guide you in completing this assignment. Check with your instructor to make sure
your prelab answers are correct before moving on.
The next thing you will need is the starter source code Stars.java. If you do not have this file,
ask your instructor how to obtain it.
Before you begin modifying the Stars program read the source code from beginning to end. It is
okay if you don’t understand what all of the code means. For example, you don’t need to know
how the drawFrame() function works. You just need to know that it does work. As you are
reading through the code, pay close attention to the comments in the file. Most of the
comments are instructions for how you are to modify the file to complete this lab. Three of the
comments ask you to remove the line. These lines are to demonstrate the basic function of the
program and won’t be needed when finished.
After you have read the code, run the program. You should get a window with a dark
background and one lonely white star. You may have to search for the star because it is so tiny,
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
13
but it is there. If you can’t see the star, try running the program again as the star will move each
time you run the program. The goal of this lab is to extend the program to put more stars on the
screen and then ultimately move them like you are travelling in space.
Implement the following stages:
Stage 1:
a. At the top of the code, there is a library needed for your chosen collection. In your
prelab, you should have noted that the ArrayList is the most appropriate data structure to
use. Import the necessary library to include an ArrayList in this project.
b. Find the class variables. They look like this:
private int windowWidth = 800;
private int windowHeight = 600;
// declare a collection named stars that contains Point objects
private static final int NUMSTARS = 100;
private Point star; // remove this line
Remove the last variable as instructed. On the line that asks you to declare a collection,
declare an ArrayList named stars that stores elements of the java.awt.Point class, or
simply Point.
Notice the variable NUMSTARS = 100, this variable tells us that we will be drawing 100
stars. This is the variable to change if you want more or fewer stars.
c. Find the function named initialize().
// initialize the collection of stars
// for the number of stars you want 0 -> numstars
// generate a random x and y postion within
// the boundaries of the window
int x = (int) (Math.random()*getWidth());
int y = (int) (Math.random()*getHeight());
// create a new Point with that x and y
Point p = new Point(x,y);
star = p; // remove this line
// add the new Point to the collection using x and y
The first line in the function asks you to initialize the collection of stars. Initialize the
ArrayList stars to contain Point.
remember, declaration is when you determine the variable type:
int i; or String str;
Initialization is when you provide a value:
i = 15; or str = “hello world”;
Next you want to create a for loop that counts from 0 to the number of stars. Use the
variable NUMSTARS. It should look something like
for(int i=0;i<NUMSTARS;i++)
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
14
The next three comments are informational comments. The x and y points have already
been randomly selected for you. The x and y selection should be inside the for loop you
just created as well as creating the Point p.
Remove the line star = p; as instructed in the code.
The last thing that should be included inside the for loop is to add the point p just created
to your ArrayList stars.
d. Locate the function drawStars(Graphics g).
private void drawStars (Graphics g) {
g.setColor(Color.WHITE);
// the for each look for drawing the stars goes here
g.drawLine(star.x,star.y,star.x,star.y);
// remove or replace this function
}
After the color has been set, place your for each loop for drawing each of the stars in the
ArrayList. You may either convert the drawLine function here into your for loop, or
remove it if you’ve typed your drawLine statement.
e. Compile the code. Note any errors that occurred into your journal. Fix your error; try it
yourself first, then ask a neighbor. If neither of you can fix it, then ask your instructor.
Write down how you fixed your errors in your journal.
f.
Once your code compiles, run it. You should see stars on the screen like in the picture
in this document. If you do not, go back through these instructions and see what you
might have done wrong. Note any errors and corrections in your journal. Next is to get
the stars to move!
Stage 2:
a. Find the update() function.
private void update() {
// the code for moving the stars goes here
drawFrame();
}
Before the call to drawFrame(), insert the code you created in your prelab for moving the
point down two pixels and adjusting for the bottom of the screen.
To recall, you need a for each loop to go through each Point in stars, add 2 to p.y. If
p.y is greater than the bottom of the screen (obtained through getHeight()), reset p.y to
0.
b. Compile the code. Note any errors that occurred into your journal. Fix your error, try
yourself first, then ask a neighbor. If neither of you can fix it, then ask your instructor.
Write down how you fixed your errors in your journal.
c. Once your code compiles, run it. You should see stars on the screen like in the picture
in this document, but this time, they are moving! If you do not, go back through these
instructions and see what you might have done wrong. Note any errors and corrections
in your journal.
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
15
d. See if you can change the direction the stars are moving. Make notes in your journal
about what these changes are and how they affected the movement of the stars. See if
you can make the stars move faster, or slower.
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
16
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – PreLab 2
In this lab, your task is to solve a maze.
To solve the maze, you are going to try and travel down the corridors until you can’t travel any
further. Along the way, you will need to keep track of where you’ve been before so that you can
backtrack and try different routes. You are to keep trying and backtracking until either you have
tried all passages, in which case there is no path to the exit, or you are at the exit and you can
display the solution.
Before you get started, answer these questions:
1. Given that you want to track where you’ve been, and backtrack to Points previously
visited, which of the following data structures would be the best choice for storing the
points you have already visited? (Circle the Answer that Best Fits)
ArrayList
Stack
Queue
2. To track your current location, and locations you have previously been, you will use the
Point class from the java.awt Library. What is the appropriate code to declare and
instantiate the data structure to use Points with a variable name of history?
3. What are the appropriate functions/methods that you need to use to insert and retrieve
the elements in the appropriate data type?
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
17
4. What is the code to determine if there are no more elements left in the appropriate data
type?
5. If you had a Point called current and you data structure called history, what is the code
to store current in history, given that history is the appropriate data structure?
6. Given the situation in question 5, assume you wanted to do the reverse and retrieve the
last item stored in history and replace the value in current with the value from history,
what would be the appropriate code?
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
18
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – PreLab 2 KEY
In this lab, your task is to solve a maze.
To solve the maze, you are going to try and travel down the
corridors until you can’t travel any further. Along the way, you will need to keep track of where
you’ve been before so that you can backtrack and try different routes. You are to keep trying
and backtracking until either you have tried all passages, in which case there is no path to the
exit, or you are at the exit and you can display the solution.
Before you get started, answer these questions:
1. Given that you want to track where you’ve been and backtrack to Points previously
visited, which of the following data structures would be the best choice for storing the
points you have already visited? (Circle the Answer that Best Fits)
ArrayList
Stack
Queue
2. To track your current location and locations you have previously been, you will use the
Point class from the java.awt Library. What is the appropriate code to declare and
instantiate the data structure to use Points with a variable name of history?
Stack<Point> history = new Stack<Point>()
// Or
Stack<Point> history;
History = new Stack<Point>();
// the second way is preferred
3. What are the appropriate functions/methods that you need to use to insert and retrieve
the elements in the appropriate data type?
history.push();
history.pop();
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
19
4. What is the code to determine if there are no more elements left in the appropriate data
type?
history.isEmpty(); // will return true if empty
5. If you had a Point called current and you data structure called history, what is the code
to store current in history, given that history is the appropriate data structure?
history.push(current);
6. Given the situation in question 5, assume you wanted to do the reverse and retrieve the
last item stored in history and replace the value in current with the value from history,
what would be the appropriate code?
current = history.pop();
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
20
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – Lab 2: Stack
In this lab, your task is to solve a maze.
To solve the maze, you are going to try and travel down the corridors until you can’t travel any
further. Along the way, you will need to keep track of where you’ve been before so that you can
backtrack and try different routes. You keep trying and backtracking until either you have tried
all passages, in which case there is no path to the exit, or you are at the exit and you can
display the solution.
To complete this lab, you should have already completed PreLab 2. The answers from the
PreLab will guide you in completing this assignment. Check with your instructor to make sure
your prelab answers are correct before moving on.
The next thing you will need is the starter source code Maze.java. If you do not have this file,
ask your instructor how to obtain it.
Before you begin modifying the Maze program read the source code from beginning to end. As
you are reading through the code, pay close attention to the comments in the file. Most of the
comments are instructions for how you are to modify the file to complete this lab. There is one
line that asks you to uncomment the line. It was commented so that you could see the program
run.
After you have read the code, run the program. You should get a console window with a Text
version of a maze. The goal of this lab is to extend the program to generate a solution path for
that maze.
Complete the following steps to complete the maze:
1. At the top, import the Stack class library.
2. Find the comment “// <insert code here 1>”. Replace the comment with a declaration of
a stack called history that stores Point objects.
3. In the Maze constructor, find the comment “// <insert code here 2>”. Replace this
comment with the code to initialize the value of the stack to a new stack that can store
Point objects. Remember that the stack is called history.
4. Find the comment “// <insert code here 3>”. Replace this comment with code that stores
the point last in the stack history. It’s important to store last instead of current because
last is a copy of current and will prevent changes to the points stored in the history.
5. Find the comment “// <insert code here 4>”. Replace this comment with the code that
asks the stack history if it is empty, if it is empty, you should return the value false. You
may simply uncomment the line afterwards for the return statement or remove it if you
have supplied your own return statement.
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
21
6. Find the comment “// <insert code here 5>”. Replace the comment with the code that
will retrieve the last point from the history stack then store that point in the current
point.
7. Compile the code. Note any errors that occurred into your journal. Fix your error, try
yourself first, then ask a neighbor. If neither of you can fix it, then ask your instructor.
Write down how you fixed your errors in your journal.
8. Once your code compiles, run it. You should see a solution path for the maze on the
screen. If you do not, go back through these instructions and see what you might have
done wrong. Note any errors and corrections in your journal.
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
22
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – Basics Quiz
1. What can a Collection simulate?
a. Random collections like a herd of cattle
b. Piles of things like sticky notes on top of each other
c. Orders waiting to be processed
d. All of the Above
2. Which of the following is NOT a purpose of data structures?
a. Increase speed of data usage
b. Increase efficiency of data usage
c. Minimizing memory requirements
d. None of the Above
3. Which of the following is NOT a common data structure?
a. Box
b. Stack
c. Queue
d. None of the Above
4. What structure does Java use to implement a Queue?
a. ArrayList
b. Stack
c. LinkedList
d. None of the Above
5. What does an iterator do?
a. Creates a new iteration of your data structure
b. Allows you to look at each element in a data structure
c. Separates elements of the data structure
d. None of the Above
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
23
6. What are generics for?
a. Allows data structures to determine the type of object it stores when it is
created
b. Allows data structures to be used in any kind of program
c. Determines the age of a data structure
d. None of the Above
7. What does auto-boxing do?
a. Automatically creates a collection
b. Automatically converts between objects and primitive data types
c. Automatically stores values in boxes
d. None of the Above
8. What type of Object would you need to declare to store a char in a Collection?
a. char
b. Character
c. String
d. None of the Above
9. What type of loop uses iterators to function?
a. do … while loop
b. while loop
c. for each loop
d. None of the Above
10. Which of the following data types would NOT require auto-boxing?
a. int
b. float
c. String
d. None of the Above
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
24
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – Basics Quiz KEY
1. What can a Collection simulate?
a. Random collections like a herd of cattle
b. Piles of things like sticky notes on top of each other
c. Orders waiting to be processed
d. All of the Above
2. Which of the following is NOT a purpose of data structures?
a. Increase speed of data usage
b. Increase efficiency of data usage
c. Minimizing memory requirements
d. None of the Above
3. Which of the following is NOT a common data structure?
a. Box
b. Stack
c. Queue
d. None of the Above
4. What structure does Java use to implement a Queue?
a. ArrayList
b. Stack
c. LinkedList
d. None of the Above
5. What does an iterator do?
a. Creates a new iteration of your data structure
b. Allows you to look at each element in a data structure
c. Separates elements of the data structure
d. None of the Above
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
25
6. What are generics for?
a. Allows data structures to determine the type of object it stores when
it is created
b. Allows data structures to be used in any kind of program
c. Determines the age of a data structure
d. None of the Above
7. What does auto-boxing do?
a. Automatically creates a collection
b. Automatically converts between objects and primitive data types
c. Automatically stores values in boxes
d. None of the Above
8. What type of Object would you need to declare to store a char in a Collection?
a. char
b. Character
c. String
d. None of the Above
9. What type of loop uses iterators to function?
a. do … while loop
b. while loop
c. for each loop
d. None of the Above
10. Which of the following data types would NOT require auto-boxing?
a. int
b. float
c. String
d. None of the Above
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
26
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – Collections Quiz
1. Which type of data structure simulates an unordered group of elements?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
2. Which type of data structure simulates elements piled one upon another?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
3. Which type of data structure simulates elements to be processed in sequence?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
4. What does it mean for a data structure to be dynamic?
a. It is easy to use
b. It is fantastic
c. It is resizable
d. It can be used for many different things
5. What does it mean for a data structure to have random access?
a. It will access random elements
b. It can access requested elements
c. It will access sometimes, but not others
d. None of the Above
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
27
6. What function would you use on an ArrayList, Stack or LinkedList to remove all
the elements from the collection at once?
a. remove()
b. isEmpty()
c. clear()
d. None of the Above
7. Which type of data structure uses the LIFO access?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
8. Which type of data structure uses the FIFO access?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
9. If you wanted to write a “smart” program for a robot to remember things it has
done recently, which data structure would be the best fit for modeling the robot’s
memory?
a. ArrayList
b. Stack
c. Queue
d. LinkedList
10. If you wanted to write a “smart” program for a robot to have it process
instructions you give it, in the order you gave the instructions, which data
structure would be the best fit for modeling the instruction sequencing?
a. ArrayList
b. Stack
c. Queue
d. LinkedList
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
28
NAME:________________________________ DATE: _______ PERIOD: _______
Data Structures – Collections Quiz KEY
1. Which type of data structure simulates an unordered group of elements?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
2. Which type of data structure simulates elements piled one upon another?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
3. Which type of data structure simulates elements to be processed in sequence?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
4. What does it mean for a data structure to be dynamic?
a. It is easy to use
b. It is fantastic
c. It is resizable
d. It can be used for many different things
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
29
5. What does it mean for a data structure to have random access?
a. It will access random elements
b. It can access requested elements
c. It will access sometimes, but not others
d. None of the Above
6. What function would you use on an ArrayList, Stack or LinkedList to remove all
the elements from the collection at once?
a. remove()
b. isEmpty()
c. clear()
d. None of the Above
7. Which type of data structure uses the LIFO access?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
8. Which type of data structure uses the FIFO access?
a. ArrayList
b. Stack
c. Queue
d. None of the Above
9. If you wanted to write a “smart” program for a robot to remember things it has
done recently, which data structure would be the best fit for modeling the robot’s
memory?
a. ArrayList
b. Stack
c. Queue
d. LinkedList
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
30
10. If you wanted to write a “smart” program for a robot to have it process
instructions you give it, in the order you gave the instructions, which data
structure would be the best fit for modeling the instruction sequencing?
a. ArrayList
b. Stack
c. Queue
d. LinkedList
IT: Advanced Computer Programming: Data Structures Lesson Plan
Copyright © Texas Education Agency, 2013
31
Download