Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Four Defining Your Own Classes. Topics Instantiable classes Components of a class –constructors –accessors –mutators Visibility modifiers Class.

Similar presentations


Presentation on theme: "Chapter Four Defining Your Own Classes. Topics Instantiable classes Components of a class –constructors –accessors –mutators Visibility modifiers Class."— Presentation transcript:

1 Chapter Four Defining Your Own Classes

2 Topics Instantiable classes Components of a class –constructors –accessors –mutators Visibility modifiers Class diagrams revisited

3 Defining Instantiable Classes Learning how to define instantiable classes is the first step toward mastering the skills necessary in building large programs. A class is instantiable if we can create instances of the class. The DecimalFormat, GregorianCalendar, and String classes are all instantiable classes, while the Math class is not.

4 Currency converter We need two methods for conversion: fromDollar and toDollar. CurrencyConverteryenConverter; double amountInYen, amountInDollar; yenConverter = new CurrencyConverter( );... amountInYen = yenConverter.fromDollar(200); //from dollar to yen amountInDollar = yenConverter.toDollar(15000); //from yen to dollar

5 Currency converter Since the exchange rate fluctuates, we need a method to set the exchange rate. CurrencyConverteryenConverter; yenConverter = new CurrencyConverter( ); yenConverter.setExchangeRate(130.77);

6 CurrencyConverter Want to convert both to and from dollars To convert, need to know exchange rate –exchange rates fluctuate Class diagram for a CurrencyConverter object.

7 Template for Instantiable Class

8 Create multiple instances CurrencyConverter yenConverter, markConverter; double amountInYen, amountInMark, amountInDollar; yenConverter = new CurrencyConverter(); yenConverter.setExchangeRate(130.77); markConverter = new CurrencyConverter( ); markConverter.setExchangeRate(1.792); amountInYen = yenConverter.fromDollar( 200 ); amountInMark = markConverter.fromDollar( 200 ); amountInDollar= yenConverter.toDollar( 10000 ); amountInMark= markConverter.fromDollar(amountInDollar);

9 Each instance has its own data euroConverter = new CurrencyConverter(); euroConverter.setExchangeRate(0.926); yenConverter = new CurrencyConverter(); yenConverter.setExchangeRate(118.65); poundConverter = new CurrencyConverter( ); poundConverter.setExchangeRate(0.652);

10 Syntax for defining a method

11 Methods in a Class If the method declaration includes the static modifier, it is a class method. –Class methods can access only class variables and constants. If the method declaration does not include the static modifier, it is an instance method. –Instance methods can access class variables and constants, as well as instance variables.

12 Methods that return values We call a method that returns a value a value- returning method, or non-void method. A value-returning method must include a return statement in the following format: return ; public double toDollar( double foreignMoney ) { return (foreignMoney / exchangeRate); }

13 Constructors A constructor is a special method that is executed when a new instance of the class is created. –The purpose of the constructor is to initialize an object to a valid state. Whenever an object is created, we should ensure that it is created in a valid state by properly initializing all data members in a constructor. –The name of a constructor must be the same as the name of the class. –If no constructor is defined for a class, then the Java compiler will include a default constructor.

14 Syntax of a constructor.

15 Default Constructor The default constructor will have the following form: public ( ) { } –numeric instance data will be initialized to 0 public CurrencyConverter( ) { }

16 Constructors A constructor must not have a return type. It is possible to create multiple constructors for a class, as long as the constructors have either –A different number of parameters, or –Different data types for the parameters if the number of parameters is the same.

17 Multiple Constructors public MyClass( int value ) { … } public MyClass( ) { … } public MyClass( float value ) { … }

18 Information Hiding Public methods of a class determine the behavior of its instances. Internal details are implemented by private methods and private data members. –the user (client program) doesn't need to know the details Declaring the data members private ensures the integrity of the class.

19 Visibility Modifiers The modifiers public and private designate the accessibility of data members and methods. If a class component (data member or method) is declared private, no outside methods can access it. If a class component is declared public, any outside method can access it.

20 Public Members Class constants may be declared public because: –A constant is “read only” by nature –A constant is a clean way to make characteristics of the instances known to client programmers. Public class data members are accessed by the syntax.


Download ppt "Chapter Four Defining Your Own Classes. Topics Instantiable classes Components of a class –constructors –accessors –mutators Visibility modifiers Class."

Similar presentations


Ads by Google