Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look MSIS670: Object-Oriented Software Engineering Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look 12/30/2018 4.1
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/30/2018
Implementing a Time Abstract Data Type with a Class: Time2. java (p 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. 12/30/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 ); … 12/30/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 26-31 of class Time1, Time1 still com[piles 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). 12/30/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). 12/30/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. 12/30/2018
Lab Activities (Week 4) Coding Assignment 4 class MyRectangle Two .java files. Both should be saved on a same project. Similar to the Time2 example. For more advanced: use GUI application to test multiple constructors 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/30/2018