Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 3 Object-based Programming: Classes and Objects

Similar presentations


Presentation on theme: "Week 3 Object-based Programming: Classes and Objects"— Presentation transcript:

1 Week 3 Object-based Programming: Classes and Objects
MSIS670: Object-Oriented Software Engineering Week 3 Object-based Programming: Classes and Objects 12/5/2018 3.1

2 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. 12/5/2018

3 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. 12/5/2018

4 Declaring a Class 12/5/2018 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. 12/5/2018

5 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!" 12/5/2018

6 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. 12/5/2018

7 Constructor 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 (next 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. 12/5/2018

8 Lab Activities (Week 3) Coding Assignment 3 class MyRectangle
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. 12/5/2018


Download ppt "Week 3 Object-based Programming: Classes and Objects"

Similar presentations


Ads by Google