312 Methods Chapter 6 6.41 Exercise 6.31 through (Personal web server)

312 Methods Chapter 6 6.41 Exercise 6.31 through Exercise 6.33 developed a computer-assisted instruction program to teach an elementary school student multiplication. This exercise suggests enhancements to that program. a) Modify the program to allow the user to enter a grade-level capability. A grade level of 1 means to use only single-digit numbers in the problems, a grade level of 2 means to use numbers as large as two digits, etc. b) Modify the program to allow the user to pick the type of arithmetic problems he or she wishes to study. An option of 1 means addition problems only, 2 means subtraction problems only, 3 means multiplication problems only, 4 means division problems only and 5 means to intermix randomly problems of all these types. 6.42 Write method distance, to calculate the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an applet that enables the user to enter the coordinates of the points. 6.43 What does the following method do? // Parameter b must be a positive // integer to prevent infinite recursion public int mystery( int a, int b ) { if ( b == 1 ) return a; else return a + mystery( a, b - 1 ); } 6.44 After you determine what the program in Exercise 6.43 does, modify the method to operate properly after removing the restriction of the second argument being nonnegative. Also, incorporate the method into an applet that enables the user to enter two integers, and test the method. 6.45 Write an application that tests as many of the math-library methods in Fig. 6.2 as you can. Exercise each of the methods by having your program print out tables of return values for a diversity of argument values. 6.46 Find the error in the following recursive method, and explain how to correct it: public int sum( int n ) { if ( n == 0 ) return 0; else return n + sum( n ); } 6.47 Modify the craps program of Fig. 6.9 to allow wagering. Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager. Check that wager is less than or equal to bankBalance, and if not, have the user reenter wager until a valid wager is entered. After a correct wager is entered, run one game of craps. If the player wins, increase bankBalanceby wager, and print the new bankBalance. If the player loses, decrease bankBalance by wager, print the new bankBalance, check if bankBalance has become zero, and if so, print the message “Sorry. You busted!” As the game progresses, print various messages to create some chatter, such as “Oh, you’re going for broke, huh?” or “Aw c’mon, take a chance!”or “You’reupbig.Now’sthetime tocashin yourchips!”. Implement the chatter as a separate method that randomly chooses the string to display. 6.48 Write an applet that uses a method circleArea to prompt the user for the radius of a circle and to calculate and print the area of that circle. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply