Problem Solving, Object-Oriented Design and Java

Slides:



Advertisements
Similar presentations
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Advertisements

Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Chapter 10 Classes Continued
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
The Java Programming Language
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Lecture 2 Software Concepts Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Creating a Java Application and Applet
Classes, Interfaces and Packages
Object-Oriented Design Bina Ramamurthy SUNY at Buffalo.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Java Computer Industry Lab. 1 Programming Java Java Basics Incheon Paik.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Object-Oriented Design
JAVA MULTIPLE CHOICE QUESTION.
Working with Java.
Java Course Review.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
B.Ramamurthy Copyright 1998 CSE Department
Introduction to Object-oriented Program Design
Your First Java Application
Defining Classes and Methods
Chapter 4: Writing classes
Object-oriented Design in Processing
Object-Oriented Programming
Chapter 1: Computer Systems
Java Applets.
Chapter 4 Writing Classes.
Object-Oriented Programming
Object-oriented Design in Processing
Problem Solving, Object-Oriented Design and Java
Focus of the Course Object-Oriented Software Development
Object-Oriented Programming
B.Ramamurthy Copyright 1998 CSE Department
Object Oriented Programming in java
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Defining Classes and Methods
Introducing Java.
Object-oriented Design in Processing
Final and Abstract Classes
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Object-oriented Design in Processing
Presentation transcript:

Problem Solving, Object-Oriented Design and Java B.Ramamurthy 1/11/2019 B.Ramamurthy

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

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

Conventional Compiled Languages C++source Mac Compiler Mac Hardware PC Hardware PC Compiler Sun Compiler Sun Hardware 1/11/2019 B.Ramamurthy

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

“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 machine-dependent interpreter. 1/11/2019 B.Ramamurthy

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

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

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: /util/lang/jdk1.2.1/docs/index.html Open in it in your browser and traverse through the javadoc tree 1/11/2019 B.Ramamurthy

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: class: methods and data (variables + constants) describing a collection (type) of object application: class that has a main method: represents a our regular program applet: class that is meant for execution using a appletviewer/browser 1/11/2019 B.Ramamurthy

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

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: Color and diameter are characteristics (Data Declarations) throw, bounce, roll are behaviors (Methods) 1/11/2019 B.Ramamurthy

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/11/2019 B.Ramamurthy

Example objects Object References redRose class Rose blueRose class 1/11/2019 B.Ramamurthy

Instantiation : Examples class FordCar ---- defines a class name FordCar FordCar windstar; ---- defines a Object reference windStar windstar = new FordCar(); ---- instantiates a windstar Object class HousePlan1 { color…. HousePlan1 blueHouse; blueHouse = new HousePlan1(BLUE); HousePlan1 greenHouse = new HousePlan1(GREEN); 1/11/2019 B.Ramamurthy

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: redRose.bloom(); greenHouse.color 1/11/2019 B.Ramamurthy

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

Class Structure class variables constants methods 1/11/2019 B.Ramamurthy

Defining Classes Syntax: class 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/11/2019 B.Ramamurthy

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: nextInt, myPen, readInt() Classes: Start with an uppercase letter. EX: Tree, Car, System , Math 1/11/2019 B.Ramamurthy

A complete example Problem Statement: You have been hired to assist in an secret encryption project. In this project each message (string) sent out is attached to a randomly generated secret code (integer) between 1 and 999. Design and develop an application program in Java to carry out this project. 1/11/2019 B.Ramamurthy

Identify Objects There are two central objects: Message Secret code Is there any class predefined in JAVA API that can be associated with these objects? Yes , “string” of java.lang and “Random” of java.util 1/11/2019 B.Ramamurthy

The Random class Random class is defined in java.util package. nextInt() method of Random class returns an integer between 0 and MAXINT of the system. 1/11/2019 B.Ramamurthy

Design Class Random Class String An instance of Random An instance of string Class Random An instance of Random number generator Input and fill up message. Generate Random integer Attach (concatenate) Output combined message. 1/11/2019 B.Ramamurthy

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 correction) 1/11/2019 B.Ramamurthy

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/11/2019 B.Ramamurthy

Method Invocation/Call Syntax: method_name (values); object_name.method_name(values); classname.method_name(values); Examples: computeSum(); // call to method from within the class where it is located YourRose.paintIt(Red); Math.abs(X); 1/11/2019 B.Ramamurthy

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/11/2019 B.Ramamurthy

Method Definition : Syntax visibility return_type method_name (parameter_list) { statements } 1/11/2019 B.Ramamurthy

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/11/2019 B.Ramamurthy

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/11/2019 B.Ramamurthy

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 specifies the type and name of each parameter and is called formal parameter list. The corresponding parameter list in the method invocation is called an actual parameter list. 1/11/2019 B.Ramamurthy

Parameter list : Syntax Formal parameter list: This is like molds or templates (parm_type parm_name, 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/11/2019 B.Ramamurthy

Method Definition : review header body Visibility modifiers parameter list return type Name { statements } 1/11/2019 B.Ramamurthy

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

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

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/11/2019 B.Ramamurthy

Method Invocation : semantics Operating System 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 1 8 2 Main method Rect.Area(….) 3 7 4 8 Area method 5 6 1/11/2019 B.Ramamurthy

Constructors A Constructor is used to create or instantiate an object from the class. Constructor is a special method: 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. 1/11/2019 B.Ramamurthy

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 myCar(Red); // initializing constructor for Car class with color as parameter 1/11/2019 B.Ramamurthy

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

..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/11/2019 B.Ramamurthy

CLASSPATH Many times classes needed are available in a packages other than the Java API. Set the CLASSPATH environment variable to point to such packages. Assume that one such is located at /projects/bina/cse116/ setenv CLASSPATH .:/projects/bina/cse116/ You may now import a class from a package in this directory. 1/11/2019 B.Ramamurthy

Applet An applet is a Java program that operates within a browser and hence can appear in a web page. It can be fetched from a remote computer and run on the local computer. Applet’s enabling technologies: Java Virtual Machine (JVM) Interpretation rather than compilation 1/11/2019 B.Ramamurthy

Requirements Besides the classes, an applet requires a .html file and a tool for viewing the applet (Ex: appletviewer of JDK) An applet extends Applet class of java.applet package. import java.applet.Applet; An applet should have at least an init() or a paint() method (just as an application should have main() method) 1/11/2019 B.Ramamurthy

Applet Class Applet class has four important methods: init() start() stop() destroy() Besides these paint() method of component class is commonly used for drawing graphics in an applet window. 1/11/2019 B.Ramamurthy

Applet Runtime Structure html file .html Applet file .java javac includes appletviewer Java runtime JVM Applet file .class 1/11/2019 B.Ramamurthy

Summary An overview of OOP, problem solving using OOP and Java language was presented. 1/11/2019 B.Ramamurthy