18.12. Programming Exercises

 
[Page 567 ( continued )]

Programming Exercises

Sections 16.2 “16.4

16.1 ( Converting applications to applets ) Convert Listing 15.2, ButtonDemo.java, into an applet.

Sections 16.5 “16.6

16.2* ( Passing strings to applets ) Rewrite Listing 16.5, DisplayMessage.java, to display a message with a standard color , font, and size . The message , x , y , color , fontname , and fontsize are parameters in the <applet> tag, as shown below:
   <applet     code   = "Exercise16_2.class"   width   = 200   height   = 50   alt   = "You must have a Java-enabled browser to view the applet"   >     <param name   = MESSAGE value = "Welcome to Java"   />     <param name   = X value = 40  />    <param name   = Y value = 50  />    <param name   = COLOR value = "red"  />    <param name   = FONTNAME value = "Monospaced"  />    <param name   = FONTSIZE value = 20  />    </applet>   

16.3 ( Enabling applets to run standalone ) Rewrite the LoanApplet in Listing 16.1, LoanApplet.Java, to enable it to run as an application as well as an applet.
16.4* ( Creating multiple windows from an applet ) Write an applet that contains two buttons called Investment Calculator and Loan Calculator . When you click Investment Calculator, a frame appears in a new window for calculating future investment values. When you click Loan Calculator , a frame appears in a separate new window for computing loan payments (see Figure 16.18).
[Page 568]
Figure 16.18. You can show frames in the applets.

16.5** ( A clock learning tool ) Develop a clock applet to show a first-grade student how to read a clock. Modify Exercise 13.20 to display a detailed clock with an hour hand and minute hand in an applet, as shown in Figure 16.19(a). The hour and minute values are randomly generated. The hour is between 0 and 11, and the minute is 0, 15, 30, or 45. A new random time is displayed upon a mouse click on the clock. Enable the applet to run standalone.
Figure 16.19. (a) Upon a mouse click on the clock, the clock time is randomly displayed. (b) The New Game button starts a new game.


16.6** ( TicTacToe ) Rewrite the program in Listing 16.7, TicTacToe.java, with the following modifications:
  • Declare Cell as a separate class rather than an inner class.

  • Add a button named New Game , as shown in Figure 16.19(b). The New Game button starts a new game.

16.7** ( Tax calculator ) Create an applet to compute tax, as shown in Figure 16.20. The applet lets the user select the tax status and enter the taxable income to compute the tax based on the 2001 federal tax rates, as shown in Exercise 7.14. Enable it to run standalone.
[Page 569]
Figure 16.20. The tax calculator computes the tax for the specified taxable income and tax status.
(This item is displayed on page 568 in the print version)


16.8*** ( Creating a calculator ) Use various panels of FlowLayout , GridLayout , and BorderLayout to lay out the following calculator and to implement addition subtraction division (/), square root (sqrt), and modulus (%) functions (see Figure 16.21(a)). Enable it to run standalone.
Figure 16.21. (a) Exercise 16.8 is a Java implementation of a popular calculator. (b) Exercise 16.9 converts between decimal, hex, and binary numbers .

16.9* ( Converting numbers ) Write an applet that converts between decimal, hex, and binary numbers, as shown in Figure 16.21(b). When you enter a decimal value on the decimal value text field and press the Enter key, its corresponding hex and binary numbers are displayed in the other two text fields. Likewise, you can enter values in the other fields and convert them accordingly . Enable it to run standalone.
16.10** ( Repainting a partial area ) When you repaint the entire viewing area of a panel, sometimes only a tiny portion of the viewing area is changed. You can improve the performance by only repainting the affected area, but do not invoke super.paintComponent(g) when repainting the panel, because this will cause the entire viewing area to be cleared. Use this approach to write an applet to display the temperatures of each hour during the last twenty-four hours in a histogram. Suppose that the temperatures between 50 and 90 degrees Fahrenheit are obtained randomly and are updated every hour. The temperature of the current hour needs to be redisplayed, while the others remain unchanged. Use a unique color to highlight the temperature for the current hour (see Figure 16.22).
Figure 16.22. The histogram displays the average temperature of every hour in the last twenty-four hours.


16.11** ( Showing a running fan ) Write a Java applet that simulates a running fan, as shown in Figure 16.23. The buttons Start, Stop, and Reverse control the fan. The scrollbar controls the fan's speed. Create a class named Fan , a subclass of JPanel , to display the fan. This class also contains the methods to suspend and resume the fan, set its speed, and reverse its direction. Create a class named FanControl that contains a fan, and three buttons and a scroll bar to control the fan. Create a Java applet that contains an instance of FanControl . Enable the applet to run standalone.
[Page 570]
Figure 16.23. The program simulates a running fan.


16.12** ( Controlling a group of fans ) Write a Java applet that displays three fans in a group, with control buttons to start and stop all of them, as shown in Figure 16.24. Use the FanControl to control and display a single fan. Enable the applet to run standalone.
Figure 16.24. The program runs and controls a group of fans.

16.13*** ( Creating an elevator simulator ) Write an applet that simulates an elevator going up and down (see Figure 16.25). The buttons on the left indicate the floor where the passenger is now located. The passenger must click a button on the left to request that the elevator come to his or her floor. On entering the elevator, the passenger clicks a button on the right to request that it go to the specified floor. Enable the applet to run standalone.
Figure 16.25. The program simulates elevator operations.


[Page 571]
16.14* ( Controlling a group of clocks ) Write a Java applet that displays three clocks in a group, with control buttons to start and stop all of them, as shown in Figure 16.26. Use the ClockControl to control and display a single clock. Enable the applet to run standalone.
Figure 16.26. Three clocks run independently with individual control and group control.

Section 16.9 Displaying Images

16.15* ( Enlarging and shrinking an image ) Write an applet that will display a sequence of images from a single image file in different sizes. Initially, the viewing area for this image has a width of 300 and a height of 300. Your program should continuously shrink the viewing area by 1 in width and 1 in height until it reaches a width of 50 and a height of 50. At that point, the viewing area should continuously enlarge by 1 in width and 1 in height until it reaches a width of 300 and a height of 300. The viewing area should shrink and enlarge (alternately) to create animation for the single image. Enable the applet to run standalone.
16.16*** ( Simulating a stock ticker ) Write a Java applet that displays a stock index ticker (see Figure 16.27). The stock index information is passed from the <param> tag in the HTML file. Each index has four parameters: Index Name (e.g., S&P 500), Current Time (e.g., 15:54), the index from the previous day (e.g., 919.01), and Change (e.g., 4.54). Enable the applet to run standalone.
Figure 16.27. The program displays a stock index ticker.

Use at least five indexes, such as Dow Jones, S&P 500, NASDAQ, NIKKEI, and Gold & Silver Index. Display positive changes in green, and negative changes in red. The indexes move from right to left in the applet's viewing area. The applet freezes the ticker when the mouse button is pressed; it moves again when the mouse button is released.

16.17** ( Racing cars ) Write an applet that simulates four cars racing, as shown in Figure 16.28. You can set the speed for each car with 1 the highest.
[Page 572]
Figure 16.28. You can set speed for each car.


16.18** ( Showing national flags ) Write an applet that introduces national flags, one after the other, by presenting each one's image, name, and description (see Figure 16.29) along with audio that reads the description.
Figure 16.29. This applet shows each country's flag, name, and description, one after another, and reads the description that is currently shown.


Suppose your applet displays the flags of eight countries . Assume that the photo image files, named flag0.gif, flag1.gif, and so on, up to flag7.gif, are stored in a subdirectory named image in the applet's directory. The length of each audio is less than 10 seconds. Assume that the name and description of each country's flag are passed from the HTML using the parameter name0 , name1 , name7 , and description0 , description1 , and description7 . Pass the number of countries as an HTML parameter using numberOfCountries . Here is an example:

 <param name="numberOfCountries" value=8> <param name="name0" value="Canada"> <param name="description0" value= "The Maple Leaf flag The Canadian National Flag was adopted by the Canadian Parliament on October 22, 1964 and was proclaimed into law by Her Majesty Queen Elizabeth II (the Queen of Canada) on February 15, 1965. The Canadian Flag (colloquially known as The Maple Leaf Flag) is a red flag of the proportions two by length and one by width, containing in its center a white square, with a single red stylized eleven-point mapleleaf centered in the white square."> 

Hint

Use the DescriptionPanel class to display the image, name, and the text. The DescriptionPanel class was introduced in Listing 15.6, TextAreaDemo.java.



[Page 573]
16.19*** ( Bouncing balls ) The example in §16.8 simulates a bouncing ball. Extend the example to allow multiple balls, as shown in Figure 16.30. You may use the + 1 or “ 1 button to increase or decrease the number of the balls, and use the Suspend and Resume buttons to freeze the balls or resume bouncing.
Figure 16.30. The applet allows you to add or remove bouncing balls.


Section 16.12 Playing Audio

16.20* ( Playing, looping, and stopping a sound clip ) Write an applet that meets the following requirements:
  • Get an audio file. The file is in the class directory.

  • Place three buttons labeled Play, Loop, and Stop, as shown in Figure 16.31.

    Figure 16.31. Click Play to play an audio clip once, click Loop to play an audio repeatedly, and click Stop to terminate playing.

  • If you click the Play button, the audio file is played once. If you click the Loop button, the audio file keeps playing repeatedly. If you click the Stop button, the playing stops.

  • The applet can run as an application.

16.21** ( Creating an alarm clock ) Write an applet that will display a digital clock with a large display panel that shows hour, minute, and second. This clock should allow the user to set an alarm. Figure 16.32(a) shows an example of such a clock. To turn on the alarm, check the Alarm check box. To specify the alarm time, click the "Set alarm" button to display a new frame, as shown in Figure 16.32(b). You can set the alarm time in the frame. Enable the applet to run standalone.
Figure 16.32. The program displays current hour, minute, and second, and enables you to set an alarm.


[Page 574]
16.22** ( Creating an image animator with audio ) Create animation using the applet (see Figure 16.33) to meet the following requirements:
  • Allow the user to specify the animation speed. The user can enter the speed in a text field.

  • Get the number of frames and the image filename prefix from the user. For example, if the user enters n for the number of frames and L for the image prefix, then the files are L1 , L2 , and so on, to L n. Assume that the images are stored in the image directory, a subdirectory of the applet's directory.

  • Allow the user to specify an audio filename. The audio file is stored in the same directory as the applet. The sound is played while the animation runs.

  • Enable the applet to run standalone.

Figure 16.33. This applet lets the user select image files, audio file, and animation speed.


16.23** ( Raising flag and playing anthem ) Create an applet that displays a flag rising up, as shown in Figure 14.20. As the national flag rises, play the national anthem. (You may use a flag image and anthem audio file from Listing 16.13.)
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net