The idea for this lab was kindly suggested by Alex Leighton, a current CS major (thanks, Alex!), based on a project he did earlier in his schooling (on a calculator, no less!). The lab is meant to give you some further experience with loops and arrays, some first lab experience with scanner-based input, and also to give you some more freedom (and responsibility) for overall program and class design.
The basic idea behind the lab is to draw certain specific patterns of lines under user control. The user inputs a series of numbers (let's assume they are non-negative integers) via a scanner input box. After the user hits "enter", the program will use the integers to draw a pattern of lines in a draeing panel as follows: each integer is interpreted as the length of a line segment to be drawn from the current position, but with successive "right-hand" turns after each segment is drawn.
To get a better idea of the idea, imagine that you are standing on the screen yourself, prepared to do the drawing. You begin facing toward the right ("east"): after drawing the first segment, you turn right (clockwise when viewed from above), so now you're facing down ("south"). Then you draw another line segment, and now turn to face "west". You repeat the process of drawing a segment and turning 90 degrees for each of the integers you read in; when you reach the end of the list, you continue again from the front, re-using the numbers as you go.
(The idea of imagining yourself, or some other being, as doing the drawing under various commands was popularized in the Logo language, which was developed for teaching children. The Logo version used a turtle (its tail raised and lowered the pen to the paper), so these are often called "turtle graphics". Typical turtle commands include movement, rotation, and raising or lowering a pen chosen from several different colors or line styles. Our "turtle" is pretty limited, in that it only draws and turns 90 degrees, and always in that order.)
Your job is to write a program which will do this sort of drawing, based on the numbers entered into scanner input. You should repeatedly read in numbers, redrawing the line pattern each time, until the user enters an empty input. This will allow the user to modify the numbers to see the effect on the pattern drawn.
Most of the programming we have done so far has been either simple class-less
expression evaluation in the interactions pane or class-based programming.
Lately, we have been "driving" the overall program through a
method in a
Main class. This lab can be approached in a couple of ways:
hasNextInt()method. You can store the numbers in an array, but how do you know how big to make it? (Hint: you may assume there will never be more than some default maximum: remember to tell the user what the maximum is with your input prompt!)
Integerclasses. If you have a string of input (say from a user's scanner input), you can split it up into an array of "words" as follows:
Then any given "word" (in this case it will be a numeral, i.e., a string representing some integer) can be converted into a number as follws:
String words = myString.split(" +");
This code could be wrapped in a loop in order to convert every numeral in an input string into a number in an array. If you use this approach, you don't need to worry about a maximum size of input, since the
... Integer.parseInt(words[i]) ...
split()method will size the array for you.
%in Java) in a couple of places in your code: first, you can use the current "frame" number (in your big drawing loop) modulo the number 4 to tell you which direction you're currently facing. (If you use a class-based approach, you won't have a loop number, but you can increment your current "direction" by one, then use the modulus to convert.) In any case, any number "modulo" four will always give you a number between 0 and 3, which you can use to represent one of the four directions. You can also use modulus to access the array of numbers: recall that you will run your overall loop for a large number of repetitions, but your array of numbers will generally be rather small (say 20 or so). By using the loop number modulo the array length, you can ensure that you have a valid index into the array, and that you properly re-use the numbers from the user input (array).
setText()method (this just makes for a convenient way to remember and re-enter the numbers, without having to memorize them or write them down);