From mkulishe@nmu.edu Thu Oct 30 18:19:19 1997 Return-Path: Received: from VM.NMU.EDU (VM.NMU.EDU [198.110.200.34]) by euclid.acs.nmu.edu (8.8.5/8.8.5) with SMTP id SAA02261 for ; Thu, 30 Oct 1997 18:19:18 -0500 Received: from NMU.EDU by VM.NMU.EDU (IBM VM SMTP V2R3) with BSMTP id 4591; Thu, 30 Oct 97 18:20:48 EST Resent-Message-Id: <30OCT97.19813346.0008.MUSIC@NMU.EDU> Resent-Date: Thu, 30 Oct 1997 18:20:44 EST Resent-From: "Appleton, Randy R" Resent-Reply-To: Meredith Kulisheck Resent-To: Errors-To: NMU Electonic Mail Postmaster <$EMP@nmu.edu> X-Mailer: MUSIC/SP V5.1.0 Received: by NMU.EDU (MUSIC Mailer V5.1.0); Thu, 30 Oct 1997 18:20:42 EST Received: from [] by NMU.EDU (MUSIC Mailer V5.1.0) with POP3 for ; Thu, 30 Oct 1997 18:20:36 EST Message-Id: <3.0.1.32.19971030181944.006a38d0@pop.mail.nmu.edu> X-Sender: mkulishe@pop.mail.nmu.edu X-Mailer: Windows Eudora Pro Version 3.0.1 (32) Date: Thu, 30 Oct 1997 18:19:44 -0500 To: rappleto@nmu.edu From: Meredith Kulisheck Subject: Another test! Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Status: RO X-Status: CS 120, Test 2 NAME 10/31/97 True or False (4 pts each) 1. If an array arr has been declared to have 30 elements of type int, then arr[ 12 ] is the twelfth element of this array. 2. If an array arr has been declared to have 30 elements of type int, and the value stored in arr[ i] is 12, then i is 12. 3. If an array arr has been declared to have 30 elements of type int, then the following for loop would print the positions of all the positive elements in the array.. for ( int i = 0; i <= 30; ++i ) if ( arr[ i ] > 0 ) g.drawString( "" + i, 30, 30 + i * 15 ); 4. An array can store several types of values. 5. It is equally acceptable to refer to arr[ 3 ] and arr[ 3. ] (if arr is a sufficiently large array). For #6, #7, and #8, suppose that a method begins public int testMethod( int a, double b ) { 6. If a and b are of type int and c is of type double, this method could be called with a = testMethod( b, c ); 7. testMethod ends with either return true; or return false; 8. There might be more than one return statement in testMethod. Give the output for the following program (30 pts): import java.awt.*; import java.applet.Applet; public class Test2P3 extends BackgroundApplet { int arr [ ]; public void init () { arr = new int [ 10 ]; } public void paint ( Graphics g ) { arr[ 0 ] = 1; arr[ 1 ] = 1; for ( int i = 2; i < 10; ++i ) arr[ i ] = arr[ i - 1 ] + arr[ i - 2 ]; g.setColor( Color.black ); for ( int k = 0; k < 10; ++k ) g.drawString( "" + arr[ k ], 30, 30 + 15 * k ) } } Complete the following program as directed. (38 pts) import java.awt.*; import java.applet.Applet; public class Test2P3 extends BackgroundApplet { // Declare count and max to be of type int, and names to be an array of type String. public void init () { // Give count an initial value of 0 and max to have an initial value of 200. (This is more than will be needed.) Allocate memory for names to have max elements. */ requestFocus(); } public boolean keyDown ( Event e, int key ) { /* Write the code so that stringDialog is called if the space bar is pressed and if there are fewer than max names in the array. The message should tell the user to enter a name or xxx to quit. */ return true; } public void paint ( Graphics g ) { g.setColor( Color.red ); g.drawString( "Press the Space Bar to enter a name.", 30, 15 ); g.setColor( Color.black ); // Write a for loop to print all elements in the array. } // End of paint public boolean dialogResponse( boolean cancelled, String response ) { if ( ! cancelled ) { if ( response.equals( "xxx" ) ) // User typed xxx (to quit) // Call the method reverse. else // Put response in the count position of names and increment count. return true; } public void reverse( ) { /* Write this method to reverse the order of the names in the array. */ } }