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 7: Loop Control Structures

small logo

Loop Control Structures

In this lab we focus on the three loop control structures found in Java, the while, for, and do_while loops. We'll use each of these loops to control the actions of the Turtle.

Prerequisite Knowledge

This lab assumes that you have the knowledge to properly write methods and use control statements, as well as the knowledge to control the Turtle's movements.

Materials

Basic RCX Turtle with bump sensors.

Task One

Create a program that will use a while loop to make your robot go in a square pattern using only one forward call and one turn call in the body of the loop.

To do this, create a method called square that has the Turtle travel around in a square. Your method should instantiate two variables; one, called number, to hold the number of times the loop should make a square and one, called counter, that start outs at 0. Then in your while loop, the counter should be incremented every time the loop goes around.

Call the square method from your main method.

Task Two

Now, take your code from Task 1, and modify the square method so that it uses a for loop instead of a while loop.

Task Three

In Task 3, we are going to input a number into our Turtle through the bump sensors, and then the Turtle is going to draw out a square that number of times. You should program your Turtle so that each time you touch your right bump sensor, it increases the number of times that your Turtle will draw out the square. Once you touch the left bump sensor, the Turtle starts to draw out the squares.

Place the code that receives the input in a separate method called getCount, which is called from the main method and returns the number of times the square method should be called. Use a do_while loop in your getCount method.

Place a for loop in the main method which calls square the appropriate number of times.

Task Four

Change your square method into a polygon method, which receives as parameter the number of sides of the polygon and draws out that shape with the Turtle. Otherwise the program remains as in Task 3, with the exception that your main method now calls polygon. Test out your code with the number of sides equal to 6.

Task Five

In Task 5, we are going to modify our getCount method so that it reads in input from the sensors in the form of a 4 bit binary number. In this approach, the left bump sensor will represent a 1, and the right bump sensor will represent a 0. So an input series of left, left, right, right should correspond to the binary number 1100, or the decimal number 12. From there, it should draw out a polygon with the number of sides that we input. It only need to draw it once.

Keep in mind when you input your number that the simplest polygons have at least 3 sides.

An Additional Problem

Design an approach that would allow the user to be able to input the digits of numbers in bases other than binary.

Conclusion

In this lab, you worked with the following:

  • While, for, and do_while loops.
  • Using bump sensors as an input device.
  • Inputting numbers in binary.