Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Object of Java Spring 2003 Wayne State College CSC 340 Lect. #1.

Similar presentations


Presentation on theme: "The Object of Java Spring 2003 Wayne State College CSC 340 Lect. #1."— Presentation transcript:

1 The Object of Java Spring 2003 Wayne State College CSC 340 Lect. #1

2 Text editor (ProjectBuilder.app) Software ToolDeliverable Java compiler (javac) Bytecode file ( className.class ) Java Virtual Machine (java) Source code file ( className.java ) Action Programmer types the software into the computer. The software is translated into a form that is “understood” by the computer. The program executes. The Object of Java, David D. Riley © Addison Wesley pub.

3 Definition An object is an entity that has 1) state (what the object looks like) and 2) behavior (how the object acts). Examples  a pizza  a microwave oven More complicated objects are often constructed from simpler ones.  a telephone  an audio CD player The Object of Java, David D. Riley © Addison Wesley pub.

4 Class Every object belongs to a group of objects of the same type. This group is referred to as the object’s class.. for example  Kermit the Frog belongs to the class of __________.  Wisconsin is an object of type ________.  Your new Ferrari is an object from the ________ class. A class defines the state and behavior for some group of objects.  State is defined via attributes (called instance variables in Java).  Behavior is defined via operations (called methods in Java). Definition The Object of Java, David D. Riley © Addison Wesley pub.

5 A class diagram is a picture of the members of a class. The notation used here is borrowed from the Universal Modeling Language (UML). Example Class Name attributes/instance variables methods PortableCDPlayer boolean containsDisk int totalNumberOfTracks int trackPlaying int secondsPlayed void insertCD() void removeCD() void play() void stop() void pause() void skipToNextTrack() The Object of Java, David D. Riley © Addison Wesley pub.

6 A DrawingTool object can be moved around a window to draw lines. It appears like an arrow. (See the green arrow to the right.) The Class Diagram DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw() The Object of Java, David D. Riley © Addison Wesley pub.

7 The method call is the primary instruction in a Java program. Method Call Syntax When a method call is executed, the operation associated with the method is applied to an object. objectReference. methodName() ; where  objectReference refers to some particular object, and  methodName is the name of a parameterless method from the class to which objectName belongs. Examples (assume an object, pen, of type DrawingTool) DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw() pen.moveForward(); pen.dontDraw(); pen.turnClockwise(); The Object of Java, David D. Riley © Addison Wesley pub.

8 Within a Java program instructions are also called statements. A sequence of statements (instructions) is executed in order from top to bottom. Sample Java (assume an object, pen, of type DrawingTool) DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw() pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.dontDraw(); pen.moveForward(); The Object of Java, David D. Riley © Addison Wesley pub.

9 Objects don’t exist until they are instantiated. Attempting to call methods on objects before they are instantiated is an error. Syntax to Instantiate & Assign objectName = new constructorName(); where  objectName the the name (i.e., variable) of some particular object, and  constructorName is the name of a parameterless constructor method from the class to which objectName belongs. (Note: In Java constructors have the same name as the class.) Example (assume an object, pen, of type DrawingTool) pen = new DrawingTool(); The Object of Java, David D. Riley © Addison Wesley pub.

10 Programming languages, like Java, are extremely particular about notation (syntax). MethodCall Notes  ObjectReference is a variable name or other acceptable object reference  MethodName is the name of a parameterless method from the class to which ObjectReference belongs.  No white space is permitted before or after the period (. ). Syntax diagrams are a pictorial way to define permissible syntax, ObjectReferenceMethodName.(); The Object of Java, David D. Riley © Addison Wesley pub.

11 AssignmentInstruction Notes  Variable is a variable name  Expression must have a type that conforms to the type of Variable. VariableExpression =; Expression (abridged version) Note  Constructor names a valid parameterless constructor. Variable Constructor ()new StatementSequence OneStatement OneStatement (abridged version) MethodCall AssignmentInstruction The Object of Java, David D. Riley © Addison Wesley pub.

12 InstanceVarDecls StatementSequence ImportDecls PrivateMethod publicclassDirector{ publicDirector(){ } } DirectorClass (abridged version of Class ) Semantics Executing go.class causes StatementSequence from Director to execute. Notes  ImportDecls and PrivateMethod are explained later.  The go.class file is required to cause this class to execute. The Object of Java, David D. Riley © Addison Wesley pub.

13 InstanceVarDecls Notes  The scope of each Variable is the class in which it is declared.  Each Variable must be named by an identifier that is unique for the class/ OneVarDecl PrivateVarDecl (a version of OneVarDecl ) ClassName private Variable ;, Example private DrawingTool pen; private DrawingTool pencil; Alternative private DrawingTool pen, pencil; The Object of Java, David D. Riley © Addison Wesley pub.

14 public class Director { private DrawingTool pen; public Director() { pen = new DrawingTool(); pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockWise(); pen.moveForward(); } The following program can be executed by go.class. When this program executes, it draws a square. DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw() The Object of Java, David D. Riley © Addison Wesley pub.

15 Identifiers Program entities are named by identifiers. Syntax  begin with one alphabetic character (a-z or A-Z)  followed by zero or more consecutive alphabetic and/or numeric (0-9) characters. (Underscores and dollars sign are reserved for special usage.) Notes  Take care to use different names for different entities.  Java identifiers are case sensitive (i.e., capital letters are different than small letters).  Identifiers should be meaningful to enhance software readability. Identifiers name... each class - - like DrawingTool each variable - - like pen each method - - like moveForward and other things (stay tuned) The Object of Java, David D. Riley © Addison Wesley pub.

16 Programmers frequently include notes within their programs. Such notes are called comments. Comment Comments can assist you and others in understanding a program. Notes  anythingOnOneLine is any sequence of characters up to the end of the text line.  anythingButCommentEnd is any sequence of characters up to the first */.  Comment is permitted anywhere white space is allowed. anythingOnOneLine // /* anythingButCommentEnd */ Semantics Executing a comment has no effect at run-time. Why Comment? The Object of Java, David D. Riley © Addison Wesley pub.

17 /* Program to draw two right angles Author: David Riley Date: August, 2000 */ public class Director { private DrawingTool pen, pencil; public Director() { pen = new DrawingTool(); pen.draw(); pen.moveForward(); pencil = new DrawingTool(); pencil.turnClockwise(); pencil.dontDraw(); pencil.moveForward(); pen.turnClockwise(); pen.moveForward(); pencil.draw(); pencil.moveForward(); pencil.turnClockWise(); pencil.moveForward(); } DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw() The Object of Java, David D. Riley © Addison Wesley pub.


Download ppt "The Object of Java Spring 2003 Wayne State College CSC 340 Lect. #1."

Similar presentations


Ads by Google