Beruflich Dokumente
Kultur Dokumente
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
The Class provider defines the data and the methods for the class.
2
methods
Attribut
The attributes phoneNumber and balance are encapsulated with each PhoneCard object. The values of these attributes will differ with each instance of PhoneCard
my PhoneCard may have phoneNumber 0121122334 and balance of RM100.00 your PhoneCard may have phoneNumber 0111093823 and balance of RM3.50
The attributes are usually private, which means they are not directly accessible.
4
PhoneCard Methods
public PhoneCard(String phoneNumber, double balance)
the constructor, which creates a new PhoneCard object. returns the value of phoneNumber returns the value of balance sets the value of phoneNumber to newPhoneNum sets the value of balance to newBalance tops up the PhoneCard's balance by amount. reduces the PhoneCard's balance by the cost of the call. returns a String containing information about the PhoneCard object.
5
myCard
myCard
extraCard
myCard
extraCard
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 PhoneCard class, they are:
phoneNumber balance
The instance variables are declared as private so that the information is hidden from other classes.
10
Definisi Class
PhoneCard phoneNumber balance PhoneCard( ) getPhoneNumber( ) getBalance( ) setPhoneNumber( ) setBalance( ) makeCall( ) topUp( ) toString( )
public class PhoneCard { // attributes private String phoneNumber; private double balance; //methods go here
class in Java
Constructors
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:
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
// default Constructor, no arguments public PhoneCard() { phoneNumber = "" // empty string balance = 0.0; }
the constructor initializes the instance variables when a new PhoneCard object is created.
13
// Constructor with arguments public PhoneCard(String inNumber, double inBalance) { phoneNumber = inNumber; if (inBalance > 0) // check validity balance = inBalance; else balance = 0.0; }
14
16
Writer Methods
public class PhoneCard { // attributes private String phoneNumber; private double balance; This setter 'sets' the phone number to newNumber //writer methods public void setPhoneNumber(String newNumber) { phoneNumber = newNumber; }
Query Methods
The class provider usually has a toString method that returns a String containing information about the object's public class PhoneCard current data. {
// attributes private String phoneNumber; private double balance;
// toString returns information about PhoneCard public String toString() { return phoneNumber + " has balance of " + balance; }
19
Standard Methods
We say that the
methods are standard methods because we expect them to be available for most class providers.
20
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.
makeCall method
// Method to record calls made, thus reducing balance public boolean makeCall(double duration, double costPerMin) { double cost = duration * costPerMin; if (cost > 0) balance -= cost; else return false; // invalid parameters if (balance < 0) { balance = 0; return false; } return true;
// set to zero
24
26
Latihan
Write the class provider for a class called Rectangle. Rectangles have length width Write the default constructor: length and width set to zero constructor with arguments to set the length and width as long as they are non-negative reader methods writer methods method to calculate the area method to calculate the perimeter toString method
27
Latihan
Now test your Rectangle class using a driver program.
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
Other Classes
What data and methods would you define for the following objects?