180 likes | 197 Views
Review key concepts for a Java midterm focusing on Chapters 4 and 5. Includes constructors, methods, parameters, logical expressions, operators, and control flow in Java.
E N D
Review for Midterm 2 Aaron Bloomfield CS 101-E
Test focus • Test will focus on the material covered since the last midterm • Chapters 4 and 5 • The test is cumulative, though, and can include any material covered so far
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
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
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
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 • Actual parameter: the value passed into the method • Formal parameter: the value within the method
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
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
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
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
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
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”
Ordering • Ordering operators: <, >, <=, and >=. 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
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
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
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!