1 Review Over the past two weeks we have introduced a software development process called object oriented programming We create programs on a computer to model things this could be as simple as printing a greeting or a circle to creating a human resource system for a large corporation Chapter 1 in the text is about the object oriented approach Classes and objects allow us to build a system in a logical way gives a good description of the system allows for code reuse fosters system building by teams - efficient The Software Engineering approach gives a clear methodology for building a system in this manner. Waterfall model Analysis Design Implementation Test Maintenance 2 We have started to build our programming technical skills What is the basic necessity for a program? – hello world Code, compile, and run What text editor do I use? How do I compile a program? How do I run a compiled program? notepad, javac, and java How can I improve my efficiency enhance coding find syntax errors quickly test easily reuse code – how did we do grayCircle Integrated Development Environment 3 The Object of Java Chapter 2 notes Objectives: Introduce basic Java concepts necessary to write an initial program in the form a class called Director Syntax diagrams (section 2.1) Describe syntax and semantics of a series of statements Syntax and semantics of a parameterless method call instruction Syntax of an assignment instruction used to construct new objects Syntax and usage of instance variable declarations Class specifications and method preconditions and postconditions Syntax of Java comments and their usage 4 2.1 Syntax Diagrams Syntax (format) and Semantics (behavior or meaning) Coding errors such as leaving out a semi colon are syntax errors. Run time errors such as not calculating an average correctly are semantic errors The use of blanks as separators is not usually understood. 2.2 The Method Call Each object has a set of behaviors that are programmed as methods. e.g. a drawing pencil can move forward and turn, etc object reference . method name ( ) ; e.g. pencil.moveForward(); Constructors fall into this category DrawingTool() Executing a method is sometimes known as sending a message to the object. 5 2.3 Instruction Sequences Normally called statement sequences or statements in the Java program Unless there is a branch, statements are executed in a sequential manner. Will use a drawing tool in the next lab in a sequence to create a picture. 2.4 Constructing and Assigning Objects An object of a particular class must be created, instantiated, or constructed before it can be used. The format of the instantiation is new ConstructorName() In particular, the constructor name is the class name. e.g. new DrawingTool() In order to associate a newly constructed object with a name, an assignment statement or instruction is used. Variable = Expression; Assignment Instruction assigns or binds a variable name to the result of an expression evaluation. 6 Full method for creating and instantiating an object is developed using sections 2.4 and 2.6. Every Java variable must be declared before it can be used. (and then initialized) private DrawingTool pencil; // line 1 declares pencil of type DrawingTool. You then instantiate it with pencil = new DrawingTool(); // line 2 This can be done in one line. There are various problems that can occur with unbound and the process of binding variables. After the execution of line 1, pencil has a null value. Using it then, before line 2 would cause a NullPointerException. Consider private DrawingTool pen; pen = new DrawingTool(); pencil = pen; This results in pen and pencil pointing to the same storage location. They are both aliases for each other. 7 pen = new DrawingTool(); pencil = new DrawingTool(); pencil = pen; This results in the object that pencil originally pointed to becoming an orphan. Can’t access it again, Java VM will recover the space. pencil = new DrawingTool(); pencil = new DrawingTool(); This creates and orphan, and so does pencil = new DrawingTool(); pencil = null; Not a good practice 2.5 Swapping This routine is a classic that illustrates the need for temporary storage. temp = pen; pen = pencil; pencil = temp; Usually created as a helper method within a class. 8 2.6 Putting It Together in a Java Class Instance variables are declared as part of a class declaration. The values can vary from one class instantiation to the next. private DrawingTool pencil; (The difficult part is that there are also class variables that do not vary from instantiation to instantiation.) Declare the variable, Instantiate the variable, and Bind or initialize the variable. Figure 2.11 as an example of a Director Class. This is run with a go class. // Author: Riley -- May, 2000 public class go { public static void main(String args[]) Director director = new Director(); } } { 9 2.7 Programming by Contract A class invariant defines a collection of facts that are true about class objects: A DrawingTool object appears as an arrow within the DrawingCanvas window. (Window is 200 pixels wide, 150 pixels high.) I expect you to use a postcondition and possibly a precondition for each method that you write. We will practice this in the upcoming lab. The Precondition/Postcondition Contract: If the calling code ensures the precondition is true at the time it calls the method, then the postcodition is guaranteed to be true at the time the method completes. 2.8 Comments Comments are signaled by a line beginning with // or a block beginning with /* and ending with */ Comments are a major way of documenting your program 10 COMMENTS // OR /* */ At the beginning of every class a comment to indicate the Author Date Other identifiers Throughout the class to provide specifications, (class invariants, preconditions, and postconditions). Any place the code might otherwise be confusing.