Problem Solving Object Oriented Design and Java B

Problem Solving, Object. Oriented Design and Java B. Ramamurthy 1/8/2022 B. Ramamurthy 1

Topics for Discussion OO Design Principles Java Virtual Machine Java Application Structure Class and objects Methods and Variables Access/Visibility Modifiers Summary 1/8/2022 B. Ramamurthy 2

Object-Oriented Principles OOP Encapsulation (class) -- Information Hiding -- Interface and Implementations -- Standardization -- Access Control mechanisms (private /public etc. ) 1/8/2022 Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems B. Ramamurthy Polymorphism -- Many forms of same function -- Abstract Methods -- Abstract Classes 3

Conventional Compiled Languages C++source Mac Compiler Mac Hardware C++source PC Compiler PC Hardware C++source 1/8/2022 Sun Compiler B. Ramamurthy Sun Hardware 4

Java Virtual Machine Java compiler : javac Java source JVM Byte code Mac interpreter Compiler Mac Hardware JVM Java source Byte code PC Interpreter PC Hardware JVM Java source 1/8/2022 Byte code Sun Interpreter B. Ramamurthy Sun Hardware 5

“Run-anywhere” Capability On any machine when you compile Java source code using javac byte code equivalent to the source is generated. Byte code is machine-independent. This enables the “run-anywhere” capability. You invoke java command which will feed the byte code to the machinedependent interpreter. 1/8/2022 B. Ramamurthy 6

Java Application Program Interface (Java API) Package of related classes : java. awt java. util Date class 1/8/2022 (JAVA API) Random Dictionary java. io, java. beans, . . Etc. . B. Ramamurthy package 7

Java API : A Simplistic View API packages classes methods and data declarations 1/8/2022 B. Ramamurthy 8

Java API Classes Unlike many other languages, you will referring to the classes in the API. Where is the API? Docuemntation for the packages are in: http: //java. sun. com/j 2 se/1. 4/docs/api/ Open in it in your browser and traverse through the javadoc tree 1/8/2022 B. Ramamurthy 9

Types of Programs Java is fully object-oriented. Every “function” has to be attached to a class. You will mainly deal with three types of programs: n class: methods and data (variables + constants) describing a collection (type) of object n application: class that has a main method: represents a our regular program n applet: class that is meant for execution using a appletviewer/browser 1/8/2022 B. Ramamurthy 10

Problem Solving Using Java OO Design and Progamming in Java Identify classes needed Reuse API classes 1/8/2022 Reuse your classes Write an application class Design new classes B. Ramamurthy Write an applet class Create and use objects 11

What is an Object? Object-oriented programming supports the view that programs are composed of objects that interact with one another. How would you describe an object? Using its characteristics (has a ----? ) and its behaviors (can do ---? ) Object must have unique identity (name) : Basketball, Blue ball Consider a ball: n Color and diameter are characteristics (Data Declarations) n throw, bounce, roll are behaviors (Methods) 1/8/2022 B. Ramamurthy 12

Classes are Blueprints A class defines the general nature of a collection of objects of the same type. The process creating an object from a class is called instantiation. Every object is an instance of a particular class. There can be many instances of objects from the same class possible with different values for data. 1/8/2022 B. Ramamurthy 13

Example objects Object References red. Rose class Rose blue. Rose 1/8/2022 class B. Ramamurthy 14

Instantiation : Examples class Ford. Car ---- defines a class name Ford. Car windstar; ---- defines a Object reference wind. Star windstar = new Ford. Car(); ---- instantiates a windstar Object class House. Plan 1 { color…. House. Plan 1 blue. House; blue. House = new House. Plan 1(BLUE); House. Plan 1 green. House = new House. Plan 1(GREEN); 1/8/2022 B. Ramamurthy 15

Operator new and “dot” new operator creates a object and returns a reference to that object. After an object has been instantiated, you can use dot operator to access its methods and data declarations (if you have access permissions). EX: red. Rose. bloom(); green. House. color 1/8/2022 B. Ramamurthy 16

Elements of a Class class header modifiers, type, name body statements parameters variables, constants assignment 1/8/2022 data declarations (variables, constants) methods selection B. Ramamurthy repetition others 17

Class Structure class variables constants methods 1/8/2022 B. Ramamurthy 18

Defining Classes Syntax: class_name { data-declarations constructors methods } Constructors are special methods used for instantiating (or creating) objects from a class. Data declarations are implemented using variable and constant declarations. 1/8/2022 B. Ramamurthy 19

Naming Convention Constants: All characters in uppercase, words in the identifier separated by underscore: EX: MAX_NUM Variables, objects, methods: First word all lowercase, subsequent words start with uppercase. EX: next. Int, my. Pen, read. Int() Instance variable begin with an _ Classes: Begin with an uppercase letter. EX: Tree, Car, System , Math 1/8/2022 B. Ramamurthy 20

Debugging and Testing Compile-time Errors : Usually typos or syntax errors Run-time Errors : Occurs during execution. Example: divide by zero. Logic Errors: Software will compile and execute with no problem, but will not produce expected results. (Solution: testing, and debugging) 1/8/2022 B. Ramamurthy 21

Class Components Class name (starts with uppercase), constants, instance variables, constructors definitions and method definitions. Constants: public final static double PI = 3. 14; Variables: private double _bonus; public string _name; 1/8/2022 B. Ramamurthy 22

Operations Behaviors, methods or messages to which an object will respond to. Methods: 1. 2. 3. 4. 5. 1/8/2022 Constructors Set/get methods (mutators/accesors) Predicate methods (boolean/status indicators; Ex: is. Empty()) Utility methods (for local use only: ex: internal sort after an insert) Explicit methods or interface methods that define the interface an object offers to the world. B. Ramamurthy 23

Method Invocation/Call Syntax: method_name (values); object_name. method_name(values); classname. method_name(values); Examples: compute. Sum(); // call to method from within the class where it is located Your. Rose. paint. It(Red); Math. abs(X); 1/8/2022 B. Ramamurthy 24

Defining Methods A method is group of (related) statements that carry out a specified function. A method is associated with a particular class and it specifies a behavior or functionality of the class. A method definition specifies the code to be executed when the method is invoked/activated/called. 1/8/2022 B. Ramamurthy 25

Method Definition : Syntax visibility return_type method_name (parameter_list) { statements } 1/8/2022 B. Ramamurthy 26

Return Type can be void, type or class identifier void indicates that the method called to perform an action in a self-standing way: Example: println type or class specify the value returned using a return statement inside the method. 1/8/2022 B. Ramamurthy 27

Return Statement Syntax of return statement: return; // for void methods return expression; // for type or class return value // the expression type and return type should be same 1/8/2022 B. Ramamurthy 28

Parameter List Parameter list specified in method header provides a mechanism for sending information to a method. It is powerful mechanism for specializing an object. The parameter list that appears in the header of a method n specifies the type and name of each parameter and n is called formal parameter list. The corresponding parameter list in the method invocation is called an actual parameter list. 1/8/2022 B. Ramamurthy 29

Parameter list : Syntax Formal parameter list: This is like molds or templates (parm_type parm_name, . . ) Actual parameter list: This is like material that fit into the mold or template specified in the formal list: (expression, expression. . ) 1/8/2022 B. Ramamurthy 30

Method Definition : review definition header body Visibility modifiers return type Name parameter list { statements } 1/8/2022 B. Ramamurthy 31

Method Definition : Example Write a method that computes and returns the perimeter of a rectangle class. Analysis: n n n 1/8/2022 Send to the method: Length and Width Compute inside the method: Perimeter Return from the method: Perimeter B. Ramamurthy 32

. . . Example (contd. ) public int perimeter (int length, int width) { int temp; // local temporary variable temp = 2 * (length + width); // compute perimeter return temp; // return computed value } 1/8/2022 B. Ramamurthy 33

What happens when a method is called? Control is transferred to the method called and execution continues inside the method. Control is transferred back to the caller when a return statement is executed inside the method. 1/8/2022 B. Ramamurthy 34

Method Invocation : semantics Operating System 1 4 2 Main method 8 Rect. area(…. ) 3 7 8 area 5 method 6 1/8/2022 B. Ramamurthy 1. OS to main method 2. Main method execution 3. Invoke area 4. Transfer control to area 5. Execute area method 6. Return control back to main method 7. Resume executing main 8. Exit to OS 35

Constructors A Constructor is used to create or instantiate an object from the class. Constructor is a special method: n n It has the same name as the class. It has no return type or return statement. Typically a class has more than one constructor: a default constructor which has no parameters, and other constructors with parameters. (overloading) 1/8/2022 B. Ramamurthy 36

Constructors (contd. ) You don’t have to define a constructor if you need only a default constructor. When you want initializing constructors : 1. you must include a default constructor in this case. 2. You will use initializing constructors when you want the object to start with a specific initial state rather than as default state. 3. Example: Car my. Car = new Car(RED); // initializing constructor for Car class with color as parameter 1/8/2022 B. Ramamurthy 37

Visibility Modifiers type public protected “nothing” DEFAULT private Package Visibility Method/variable name static for class methods and variables 1/8/2022 B. Ramamurthy “nothing” DEFAULT for object methods and 38 variables

. . Modifiers (contd. ) private : available only within class “nothing” specified : DEFAULT: within class and within package protected : within inherited hierarchy (only to sub classes) public : available to any class. 1/8/2022 B. Ramamurthy 39

CLASSPATH Many times classes needed are available in a packages other than the Java API. Set the CLASSPATH environment variable of your. cshrc file to point to such packages. One such is located at /projects/bina/CSE 116/ setenv CLASSPATH. : /projects/bina/CSE 116/other paths You may now import a class from a package in this directory. 1/8/2022 B. Ramamurthy 40

Arrays Array is a numbered collection of variables all of the same type. Length attribute gives the capacity of the array Cells or the individual elements of the array distinguished by an index. Lets look at an example. Common error: Array. Index. Outof. Bounds 1/8/2022 B. Ramamurthy 41

Summary An overview of OOP, problem solving using OOP and Java language was presented. Next class we will “review” interface, abstract class, concrete class, polymorphic dispatch and relationship among classes. 1/8/2022 B. Ramamurthy 42
- Slides: 42