LMICSE: Lego Mindstorms in Computer Science Education

Site Map | Contact Us
Project Overview | Staff | Grant Information
Short Workshops | Primary Workshops
CS 1 | Data Str. & Algo. | Prog. Languages | Architecture | Intelligent Sys. | Operating Sys. | Net-centric
Ada | C | C++ | Java | Lisp

Lab 1: Stacks

small logo

Use of Stacks

Stacks are a data structure that is similar to many other collections like lists or arrays but it has a last-in, first-out (LIFO) execution order. Stacks thus work in a slightly different manner than what we have worked with before. Stacks utilize the following commands:

push   - adds an item to the top of the stack
pop    - removes and returns the top item of the stack
peek   - returns the top item without removing it
empty  - returns true if the stack is empty
full   - returns whether the stack has reached its capacity

In this lab we will define and utilize this new structure into our navigation methods to redefine our navigation program.

Prerequisite Knowledge

A basic understanding of how to control the Turtle's movements as well as using data structures and other fundamentals in java.

Basic knowledge of exception handling and the system stack.

Materials

Standard Turtle robot with bump sensors.

Gridded area for testing. See printable example.

Task One

Reimpliment your original navigation lab to utilize a stack of commands instead of the array structure we have used in the past. You will need to design this stack on your own but for simplification you can simply build it up as if it were an array of objects. These objects will need to be of type point for our goTo method to work properly. Your stack should look similar to the UML found later in this lab.

Exceptions: As part of this lab you will need to define two types of exceptions that your stack may possibly throw. The StackOverflowException and StackUnderflowException are two exceptions normally equated for in stacks in case a pop or push occurs when the stack is empty or full respectively. These exceptions should be thrown by your pop, peek, and push methods and handled in your main method.

Then you will create a program that uses the new stack and the Random idea learned in Lab 3 to have your turtle move to a random set of points in random order and then retrace its steps back to the start.

Task Two

Now redesign your navigator class to turn whichever way is the shortest angle to get to the angle the Turtle wishes to be at. An easy way to do this is to take the difference or the combination of the current angle and the desired angle and then mod it by 360 to get an appropriate angle to turn.

UML Class Diagram

Your UML should look similar to:

Additional Problem

How might we go about not moving through actions that might cause the turtle to, after two movements, be right back where it is when it starts the first movement? The peek command we built into our stack might be useful here.

Conclusion

In this lab, you learned the following:

  • How to construct your own stack
  • How to organize using a stack