Docsity

Object-Oriented Design & Patterns: Chapter 1 Notes - Prof. Paul C. Grabow, Lab Reports of Software Engineering

Toggle sidebar
bg1
1
Object-Oriented Design & Patterns
Chapter 1 Notes
Revised 1/23/09
Here we will skip the following sections because they have (or are) being considered at other times, such
as in the lab:
1.1 (in Eclipse & Java tutorial)
1.2 (later)
1.9 (in Lab 1)
1.14 (later)
1. Objects vs Primitive Types
a. Eight primitive types (10)
b. Cannot convert from bool to a number, as in C++ (11)
c. Every method must have its own class (11)
d. Math class has static methods (i.e., that can be called directly without creating a Math
object)
e. When you create a new object using the “new” operator, you must always use
parentheses (unlike C++ which uses no parentheses when there are no parameters in the
constructor)
2. Control Flow
a. Essentially the same as C++
b. Enhanced for loop is different than C++ (24)
3. Object References
a. An object value is always a reference to an object (12)
b. In Java you manipulate object references not objects
c. When you copy a reference to an object you get an alias, not a new object (13)
d. Clone method (later)
e. null refers to “no object” (it is not the “null object”) (13)
f. there is automatic garbage collection, so there is no destructor in a class (14)
4. Parameter Passing
a. Implicit parameter vs explicit parameter
Greeter mine = new Greeter();
mine.function( param );
i. Here there is an implicit parameter for mine
ii. The “special name” for the implicit parameter is “this”, e.g., this.mine
iii. Here param is the explicit parameter
b. A method can change the state of an object if that object is an explicit parameter for the
method (Example 01)
c. A method cannot change the value of a parameter, which is either a reference to an object
or a primitive type (Example 01)
d. Java uses “call by value” when passing parameters (no call by reference)
pf3
pf4

Partial preview of the text

Download Object-Oriented Design & Patterns: Chapter 1 Notes - Prof. Paul C. Grabow and more Lab Reports Software Engineering in PDF only on Docsity!

Object-Oriented Design & Patterns Chapter 1 Notes

Revised 1/23/

Here we will skip the following sections because they have (or are) being considered at other times, such as in the lab:  1.1 (in Eclipse & Java tutorial)  1.2 (later)  1.9 (in Lab 1)  1.14 (later)

  1. Objects vs Primitive Types a. Eight primitive types (10) b. Cannot convert from bool to a number, as in C++ (11) c. Every method must have its own class (11) d. Math class has static methods (i.e., that can be called directly without creating a Math object) e. When you create a new object using the “new” operator, you must always use parentheses (unlike C++ which uses no parentheses when there are no parameters in the constructor)
  2. Control Flow a. Essentially the same as C++ b. Enhanced for loop is different than C++ (24)
  3. Object References a. An object value is always a reference to an object (12) b. In Java you manipulate object references not objects c. When you copy a reference to an object you get an alias, not a new object (13) d. Clone method (later) e. null refers to “no object” (it is not the “null object”) (13) f. there is automatic garbage collection, so there is no destructor in a class (14)
  4. Parameter Passing a. Implicit parameter vs explicit parameter

Greeter mine = new Greeter(); mine.function( param );

i. Here there is an implicit parameter for mine ii. The “special name” for the implicit parameter is “this”, e.g., this.mine iii. Here param is the explicit parameter b. A method can change the state of an object if that object is an explicit parameter for the method (Example 01) c. A method cannot change the value of a parameter, which is either a reference to an object or a primitive type (Example 01) d. Java uses “call by value” when passing parameters (no call by reference)

  1. Packages a. To guarantee uniqueness of package names, you should start the package name with a domain name in reverse, such as edu.baylor.csi b. The full name of a class is the package name followed by the class name, such as edu.baylor.csi3471.Greeter c. Use the import statement to avoid having to refer to the full name of each referenced class (17) d. In Eclipse, when you define a package and place classes in the package a folder is created (with the same name as the package) and the class files are placed in that folder e. You do not need to import the classes in the java.lang package, such as Math or String f. The base directory is the “top level” directory associated with the project. When you compile it is normally assumed that you are doing it from the base directory. So if your program creates any files (without specifying a location), those files will be created in the base directory.
  2. Basic Exception Handling a. When program tries to carry out illegal operation, the program generates an exception. b. Exceptions are classified according to what they represent. For example, if I try to open a file that does not exist, then I will generate a FileNotFoundException. c. We can i. Catch an exception, or ii. Throw an exception d. There are two basic categories of exceptions: i. Checked exceptions 1. If you call a method that might throw this exception, then you must specify how you want the program to deal with this failure. 2. You must either a. Declare the exception in the method header b. or catch the exception 3. The IOException is a checked exception 4. A checked exception is normally caused by an external condition – something beyond the control of the programmer. ii. Unchecked exceptions
  3. The compiler does not require you to do anything about this kind of exception. If it occurs – and you have not handled it – your program will halt.
  4. These are generally the programmer’s fault.
  5. The NullPointerException is an unchecked exception e. If a method can throw multiple exceptions then you must list them all, separated by commas (see examples on p. 20)

f. Try block and catch block try {

... code that may generate an exception } catch ( IOException exception ) { ... handle the exception }

while (iterator.hasNext() ) { String anElement = iterator.next(); if( anElement.equals( “mine” ) { Iterator.remove(); }

  1. Arrays a. Arrays can hold either objects or primitive types b. Construct an array similar to the following:

int[] numbers = new int[5];

Assume that there is a class called House, then

House[] houses = new House[20]

c. When an array is constructed its elements are set to zero, false, or null, depending upon the kind of array element d. You can use the “for each” loop to traverse all elements in the array e. You can initialize an array when it is declared, as follows:

int[] numbers = { 100, 200, 300, 400 } f. Arrays can have multiple dimensions, as follows:

int[][] matrix = new float[ 5 ] [ 8 ];

  1. Static Fields and Methods a. A static field belongs to the class, not to individual objects, so there is only one instance of the field for such a class b. The class Math has several static fields, such as Math.PI or Math.E