Unformatted text preview:

Nested for Statements• The body of a control statement can contain other statements.Such statements are said to be nested.• Many applications require nested for statements. The nextslide, for example, shows a program to display a standardcheckerboard in which the number of rows and number ofcolumns are given by the constants N_ROWS and N_COLUMNS.• The for loops in the Checkerboard program look like this:for (int i = 0; i < N_ROWS; i++) { for (int j = 0; j < N_COLUMNS; j++) { Display the square at row i and column j. }}• Because the entire inner loop runs for each cycle of the outerloop, the program displays N_ROWS x N_COLUMNS squares.The Checkerboard Programpublic void run() { double sqSize = (double) getHeight() / N_ROWS; for (int i = 0; i < N_ROWS; i++) { for (int j = 0; j < N_COLUMNS; j++) { double x = j * sqSize; double y = i * sqSize; GRect sq = new GRect(x, y, sqSize, sqSize); sq.setFilled((i + j) % 2 != 0); add(sq); } }}sqyxjisqSize30.0 0 0 0.0 0.030.011 60.02 210.088210.0Execute theinner loopseven times tocomplete thecheckerboard.Execute thesestatements sixmore times tocomplete therow.public void run() { double sqSize = (double) getHeight() / N_ROWS; for (int i = 0; i < N_ROWS; i++) { for (int j = 0; j < N_COLUMNS; j++) { double x = j * sqSize; double y = i * sqSize; GRect sq = new GRect(x, y, sqSize, sqSize); sq.setFilled((i + j) % 2 != 0); add(sq); } }}sqyxjisqSize30.0 210.088 210.0skip simulationCheckerboardExercise: Triangle Number TableWrite a program that duplicates the sample run shown at thebottom on this slide, which displays the sum of the first Nintegers for each value of N from 1 to 10. As the output suggests,these numbers can be arranged to form a triangle and aretherefore called triangle numbers.TriangleTable1 = 11 + 2 = 31 + 2 + 3 = 61 + 2 + 3 + 4 = 101 + 2 + 3 + 4 + 5 = 151 + 2 + 3 + 4 + 5 + 6 = 211 + 2 + 3 + 4 + 5 + 6 + 7 = 281 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 361 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 451 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55Design Issues: Triangle Number Table• The program involves two nested loops. The outer loop runsthrough each of the values of N from 1 to the maximum; theinner loop prints a series of values on each output line.As you think about the design of the TriangleTable program,it will help to keep the following thoughts in mind:• The individual elements of each output line are easier todisplay if you call print instead of println. The printmethod is similar to println but doesn’t return the cursorposition to the beginning of the next line in the way thatprintln does. Using print therefore makes it possible tostring several output values together on the same line.• The nth output line contains n values before the equal sign butonly n – 1 plus signs. Your program therefore cannot print aplus sign on each cycle of the inner loop but must insteadskip one cycle.Simple Graphical AnimationThe while and for statements make it possible to implementsimple graphical animation. The basic strategy is to create a setof graphical objects and then execute the following loop:for (int i = 0; i < N_STEPS; i++) { update the graphical objects by a small amount pause(PAUSE_TIME);}On each cycle of the loop, this pattern updates each animatedobject by moving it slightly or changing some other property ofthe object, such as its color. Each cycle is called a time step.After each time step, the animation pattern calls pause, whichdelays the program for some number of milliseconds (expressedhere as the constant PAUSE_TIME). Without the call to pause,the program would finish faster than the human eye can follow.for (int i = 0; i < N_STEPS; i++) { update the graphical objects by a small amount pause(PAUSE_TIME);}AnimatedSquareThe AnimatedSquare Programpublic void run() { GRect square = new GRect(0, 0, SQUARE_SIZE, SQUARE_SIZE); square.setFilled(true); square.setFillColor(Color.RED); add(square); double dx = (getWidth() - SQUARE_SIZE) / N_STEPS; double dy = (getHeight() - SQUARE_SIZE) / N_STEPS; for (int i = 0; i < N_STEPS; i++) { square.move(dx, dy); pause(PAUSE_TIME); }}squaredydx3.0 1.7AnimatedSquareskip simulationipublic void run() { GRect square = new GRect(0, 0, SQUARE_SIZE, SQUARE_SIZE); square.setFilled(true); square.setFillColor(Color.RED); add(square); double dx = (getWidth() - SQUARE_SIZE) / N_STEPS; double dy = (getHeight() - SQUARE_SIZE) / N_STEPS; for (int i = 0; i < N_STEPS; i++) { square.move(dx, dy); pause(PAUSE_TIME); }}squaredydx3.0 1.7AnimatedSquarei101Responding to Keyboard Events• The most common key events are:keyPressed( e)keyReleased( e)keyTyped(e)Called when the user presses a keyCalled when the key comes back upCalled when the user types (presses and releases) a keyIn these methods, e is a KeyEvent object, which indicateswhich key is involved along with additional data to recordwhich modifier keys (SHIFT, CTRL, and ALT) were down atthe time of the event.• The general strategy for responding to keyboard events issimilar to that for mouse events, even though the events aredifferent. Once again, you need to take the following steps:1. Call addKeyListeners from the constructor2. Write new definitions of any listener methods you need.Identifying the Key• The process of determining which key generated the eventdepends on the type of key event you are using.• If you are coding the keyTyped method, the usual strategy isto call getKeyChar on the event, which returns the charactergenerated by that key. The getKeyChar method takesaccount of modifier keys, so that typing the a key with theSHIFT key down generates the character 'A'.• When you implement the keyPressed and keyReleasedmethods, you need to call getKeyCode instead. This methodreturns an integer code for one of the keys. A complete list ofthe key codes appears in Figure 10-6 on page 361. Commonexamples include the ENTER key (VK_ENTER), the arrow keys(VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN), and the functionkeys (VK_F1 through VK_F12).Creating a Simple GUI• In addition to mouse and keyboard events, applicationprograms may include a graphical user


View Full Document

VASSAR CMPU 125 - Lecture Notes

Documents in this Course
Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?