Chapter 4 Deitel and Deitel Feb. 22, 2000 0) Using input1 = new TextField( ); // no characters can be entered PROBLEM! instead of input1 = new TextField(8); // up to 8 characters can be entered 1) Is there a sound in the forest if a huge tree falls over and no one (not even forest animals) is there listening? In your applet, when the action occurs (such as typing a number in the TextField and hitting Enter) then the ActionListener notices it and method actionPerformed is executed. Java uses the "delegation event model" (see p. 524) which means that a particular object in your program must be delegated to process each event. See the chart on p.525 listing event-listener interfaces Actually, in Ch. 10 see that there are 2 ways to do this. We have been using one way, say on p. 176. this applet handles the event NOTICE WHERE THE implements ActionListener IS LOCATED A second way is shown on p. 529-530. Another object handles the event. NOTICE WHERE THE implements ActionListener IS LOCATED 2) You need to understand, at least in a general way, what each statement in program is doing. Your book explains that well with line numbers. 3) Quiz. 4) Discuss Syllabus and Grading method. Need to turn in some homework to get a grade. 5) In many programs, and Java application, you use a loop (perhaps with a counter or a using sentinel value) to find a total of entered numbers. This is shown on p. 81 and 90. But with an applet, due to the "delegation event model" this is not done. See p. 31* Instead, each time a number is entered in the TextField, the ActionListener pays heed and actionPerformed is executed. That is where the newly entered number is added to the current total. (In fact, if you put a sentinel-controlled loop in your program, say in paint( ), with a new TextField and prompt statments, then the system becomes unstable, because too many TextFields are generated. Recall that when no number is entered in the TextField yet, the value of the instance variable is 0, as on p.45.) 6) See p. 171. Java has many Math functions. To call them, you need to put the class Math. in front of the method name like this, Math.max(5, 7) which has value 7 HERE (5,7) is the PARAMETER LIST or Math.max(5.2, 3.89) which has value 5.2 HERE (5.2, 3.89) is the PARAMETER LIST Also Math.floor(178.94) is 178.0 THE VALUE IS double, not int SO FIX p. 171 chart so that Math.floor(9.2) is 9.0 and Math.floor(-9.8) is -10.0 7) Notice above that there were 2 "versions" of method max. One version returned an int result (when both parameters were int) and the other version returned a double result. We say that a method is "overloaded" when it has more than one valid "version" How can this be? Each method has a SIGNATURE. The signature is the combination of the method name and its parameter list. A function is overloaded when 2 versions have the same name but different parameter types or different numbers of parameters. For an example, see p.204 method square with 2 headings int square (int x) and double square (double y) But notice that a method CANNOT BE OVERLOADED BY USING JUST DIFFERENT RETURN TYPES as shown on p. 205 which does not compile? Last week we saw another example of method overloading in these Ch. 9 Polygon constructor calls on p. 502 (A constructor is a special method whose name is the class name) p4 = new Polygon( xValues, yValues); p5 = new Polygon( ); The parameter lists are different. Another example is p. 176 line 16 result = new TextField("0",10); compared to p. 44 line22 input1 = new TextField(10); 8) There is also a Math.random( ) function that returns a "pseudorandom" double (decimal) number between 0.0 and 1.0 SEE p. 183 line 15 which can be "split up" this way and which gives face an int value of either 1,2,3,4,5, or 6 each time. int face; face = 1 + (int) (Math.random( ) * 6); The general formula is on p. 185. Using it to get a random 3 digit int (like 236 or 879) would require int n; n = 900 + (int) ( Math.random( ) * 100 ); // BECAUSE 100 is the lowest 3 digit int // AND there are 900 possibilities starting // with 100 and ending with 999