IFS410: Advanced Analysis and Design

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming Logic and Design Seventh Edition
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Object-Oriented Programming: Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Week 4 Object-Oriented Programming (1): Inheritance
Dr Shahriar Bijani Winter 2017
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Introduction to Classes and Objects
Defining Classes and Methods
CS360 Client/Server Programming Using Java
Object Oriented Programming in java
CIS 199 Final Review.
Introduction to Classes and Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Classes and Objects Systems Programming.
C++ Object Oriented 1.
Presentation transcript:

IFS410: Advanced Analysis and Design Week 3 Object-based Programming: Classes and Objects 11/23/2018 3.1

Programming Languages Procedural Programming Language Action-oriented Functions are units of programming Object-based Programming Language Objects/classes are units Encapsulation and Interfaces Object-oriented Programming Language Other “goodies” – Polymorphism, etc. Object-Oriented Programming Languages (such as Java) encapsulates data (attributes) and methods (behavior) in objects. Objects may allow other objects to communicate their encapsulated members through well-defined interfaces. Inheritance, Polymorphism, and other features that are considered as characteristics of true OOPL, will be discussed in the Chapter 9. In this section, the main focus is creating and using the object, which are enabled by object-based programming languages. 11/23/2018

Object-based Programming How to create objects How to use objects So far, we have learned how to create a Java application. Applets are mentioned briefly. Here, we introduce a third type of a class, which defines a type of objects. A Java application will self-execute by the method “main()”. A Java applet extends JApplet and also self-execute by the methods “init(), start(), and paint()”. This third type of classes will not execute by itself. When called from other objects (methods), this class simply creates an object described in this class, initializes the instance variables by its constructor, and waits for other instructions. 11/23/2018

Declaring a Class Fig 3.1: Class Declaration with one method The GradeBook class contains a displayMessage method )lines 7-10) that displays a message on a screen. This class does not have a method “main”. A class that has a main method is a Java application. Such a class is special because the Virtual Machine can use main to begin execution. Class GradeBook is not an application because it does not have main. Therefore, if youtry to execute GradeBook, you will get an error message. 11/23/2018

Instantiating an Object of a Class Fig. 3.2: Creating an Object of class GradeBook and calling its displayMessage method Now, this class, GradeBookTest, is an application, as it has a method “main”. Within the main method, this class is declaring a variable – myGradeBook. Variable myGradeBook is initialized with the result of the class instance creation expression – new GradeBook(). Keyword new creats a new object of the class specified by the “new.” Next, an expression myGradeBook.displayMessage(); Is calling the object’s method – displayMessage – by specifying which object (myGradeBook) and with dot separator (“.”) a name of method that the object should have defined. When executing this application after compiling both files, it will display the message "Welcome to the Grade Book!" 11/23/2018

Instance Variables, set Methods and get Methods Fig. 3.7: GradeBook class that contains a courseName as instance variable This class modifies the Fig. 3.1; not only the “displayMessage()” method, it has two more methods, and a declaration of an instance variable – courseName. Note that the instance variable is outside of any methods, and declared as private. Nobody else but the methods of objects of this class can touch that. In order to assign the value to the instance variable, this class uses the method –setCourseName( String name ) – where, “name” is the value. Also it has a getCourseName() method, which returns a String value of the contents of the variable courseName. Finally, the displayMessage() method will utilize this getCOurseName() method to display the value. 11/23/2018

Constructor Class Constructor Same name as class Initializes instance variables of a class object Called when program instantiates an object of that class When you want to use an object, which is defined by the programmer, a class (object) can call a class (actually class constructor) to instantiate an object of the class. ClassName ref = new ClassName( arguments ); ClassName indicates type of object created, ref is the reference to that object (used as a variable), and new indicates that a new object is created. Arguments specifies constructor argument values. This argument should matches the constructor arguments (number of variables, and each type) – c.f. Overloaded Constructors. e.g., Time1Test line 9, Time1 time = new Time1(); time is a new object of the type “Time1”, with no argument required. Then constructor initializes the object’s instance variables according to the specification of its constructor. 11/23/2018

Initializing Objects with Constructors pp. 366-368 Time2.java defines the class Time Time2Test.java defines the class Time2Test, which is an application. Public classes must be defined in separate files. Every Java class must extend another class. If not extend a class, it implicitly extends Object. 11/23/2018

Testing Constructors 11/23/2018

Implementing a Time Abstract Data Type with a Class: Time2.java pp. 366-368 Time2.java defines the class Time Time2Test.java defines the class Time2Test, which is an application. Public classes must be defined in separate files. Every Java class must extend another class. If not extend a class, it implicitly extends Object. 11/23/2018

Time2.java Class Constructor Same name as class Initializes instance variables of a class object Called when program instantiates an object of that class Class can have several constructors through overloading. Time2.java lines 12-15 vs 18-21 (and others) When you want to use an object, which is defined by the programmer, a class (object) can call a class (actually class constructor) to instantiate an object of the class. ClassName ref = new ClassName( arguments ); ClassName indicates type of object created, ref is the reference to that object (used as a variable), and new indicates that a new object is created. Arguments specifies constructor argument values. This argument should matches the constructor arguments (number of variables, and each type) – c.f. Overloaded Constructors. e.g., Time2Test lines 8-13, Time2 t1 = new Time2(); t1 is a new object of the type “Time2”, with no argument required. Then constructor initializes the object’s instance variables according to the specification of its constructor. 11/23/2018

Using Overloaded Constructors Methods in a class may have the same names. Must have different arguments. Different in number of parameters. Different in data types. e.g., (P. 366-367). public Time2(); public Time2( int h ); … 11/23/2018

Overriding Methods Method Object.toString Gives String representation of object But we want it to be a standard time format Time2 overrides method toString Lines 97-102 Time2.toString is now different from Object.toString If we remove lines 97-102 of class Time2, Time2 still compiles correctly. When a message calls for toString method, it uses method java.lang.Object.toString; giving String representation of object (not in a universal time format). 11/23/2018

toString() Method in Time2.java Given a set of parameters (values of variables hour, minute, and second), this method toString gives back a String in a format of hh:mm:ss (in which hh is up to 12 format, not 24 hour format). twoDigits is another object of a class called DecimalFormat, which has a method called format that changes the view of a number into a two digit (even it is a single digit). 11/23/2018

Controlling Access to Members Member Access Modifiers Control access to class’ instance variables and methods public Variables and methods are accessible to class clients private Variables and methods not accessible to class clients Accessor / Mutator methods Designers use private data and public methods to enforce the notion of information hiding and the principle of least privilege. By doing so, the programmer of the class controls how the class’ data is manipulated. This is the principle of data encapsulation. It makes easier to debug the program because if there is a problem with data manipulation, the problem is localized in the same class’s methods. 11/23/2018

Using set and get methods Mutator method – set public method Allow clients to modify private data Accesor method – get Allow clients to read private data The set and /or get methods are not mandatory for each of the private data. Sometimes it is completely hidden from outside. Only if it makes sense to provide such methods, you should provide them. 11/23/2018

Package Access When no access modifier (public, private, etc.) is provided for a method or variable, it is considered to have package access. When multiple classes are packaged together, these classes can access each other’s package-access methods and data directly from a reference of an object. As we have been seeing in the previous examples, we can use packages that were created before (and stored somewhere) in a class we are creating by importing them. The statement before class declaration, e.g., import java.awt.*, imports the java core package and we can use the public classes and public methods from those packages. We can also create a package by ourselves. Usually a package is a directory and subdirectory (directory com/deitel/jhtp4/ch08 is expressed as com.deitel.jhtp4.ch08) or project in JCreator. Put related classes and interfaces together in a package help programmers manage complexity of application components and facilitate the software reuse. A class file can have only one package statement as the file is stored only in one directory. 11/23/2018

Package Access Above figure (Fig. 8.20 on p. 397) shows the example of usage of package access. Notice that this file has two class declarations. One class, PackageDataTest is a public class, but the other, PackageData is not declared as public. Remember that there is only one public class in one file, but you can have many other classes if they are not public. The multiple classes created by one file will be stored in the same package, and thus, they can access to the package access methods in the other classes. 11/23/2018

Lab Activities (Week 3) Coding Assignment 3 class MyDate Two .java files. Both should be saved on a same project. Similar to the GradeBook example. For this assignment, you have to create a class MyRectangle and another class to test this class. Place them together in a package (project) before you start coding them. Turn in the coding as usual for both files. 11/23/2018