Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Review for Midterm 2 Aaron Bloomfield CS 101-E.

Similar presentations


Presentation on theme: "1 Review for Midterm 2 Aaron Bloomfield CS 101-E."— Presentation transcript:

1 1 Review for Midterm 2 Aaron Bloomfield CS 101-E

2 2 Test focus Test will focus on the material covered since the last midterm Chapters 4 and 5 Chapters 4 and 5 The test is cumulative, though, and can include any material covered so far

3 3 Chapter 4

4 4 Construction Constructors should initialize all the data fields to some sane value A constructor has the same name as the class A default constructor takes in no parameters If a default constructor is not specified, then Java supplies one for you that does nothing All instance variables are initialized to default values (0, false, or null) before the constructor starts To create an object, you declare the object reference then call the constructor to create the object

5 5 Member variables Each object has its own copy of an instance variable Instance variables are normally private Private variables and methods can only be accessed by code within that class Instance variable = member variable: a data field in an object

6 6 Methods Flow of control passes from the invoking statement block to the invoked method, then returns (once the method completes) back to the statement block To define a method, state it's prototype (name, return type, parameters) and the body (the statement block) You use return to return a value A method that does not return a value has return type void Object behaviors are implemented using methods Instance methods are associated with an object, as opposed to class methods, which are associated with the class Methods can access the member variables

7 7 Parameters Java uses pass-by-value However, an object as a parameter has the reference passed in, so it's sort of like pass- by-reference for objects However, an object as a parameter has the reference passed in, so it's sort of like pass- by-reference for objects Actual parameter: the value passed into the method Formal parameter: the value within the method

8 8 Method types An accessor method allows access to an attribute (member variable) of an object (that is most likely private) A mutator method allows the changing of an attribute (member variable) of an object (that is most likely private) A facilitator method performs a task for the object

9 9 Misc A statement block is denoted by curly braces Activation record is the name for the copy of the parameters within a method Data abstraction: hide the internals of how a method operates. An variable that is an object is really a reference The programmer “interface” is made up of the public methods

10 10 JFrame and Graphics JFrame is the class that represents a GUI window JFrame has methods setVisible() and setSize() Drawing on a JFrame is done by accessing its Graphics object via the getGraphics() method Graphics has methods setColor(), fillRect(), drawRect(), etc. When drawing on a Graphics object, (0,0) is in the upper-left

11 11 Chapter 5

12 12 Logical expressions Logical expression has values either true or false Java has the boolean type with values true or false Truth table: method to dissect a logical expression

13 13 Logical operators Three primary logical operators: and, or, not An and operation is only true when both parts are true An or operation is true when either (or both) parts are true A not operation negates (switches) the value of the expression Logical operators: and is &&, or is ||, not is ! Not operator is unary

14 14 Equality Two equality operators: == and != When comparing objects, == compares the references, not the objects themselves Use the.equals() method, when available, to test for object equality Don't test floating point values for equality! Instead, test for “closeness”

15 15 Ordering Ordering operators:, =. These only work on primitive types! Relational operators are the equality operators and the ordering operators For booleans, false is less than true For characters, ordering is based on the Unicode numbers of the characters

16 16 If statements An if statement has the form: if (expression) action An if-else statement has the form: if (expression) action1 else action2 An if-else-if statement is used when there are many tasks to do, depending on the logical expressions

17 17 Switches A switch statement is useful instead of a long-winded if-else-if block Must always put either break at the end of a switch statement block, or a comment such as '// FALL THRU' The default case means any case not matched by any of the other cases

18 18 Misc Operator precedence: (p 187) Short-circuit evaluation: left side is evaluated first. If the result can be determined at that point, right side is not evaluated System.exit() will terminate the program immediately Use consistent indentation!


Download ppt "1 Review for Midterm 2 Aaron Bloomfield CS 101-E."

Similar presentations


Ads by Google