📄 ch37.htm
字号:
the text string to display, and the column and row at which todisplay the text.<LI>The <TT>paint()</TT> method draws whatever needs to be displayedin the applet's display area. Java calls <TT>paint()</TT> wheneverthe applet needs to be redrawn.<LI>One way to get user input is to add a <TT>TextField</TT> controlto your applet.<LI>Java calls the <TT>init()</TT> method almost immediately afteran applet starts up in order to enable you to initialize objectsneeded by the applet.<LI>Java calls the <TT>action()</TT> method whenever the userdoes something with the applet's controls. For example, when theuser types text into a <TT>TextField</TT> control and pressesEnter, Java calls <TT>action()</TT> so that the applet can respondto the user's input.<LI>To convert a numerical value to a string, you call the <TT>String</TT>class's <TT>valueOf()</TT> method.</OL><H2><A NAME="Chapter7"><FONT SIZE=5 COLOR=#Ff0000>Chapter 7</FONT></A></H2><OL><LI>The addition, subtraction, multiplication, and division operatorsare +, Ð, *, and /, respectively.<LI><TT>5*3</TT> equals 15.<LI><TT>3 Ð 2 + 5 + 6 Ð1</TT> equals 11.<LI>The ++ operator increments the variable, so <TT>num</TT> endsup equal to 13.<LI><TT>12 % 5</TT> equals 2.<LI>If <TT>num</TT> equals 25, the expression <TT>num += 5</TT>makes <TT>num</TT> equal to 30.<LI>You set the text is a <TT>TextField</TT> object by calling<TT>TextField's</TT> <TT>setText()</TT> method.<LI><TT>12 + 3 * 6 / 2</TT> equals 21.<LI>You can change <TT>3 / 5</TT> from integer to floating-pointdivision by changing one or both of the numbers to a floating-pointvalue, like this: <TT>3f / 5f</TT>.<LI>You cast the result of <TT>56 Ð 34.56f</TT> to integerlike this: <TT>(int)(56 Ð 34.56f)</TT>.<LI>You convert digits in a string to an integer by calling the<TT>Integer</TT> class's <TT>parseInt()</TT> method.<LI> (12 Ð 8) * 10 / 2 * 2 equals 40.</OL><H2><A NAME="Chapter8"><FONT SIZE=5 COLOR=#Ff0000>Chapter 8</FONT></A></H2><OL><LI>An expression is a line of program code that can be reducedto a value or that assigns a value to a variable or constant.<LI> The three types of expressions are numerical, assignment,and logical.<LI>The expression <TT>(3 < 5)</TT> equals <TT>true</TT>.<LI>The expression <TT>(3 < 5) && (5 == 4 + 1)</TT>equals <TT>true</TT>.<LI>Expressions are recursive because they can contain other smallerexpressions, which in turn may contain other expressions.<LI>The six comparison operators are <TT>==</TT> (equals), <TT>!=</TT>(not equal), <TT><</TT> (less than), <TT>></TT> (greaterthan), <TT><=</TT> (less than or equals), and <TT>>=</TT>(greater than or equals).<LI>The four logical operators are <TT>!</TT> (NOT), <TT>&&</TT>(AND), <TT>||</TT> (OR), and <TT>^</TT> (exclusive OR).<LI>The result of the expression <TT>(3 < 5) || (6 == 5) ||(3 != 3)</TT> is <TT>true</TT>.<LI>The result of the expression <TT>(5 != 10) && ((3== 2 + 1) || (4 < 2 + 5))</TT> is <TT>true</TT>.<LI>The result of the expression <TT>!(5 == 2 + 3) &&!(5 + 2 != 7 - 5)</TT> is false.</OL><H2><A NAME="Chapter9"><FONT SIZE=5 COLOR=#Ff0000>Chapter 9</FONT></A></H2><OL><LI>Program flow is the order in which program statements areexecuted.<LI>Conditional branching occurs when a program branches to anew section based on the value of some data. Unconditional branchingis when a computer instruction causes the program to branch regardlessof any conditions.<LI>Two ways to control program flow are <TT>if</TT> and <TT>switch</TT>statements.<LI>No. The second line will execute only when <TT>choice</TT>equals 3.<LI>You can write an <TT>if</TT> statement without opening andclosing braces when only one program line will execute if thecondition evaluates to <TT>true</TT>. <LI>There is no difference between a logical and a Boolean expression.They are both expressions that evaluate to <TT>true</TT> or <TT>false</TT>.<LI>The program skips over both the <TT>if</TT> and the <TT>elseif</TT>.<LI>The <TT>if</TT> and <TT>switch</TT> statements are similarin that they both enable the computer to choose a path of executionbased on the value of a control variable. They are different inthat a <TT>switch</TT> statement is more appropriate for situationsin which there are many possible outcomes.<LI>The variable <TT>num</TT> ends up with the value 3. If youranswer was 2, you didn't notice that the second <TT>case</TT>has no <TT>break</TT> statement, causing program execution todrop through to the third <TT>case</TT>.</OL><H2><A NAME="Chapter10"><FONT SIZE=5 COLOR=#Ff0000>Chapter 10</FONT></A></H2><OL><LI>You should use a loop when your program must perform somesort of repetitive task.<LI>The body of the loop comprises the program lines that areexecuted each time the loop's conditional expression is true.<LI>When the conditional expression evaluates to <TT>false</TT>,the loop ends.<LI>There is no guarantee on how many times a <TT>while</TT> loopwill execute. It could be any number of times from 0 on up. A<TT>do-while</TT> loop, on the other hand, always executes atleast once.<LI>You must properly initialize the loop control variable becausethe loop's conditional expression relies on the value of the variableto determine how many times to loop.<LI>An infinite loop occurs when a loop's conditional expressioncan never result in <TT>false</TT>, causing the loop to repeatendlessly.<LI>Both the <TT>while</TT> and <TT>do-while</TT> loops can beused to perform repetitive tasks in a program. However, the <TT>while</TT>loop may execute any number of times, including 0, whereas a <TT>do-while</TT>loop always executes at least once. This is because a <TT>do-while</TT>loop's conditional expression is at the end of the loop, whereasa <TT>while</TT> loop's conditional expression is at the beginningof the loop.<LI>The loop will execute six times, and <TT>count</TT> will beequal to 16 at the end of the loop.</OL><H2><A NAME="Chapter11"><FONT SIZE=5 COLOR=#Ff0000>Chapter 11</FONT></A></H2><OL><LI>You should use a <TT>for</TT> loop when you have a repetitivetask that must be performed a specific number of times.<LI>The three parts of a <TT>for</TT> loop are the initialization,condition, and increment sections.<LI>A <TT>for</TT> loop stops looping when the condition sectionbecomes false.<LI>A <TT>for</TT> loop can count backward by decrementing thecontrol variable in the increment section, rather than incrementingit.<LI>A <TT>for</TT> loop can count by tens by adding 10 to thecontrol variable in the increment section.<LI>It's possible to create an infinite loop with a <TT>for</TT>loop, but it's unlikely because the loop control variable is handledby the loop itself.<LI>The loop will execute five times, and <TT>x</TT> will be equalto 13 at the end of the loop.</OL><H2><A NAME="Chapter12"><FONT SIZE=5 COLOR=#Ff0000>Chapter 12</FONT></A></H2><OL><LI>Top-down programming means organizing source code into levelsthat go from general functions to more detailed functions thefurther down the hierarchy you go.<LI>Functions make top-down programming possible by enabling youto organize source code into well-defined tasks.<LI>All functions have a return type, but the return type of <TT>void</TT>means that the function returns no actual value.<LI>Arguments are values that you pass to a function when youcall it. The receiving function can then access the values almostas if they were local to the function.<LI>Defining a function is the act of writing the function's sourcecode.<LI>You return a value from a function by using the keyword <TT>return</TT>followed by the value to be returned. The returned value mustbe the same type as the function's defined return type.<LI>The arguments in the function call must be in the same orderand be of the same type as the arguments given in the function'sdefinition.<LI>The best way to break source code up into functions is tolocate the groups of commands that perform a specific task andthen replace those lines with a function call. The lines thatare replaced become the body of the new function.</OL><H2><A NAME="Chapter13"><FONT SIZE=5 COLOR=#Ff0000>Chapter 13</FONT></A></H2><OL><LI>An array is a data structure that enables you to store manyrelated values under one variable name.<LI>You can access the values of an array using loops, which greatlyreduces the amount of source code needed to handle large numbersof related values.<LI>An array subscript identifies a specific element of an array.The subscript is a number or variable enclosed in square brackets.A subscript and an index are exactly the same thing.<LI>A two-dimensional array can store values in a table, witha specific number of columns and rows.<LI>The largest subscript you can use with a fifty-element arrayis 49.<LI>If you try to access a nonexistent array element, Java generatesan exception.<LI>A <TT>for</TT> loop is perfect for array access because youcan use the loop control variable as an array subscript.<LI>To initialize a two-dimensional array with <TT>for</TT> loops,you'd use nested loops. The outer loop counts through the columnsand the inner loop counts through the rows.</OL><H2><A NAME="Chapter14"><FONT SIZE=5 COLOR=#Ff0000>Chapter 14</FONT></A></H2><OL><LI>A class is a template from which you create an object. A classusually contains data fields and methods.<LI>A class provides an additional level of program abstractionthat enables you to organize data fields, and the methods thatinteract with those data fields, within a single structure.<LI>The three parts of a simple, empty class are the keyword <TT>class</TT>,followed by the name of the class and the braces that mark offthe body of the class.<LI>The two program elements that you must add to the empty classin order to create a complete class are data fields and methods.<LI>To create an object of a class, you use the <TT>new</TT> operatorfollowed by a call to the class's constructor.<LI>To use a class that's defined in a different file, you placethe <TT>import</TT> keyword, followed by the class's name, atthe top of the file that needs access to the class. You must alsobe sure that the compiler can find the class, usually by placingthe class files all in the same directory.<LI>Using inheritance, a new class (subclass) derived from a baseclass (superclass) inherits the data fields and methods definedin the base class. The programmer then only needs to add whateveradditional functionality is required by the new class.<LI>A subclass is a class that's been derived from another classusing the <TT>extends</TT> keyword. A superclass is the classfrom which a subclass is derived. That is, the terms subclassand base class are equivalent.<LI>You create a subclass by using the <TT>extends</TT> keywordlike this:<BR><TT>class SubClass extends SuperClass<BR><FONT FACE="Courier New">{<BR>}</FONT></TT><LI>To override a method, you provide, in your subclass, a methodwith exactly the same name, return type, and arguments as themethod of the superclass you want to override. Then, Java willcall your class's version of the method rather than the superclass'sversion.</OL><H2><A NAME="Chapter15"><FONT SIZE=5 COLOR=#Ff0000>Chapter 15</FONT></A></H2><OL><LI>All applets must be derived from Java's <TT>Applet</TT> class.<LI>Applet classes must be public so that the system can run theapplet. If you fail to declare an applet as <TT>public</TT>, itwill compile fine, but it will not run.<LI>The five life-cycle stages of an applet are initialization,start, paint, stop, and destroy.<LI>The paint cycle isn't an "official" stage in theapplet's life cycle. It isn't, in fact, even defined within the<TT>Applet</TT> class. Instead, the <TT>paint()</TT> method isinherited from Java's <TT>Component</TT> class.<LI>The initialize cycle occurs only once in the applet's lifecycle (when the applet is loaded and prepared to run), whereasstart can occur numerous times (after initialization or wheneverthe applet is restarted).<LI>The <TT>init()</TT>, <TT>start()</TT>, <TT>stop()</TT>, and<TT>destroy()</TT> methods as they are implemented in the <TT>Applet</TT>class do absolutely nothing. They are only placeholders that youcan override in your applet class. The same is true of the <TT>paint()</TT>method.</OL><H2><A NAME="Chapter16"><FONT SIZE=5 COLOR=#Ff0000>Chapter 16</FONT></A></H2><OL><LI>The area of an applet in which you can draw is called thecanvas.<LI>The origin of Java's graphical coordinate system is in theupper left corner with values of X increasing to the right andvalues of Y increasing downwards.<LI>The <TT>drawRect()</TT> method draws a hollow rectangle, whereasthe <TT>fillRect()</TT> method draws a filled (solid) rectangle.<LI>The four arguments for the <TT>drawRect()</TT> method arethe X,Y coordinates of the rectangle's upper left corner and thewidth and height of the rectangle.<LI>The first four arguments for both <TT>drawRect()</TT> and<TT>drawRoundRect()</TT> are exactly the same, being the X,Y coordinates,width, and height of the rectangle. The <TT>drawRoundRect()</TT>method, however, has two additional arguments that are the widthand height of a rectangle that determines the size of the roundedcorners.<LI>The <TT>drawPolygon()</TT> method uses arrays to store itscoordinates because a polygon can have any number of sides, whichmeans that the method call must have a way to pass differing numbersof points. The arrays enable you to define as many sides as youneed while keeping the method's argument count consistent.<LI>The six arguments required by the <TT>drawArc()</TT> methodare the X,Y coordinates, width, and height of the bounding rectangle,as well as the starting drawing angle and the number of degreesaround to draw.<LI>The <TT>Polygon</TT> class provides several methods that enableyou to manipulate a polygon in various ways.</OL><H2><A NAME="Chapter17"><FONT SIZE=5 COLOR=#Ff0000>Chapter 17</FONT></A></H2><OL><LI>To get a reference to the currently active font object, youcall the <TT>Graphics</TT> class's <TT>getFont()</TT> method.<LI>To get a font's name, you can call the <TT>Font</TT> class's<TT>getName()</TT> method.<LI>To get a font's height, you can call the <TT>Font</TT> class's<TT>getHeight()</TT> method.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -