Chapter 3 Introduction to Java Applets 145 SELF-REVIEW (Cedant web hosting)
Chapter 3 Introduction to Java Applets 145 SELF-REVIEW EXERCISES 3.1 Fill in the blanks in each of the following. a) Class provides methods for drawing. b) Java applets begin execution with a series of three method calls: , and . c) Methods and display lines and rectangles. d) Keyword indicates that a new class is a subclass of an existing class. e) Every Java applet should extend either class or class . f) Java s eight primitive data types are , , , , ,, and . 3.2 State whether each of the following is true or false. If false, explain why. a) To draw a rectangle, method drawRect requires four arguments that specify two points on the applet. b) Method drawLine requires four arguments that specify two points on the applet to draw a line. c) Type Doubleis a primitive data type. d) Data type int is used to declare a floating-point number. e) Method Double.parseDouble converts a String to a primitive double value. 3.3 Write Java statements to accomplish each of the following: a) Display a dialog asking the user to enter a floating-point number. b) Convert a String to a floating-point number and store the converted value in double variable age. Assume that the String is stored in stringValue. c) Draw the message “This is a Java program” on one line on an applet (assume you are defining this statement in the applet s paintmethod) at position (10, 10). d) Draw the message “This is a Java program” on two lines on an applet (assume these statements are defined in applet method paint) starting at position (10, 10) and where the first line ends with Java. Make the two lines start at the same x coordinate. ANSWERS TO SELF-REVIEW EXERCISES 3.1 a) Graphics. b) init, start, paint. c) drawLine, drawRect. d) extends. e) JApplet, Applet. f) byte, short, int, long, float, double, charand boolean. 3.2 a) False. Method drawRect requires four arguments two that specify the upper-left corner of the rectangle and two that specify the width and height of the rectangle. b) True. c) False. Type Double is a class in the java.lang package. Remember that names that start with a capital letter are normally class names. d) False. Data type double or data type float can be used to declare a floating-point number. Data type int is used to declare integers. e) True. 3.3 a) stringValue = JOptionPane.showInputDialog( “Enter a floating-point number” ); b) age = Double.parseDouble( stringValue ); c) g.drawString( “This is a Java program”, 10, 10 ); d) g.drawString( “This is a Java”, 10, 10 ); g.drawString( “program”, 10, 25 ); EXERCISES 3.4 Fill in the blanks in each of the following: a) Data type declares a single-precision floating-point variable. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01