UserDefined Classes The String class is a predefined






























- Slides: 30
User-Defined Classes The String class is a pre-defined class that is provided by the Java Designer. Sometimes programmers would like to create their own reference data types, or class definitions. Programmers will write a class provider to implement a reference data type. A class provider is like a program that we have been writing, except that it does not have a main method. 1
Classes dan Objects A Class in object-oriented modeling is a template for creating objects. An object is an instance of a class. Objects n n know things (data) know how to do things (methods) The Class provider defines the data and the methods for the class. 2
Contoh – Phone. Card class The following Phone. Card class represents a pre-paid mobile phone card. name of the class attributes (data that is to be stored) methods 3
Attribut The attributes phone. Number and balance are encapsulated with each Phone. Card object. The values of these attributes will differ with each instance of Phone. Card n n my Phone. Card may have phone. Number 0121122334 and balance of RM 100. 00 your Phone. Card may have phone. Number 0111093823 and balance of RM 3. 50 The attributes are usually private, which means they are not directly accessible. 4
Phone. Card Methods public Phone. Card(String phone. Number, double balance) n the constructor, which creates a new Phone. Card object. public String get. Phone. Number() n returns the value of phone. Number public double get. Balance() n returns the value of balance public void set. Phone. Number(String new. Phone. Num) n sets the value of phone. Number to new. Phone. Num public void set. Balance(double new. Balance) n sets the value of balance to new. Balance public void top. Up(double amount) n tops up the Phone. Card's balance by amount. public boolean make. Call(double duration, double cost. Per. Min) n reduces the Phone. Card's balance by the cost of the call. public String to. String() n returns a String containing information about the Phone. Card object. 5
Bagaimana objects dibuat The constructor call creates a new object: my. Card = new Phone. Card("012 -1122334", Phone. Card 20); my. Card 6
Bagaimana objects dibuat You can create many objects, each with its own reference: Phone. Card my. Card = new Phone. Card("012 -1122334", 20); Phone. Card extra. Card = new Phone. Card("012 -1234567", 10); my. Card extra. Card 7
Menjalankan Method Obyek We invoke a method for the object by specifying the right object and calling the appropriate method with the parameters. System. out. println(extra. Card. get. Balance()); my. Card Invokes this method for the object called extra. Card 8
Membuat class Phone. Card You can write the class provider for the Phone. Card class by specifying: n n the instance variables the class methods: Constructors Writer methods Reader methods Query methods Custom methods 9
Instance Variables The instance variables are the variables which hold data specific to the class, or the attributes. For the Phone. Card class, they are: n n phone. Number balance The instance variables are declared as private so that the information is hidden from other classes. 10
Definisi Class public class Phone. Card { // attributes private String phone. Number; private double balance; //methods go here … } class in Java class in UML notation 11
Constructors are needed to create objects of the class. A Constructor is a method with the same name as the class. We may write two kinds of constructors: n n Default Constructor, takes no arguments and sets instance variables to default values Constructor with arguments which sets instance variables to the values of the arguments passed in. 12
Contoh – Default Constructor public class Phone. Card { // attributes private String phone. Number; private double balance; constructor has the same name as the class, and no return type. // default Constructor, no arguments public Phone. Card() { phone. Number = "" // empty string balance = 0. 0; } the constructor initializes the instance variables when a new Phone. Card object is created. 13
Constructor dengan Arguments public class Phone. Card { // attributes private String phone. Number; private double balance; // Constructor with arguments public Phone. Card(String in. Number, double in. Balance) { phone. Number = in. Number; if (in. Balance > 0) // check validity balance = in. Balance; else balance = 0. 0; } arguments used to initialize instance variables 14
Reader/ Accessor Methods Although the instance variables are declared private, they can still be retrieved via methods (within the class). Reader methods are used to retrieve the values of the instance variables. Reader methods are also known as getters because they are commonly named get. XXX where XXX is the name of the instance variable. 15
Reader/accessor Methods (getters) public class Phone. Card { The return type is String phone number returned the double value balance returned // attributes private String phone. Number; private double balance; //accessor methods public String get. Phone. Number() { return phone. Number; } public double get. Balance() { return balance; } This getter 'gets' the phone Number This getter 'gets' the balance 16
Writer/Mutator Methods (setters) Similarly, the values held by the instance variables can be changed by using methods. Writer methods are used to change the values. Writer methods are also known as setters because they are commonly named set. XXX where XXX is the name of the instance variable. 17
Writer Methods public class Phone. Card { // attributes private String phone. Number; private double balance; This setter 'sets' the phone number to new. Number //writer methods public void set. Phone. Number(String new. Number) { phone. Number = new. Number; } public void set. Balance(double new. Balance) { if (new. Balance > 0) balance = new. Balance; } Some validation may be performed. Return type is void. 18 18
Query Methods The class provider usually has a to. String method that returns a String containing information about the object's public class Phone. Card current data. { // attributes private String phone. Number; private double balance; // to. String returns information about Phone. Card public String to. String() { return phone. Number + " has balance of " + balance; } 19
Standard Methods We say that the Constructor, n Getter, n Setter and n Query n methods are standard methods because we expect them to be available for most class providers. 20
Custom Methods or Services When we create a new class provider, that class will probably have methods that provide special services. For the Phone. Card class, we want to allow Phone. Card objects to: n n be 'topped-up': that is, to increase the balance in the card record that calls have been made: to decrease the balance in the card. We have to write methods to carry out the above functions. 21
Topping Up the Phone. Card The top. Up method takes in one argument representing the top-up amount and adds it to the existing balance if it is non-negative. public class Phone. Card { // attributes private String phone. Number; private double balance; // Method to top up the balance by amount public void top. Up(double amount) { if (amount > 0) balance += amount; } 22
Making a Call with the Phone. Card The make. Call method takes in two arguments: one representing the duration of the call in minutes and one representing the cost of the call per minute. The cost is calculated and subtracted from the balance. If the balance becomes negative, the balance is set to zero and the boolean value false is returned. Otherwise, the boolean value true is returned to indicate a successful call made. 23
make. Call method // Method to record calls made, thus reducing balance public boolean make. Call(double duration, double cost. Per. Min) { double cost = duration * cost. Per. Min; if (cost > 0) balance -= cost; else return false; // invalid parameters if (balance < 0) { balance = 0; return false; } return true; } // set to zero 24
System - specific code We should be careful not to use system-specific code when writing a class provider. For example, a statement containing System. out. println() only displays output to the console screen. This statement cannot be used when using the Phone. Card class in a GUI application or an Applet. This is why reader methods or query methods are used to obtain data. The driver program will then manipulate the data. 25
Sample Class Provider The Phone. Card class we have defined can now work. See the class Phone. Card. java for the complete class definition. Test the class using Test. Phone. Card. class 26
Latihan Write the class provider for a class called Rectangles have n length n width Write the n default constructor: length and width set to zero n constructor with arguments to set the length and width as long as they are non-negative n reader methods n writer methods n method to calculate the area n method to calculate the perimeter n to. String method 27
Latihan Now test your Rectangle class using a driver program. n n n Create one rectangle using the no-args Constructor Create another rectangle using the arguments 5. 5 for length and 7. 8 for width. Set the length and width of the first rectangle using the writer methods Find and display the perimeter of both rectangles. Display information about the rectangle with the larger area. 28
The Circle class A Circle class provider has been defined: it can be used to create Circle objects. Test the Circle class: public Circle() • creates a Circle object with radius 0. 0 public Circle(double radius) • creates a Circle object with the required radius. public double get. Radius() • returns the radius of the Circle public void set. Radius(double radius) • sets the radius of the Circle to the required value public double area() • returns the area of the Circle public double circumference() • returns the circumference of the Circle. 29
Other Classes What data and methods would you define for the following objects? Bank Account n Student n Library Book n Car n Write the class providers. 30