Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.

Similar presentations


Presentation on theme: "COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable."— Presentation transcript:

1 COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable classes *Components of a class *Visibility modifiers *Class diagrams revisited *Methods Local variables Parameter passing Memory allocation for parameters

2 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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, String and Planet classes are all instantiable. Math and JOptionPane and main classes we’ve looked at are not.

3 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Example: Currency Converter *We want to define a class that allows us to convert between dollars and some foreign currency. *What behavior do we need? A way to convert from dollars to the other currency A way to convert from the other currency back to dollars *What information is needed? Exchange rate

4 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Currency converter *We need two methods for conversion: fromDollar and toDollar. CurrencyConverteryenConverter; double amountInYen, amountInDollar; yenConverter = new CurrencyConverter( );... //from dollar to yen amountInYen = yenConverter.fromDollar(200); //from yen to dollar amountInDollar = yenConverter.toDollar(15000);

5 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Creating 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);

8 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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);

9 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Syntax for defining a method

10 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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.

11 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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); }

12 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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.

13 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Syntax of a constructor *.*.

14 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Default Constructor *The default constructor will have the following form: public ( ) { } Numeric instance data will be initialized to 0 Object instance variables are initialized to null public CurrencyConverter( ) { }

15 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Constructors *A constructor must not have a return type. If you give it one, it will not be considered to be a constructor! The compiler won’t give an error if you put a return type on a method whose name is the same as the name of the class.

16 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Multiple Constructors public MyClass( int value ) { … } public MyClass( ) { … } public MyClass( float value ) { … } *It is possible to create multiple constructors for a class, as long as the constructors have either A different number of parameters Different data types for the parameters if the number of parameters is the same.

17 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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.

18 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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. *If no modifier is given, the component has package access.

19 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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.

20 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Local Variables *A local variable is a variable that is declared within a method body. Local variables are accessible only from the method in which they are declared. Memory space for local variables is allocated only during the execution of the method. When the method execution completes, memory space will be cleared. The parameters of a method are local to the method.

21 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Method with local variables public fromDollar( double dollar) { double amount, fee; fee = exchangeRate - feeRate; amount = dollar * fee; return amount; } *dollar is a parameter *amount and fee are local variables *none of them can be used outside the method

22 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Local Variables *Memory for local variables is allocated when the method is called before calling from dollar after local variables are declared (in fromDollar) amt = yenConverter.fromDollar( 200); double amount, fee;

23 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Local Variables after computing values *Memory for local variables is released when the method returns amt = yenConverter.fromDollar( 200); fee = exchangeRate - feeRate; amount = dollar * fee;

24 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Calling a method from a different class *Dot notation is required when calling a method of a different object public class Aclass { public void myMethod{ Bclass obj = new Bclass(); obj.doWork(); }

25 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Using a method in the class it belongs to *Dot notation is optional when calling a method of the same object public class Bclass { public void myMethod{ doWork(); } *If we want to use dot notation in this case, we use this to refer to the current object

26 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Parameter Passing *When a method is called, the value of the argument is passed to the matching parameter, and separate memory space is allocated to store this value. *This way of passing the value of arguments is called a pass-by-value, or call-by-value scheme.

27 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Parameter Passing *The data type of the argument must be assignment-compatible with the data type of the matching parameter. the same type as the parameter a type that can be automatically promoted to that type

28 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Memory Allocation during method calls *Local variables do not exist before the method starts executing x = 10; y = 20; *Memory for myMethod is allocated and the values of the arguments are copied to the parameters tester.myMethod( x, y);

29 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Parameter Memory *Values of the parameters are changed public void myMethod( int one, double two) { one = 25; two = 35.4;} *Memory for myMethod is deallocated so modified parameter values disappear Arguments are unchanged

30 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Objects as Parameters *Passing and returning objects follow the same process as passing and returning primitive data types. The only difference is that with objects, the value being passed is the reference (or address) to an object. *When a variable is an object name, the value of the variable is the address in memory where the object is stored. *The effect of passing this value, or reference, is to have two variables (object names) referring to the same object.

31 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Kennel Classes

32 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. How objects are passed to methods *From TestKennel program kennel.board( latte); public class Kennel { public board( Pet pet) {// body of board method } }

33 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Parameter Modification *weight of pet is modified in board() *After board() executes memory for reference parameter deallocated latte has new weight

34 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Returning an Object *Local variables don't exist before method call Weight weight; weight = latte.vanityWeight( ) *Memory for local variables is allocated when method is called

35 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Returning an Object *vanityWeight is computed and set in the Weight object vanityWeight.setWeight (1.5*wgt); return vanityWeight; *The location of the object vanityWeight refers to is copied into weight *memory for vanityWeight is deallocated


Download ppt "COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable."

Similar presentations


Ads by Google