DOC PREVIEW
Stanford CS 106A - Control Statement Slides

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Eric Roberts Handout #18CS 106A January 15, 2010Control Statement SlidesOptional MovieMartin Luther King, Jr.“I Have a Dream”Gates B-12Monday, January 183:15 P.M.And so even though we face the difficultiesof today and tomorrow, I still have a dream.It is a dream deeply rooted in the Americandream. I have a dream that one day this nationwill rise up and live out the true meaning ofits creed: “We hold these truths to be self-evident, that all men are created equal.”Control StatementsEric RobertsCS 106AJanuary 15, 2010The GObject HierarchyThe classes that represent graphical objects form a hierarchy, partof which looks like this:GObjectGRect GOval GLineGLabelThe GObject class represents the collection of all graphicalobjects. The four subclasses shown in this diagram correspond toparticular types of objects: labels, rectangles, ovals, and lines.The class diagram makes it clear that any GLabel, GRect, GOval,or GLine is also a GObject.Operations on the GObject Classobject.setColor(color)Sets the color of the object to the specified color constant.object.setLocation(x, y)Changes the location of the object to the point (x, y).object.move(dx, dy)Moves the object on the screen by adding dx and dy to its current coordinates.The following operations apply to all GObjects:The standard color names are defined in the java.awt package:Color.BLACKColor.DARK_GRAYColor.GRAYColor.LIGHT_GRAYColor.WHITEColor.REDColor.YELLOWColor.GREENColor.CYANColor.BLUEColor.MAGENTAColor.ORANGEColor.PINKOperations on the GLabel ClassConstructornew GLabel(text, x, y)Creates a label containing the specified text that begins at the point (x, y).Methods specific to the GLabel classlabel.setFont( font )Sets the font used to display the label as specified by the font string.The font is typically specified as a string in the form"family-style-size"where fa m ily is the name of a font family style is either PLAIN, BOLD, ITALIC, or BOLDITALIC size is an integer indicating the point sizeDrawing Geometrical ObjectsConstructorsnew GRect( x, y, width, height)Creates a rectangle whose upper left corner is at (x, y) of the specified size.new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.Methods shared by the GRect and GOval classesobject.setFilled( fill)If fill is true, fills in the interior of the object; if false, shows only the outline.object.setFillColor( color )Sets the color used to fill the interior, which can be different from the border.new GLine( x0, y0, x1, y1)Creates a line extending from (x0, y0) to (x1, y1).– 2 –Statement Types in Java• Programs in Java consist of a set of classes. Those classescontain methods, and each of those methods consists of asequence of statements.• Statements in Java fall into three basic types:– Simple statements– Compound statements– Control statements• Simple statements are formed by adding a semicolon to theend of a Java expression.• Compound statements (also called blocks) are sequences ofstatements enclosed in curly braces.• Control statements fall into two categories:– Conditional statements that specify some kind of test– Iterative statements that specify repetitionBoolean ExpressionsGeorge Boole (1791-1871)In many ways, the most important primitivetype in Java is boolean, even though it is byfar the simplest. The only values in theboolean domain are true and false, butthese are exactly the values you need if youwant your program to make decisions.The name boolean comes from the Englishmathematician George Boole who in 1854wrote a book entitled An Investigation intothe Laws of Thought, on Which are Foundedthe Mathematical Theories of Logic andProbabilities. That book introduced a systemof logic that has come to be known asBoolean algebra, which is the foundation forthe boolean data type.Boolean Operators• The operators used with the boolean data type fall into twocategories: relational operators and logical operators.• There are six relational operators that compare values of othertypes and produce a boolean result:= =Equals<Less than!=Not equals<=Less than or equal to>= Greater than or equal to>Greater thanFor example, the expression n <= 10 has the value true if x isless than or equal to 10 and the value false otherwise.p || q means either p or q (or both)• There are also three logical operators:&& Logical AND||Logical OR! Logical NOTp && q means both p and q!p means the opposite of pNotes on the Boolean Operators• Remember that Java uses = to denote assignment. To testwhether two values are equal, you must use the = = operator.•The || operator means either or both, which is not alwaysclear in the English interpretation of or.• It is not legal in Java to use more than one relational operatorin a single comparison as is often done in mathematics. Toexpress the idea embodied in the mathematical expression0  x  90 <= x && x <= 9you need to make both comparisons explicit, as in• Be careful when you combine the ! operator with && and ||because the interpretation often differs from informal English.Short-Circuit Evaluation• Java evaluates the && and || operators using a strategy calledshort-circuit mode in which it evaluates the right operandonly if it needs to do so.• One of the advantages of short-circuit evaluation is that youcan use && and || to prevent execution errors. If n were 0 inthe earlier example, evaluating x % n would cause a “divisionby zero” error.• For example, if n is 0, the right hand operand of && inn != 0 && x % n == 0is not evaluated at all because n != 0 is false. Because theexpressionfalse && anythingis always false, the rest of the expression no longer matters.The if StatementThe simplest of the control statements is the if statement, whichoccurs in two forms. You use the first form whenever you needto perform an operation only if a particular condition is true:if (condition) { statements to be executed if the condition is true}You use the second form whenever you want to choose betweentwo alternative paths, one for cases in which a condition is trueand a second for cases in which that condition is false:if (condition) { statements to be executed if the condition is true} else { statements to be executed if the condition is false}– 3 –Common Forms of the if Statementif (condition) statementSingle line if statementif (condition) { statement . . . more statements . . .}Multiline if statement with curly bracesif (condition) {


View Full Document

Stanford CS 106A - Control Statement Slides

Download Control Statement Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Control Statement Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Control Statement Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?