Presentation is loading. Please wait.

Presentation is loading. Please wait.

4. Writing Classes Based on Java Software Development, 5 th Ed. By Lewis &Loftus.

Similar presentations


Presentation on theme: "4. Writing Classes Based on Java Software Development, 5 th Ed. By Lewis &Loftus."— Presentation transcript:

1 4. Writing Classes Based on Java Software Development, 5 th Ed. By Lewis &Loftus

2 Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

3 User-defined Classes Java program consists of a set of classes. Java program consists of a set of classes. One class must contain a method namd main()—which becomes the starting point of the program. One class must contain a method namd main()—which becomes the starting point of the program. We have been using classes from the Java Standard Library (API). We have been using classes from the Java Standard Library (API). You will now write your own classes. You will now write your own classes.

4 Classes & Objects Class has Class has Name Name State (attributes) State (attributes) Behavior (methods) Behavior (methods) E.g., to represent dice in a game program: E.g., to represent dice in a game program: Name: Die (singular of dice) Name: Die (singular of dice) Attributes: MAXVALUE, faceValue Attributes: MAXVALUE, faceValue Methods: constructor, roll(), toString(), getFacevalueValue() Methods: constructor, roll(), toString(), getFacevalueValue()

5 Classes A class can contain data declarations and method declarations A class can contain data declarations and method declarations MAXVALUE, faceValue Data declarations Method declarations

6 Classes & Objects Necessary methods Necessary methods Constructor – to create an object of Die class Constructor – to create an object of Die class toString – returns the String value that represents the object in some way toString – returns the String value that represents the object in some way getFaceValue – to return the current face value of a die getFaceValue – to return the current face value of a die RollingDice.java RollingDice.java RollingDice.java Die.java Die.java Die.java

7 Constructor Method Constructor method(s) is used to instantiate objects. Constructor method(s) is used to instantiate objects. It can set initial values for objects. It can set initial values for objects. It has the same name as the class name. It has the same name as the class name. It has no return type. It has no return type. E.g., Die(){…} Student (String name, int age){…} E.g., Die(){…} Student (String name, int age){…}

8 toString() Method Every class should include a toString() method, which returns some String value representing an object of the class. Every class should include a toString() method, which returns some String value representing an object of the class. It is called automatically when an object is passed to the System.out.println() mthod. It is called automatically when an object is passed to the System.out.println() mthod. E.g., String toString() { return “Die object”; } E.g., String toString() { return “Die object”; }

9 Scope of Data Scope of data Scope of data Area in the program where data can be referenced (can be used) Area in the program where data can be referenced (can be used) Data declared at the class level can be reference in all methods of the class (global visibility). Data declared at the class level can be reference in all methods of the class (global visibility). Data declared in a method can be referenced only in that method (local visibility). Data declared in a method can be referenced only in that method (local visibility).

10 Instance Data In class Die, faceValue is called instance data, because each instance of Die maintains its own memory for faceValue with a value. In class Die, faceValue is called instance data, because each instance of Die maintains its own memory for faceValue with a value. Each instance (object) of class Die shares its methods, but maintains its own data space. Each instance (object) of class Die shares its methods, but maintains its own data space. Every time class Die is instantiated, a new memory for faceValue is allocated. Every time class Die is instantiated, a new memory for faceValue is allocated.

11 UML Diagram UML (Unified Modeling Language) diagrams show relationships among classes and objects. UML (Unified Modeling Language) diagrams show relationships among classes and objects. RollingDice main (args : String[]) : void Die faceValue : int Die() roll() : int setFaceValue (int value) : void getFaceValue() : int toString() : String

12 Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

13 Encapsulation Two views of a class Two views of a class Internal (all variables and code visible) Internal (all variables and code visible) External (only public elements are visible) External (only public elements are visible) Interface Interface Method names with parameters—no internal details of method body Method names with parameters—no internal details of method body Methods and instance variables are encapsulated – combined into a single entity, for the purpose of Methods and instance variables are encapsulated – combined into a single entity, for the purpose of Information hiding Information hiding Data and behavior abstraction Data and behavior abstraction

14 Visibility Modifiers Modifier specifies some characteristic of method or data—e.g., final. Modifier specifies some characteristic of method or data—e.g., final. Visibility Modifiers Visibility Modifiers public public Can be referenced from anywhere Can be referenced from anywhere protected protected Can be referenced from subclasses (derived from a class) Can be referenced from subclasses (derived from a class) private private Visible only from within the class Visible only from within the class default default Visible to all classes within the same package Visible to all classes within the same package

15 Visibility Modifiers Instance variables Instance variables Should not be declared public Should not be declared public Should not be accessible directly Should not be accessible directly Should be accessed or modified via public methods Should be accessed or modified via public methods Methods Methods that provide services should be public that provide services should be public that provide support for methods should not be public that provide support for methods should not be public Constants Constants Can be public, because they cannot be modified Can be public, because they cannot be modified

16 Accessors and Mutators Given: class Fraction Given: class Fraction Accessors Accessors Methods which return the value of an instance variable Methods which return the value of an instance variable E.g., getNumerator(), getDenominator() E.g., getNumerator(), getDenominator() Mutators Mutators Mehtods which change the value of an instance variable Mehtods which change the value of an instance variable E.g., setNumerator(), setDenominator() E.g., setNumerator(), setDenominator() Required for each instance variable Required for each instance variable

17 Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

18 Method Header A method declaration begins with a method header A method declaration begins with a method header boolean isEven (int num) method name return type parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal parameter

19 Method Body The method header is followed by the method body The method header is followed by the method body Boolean isEven (int num) { boolean result; if (num % 2 == 0) result = true; else result = false; return result; } The return expression must be consistent with the return type result is a local variable. It is created each time the method is called, and is destroyed when it finishes executing

20 Invoking a Method boolean isEven (int num) { boolean result; if (num % 2 == 0) result = true; else result = false; return result; } If (isEven(a * b - c)){ … } else{ … } Actual parameter Formal parameter The value of actual parameter is copied to the formal parameter

21 Example Programs Transactions.java Transactions.java Transactions.java Account.java Account.java Account.java

22 Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields


Download ppt "4. Writing Classes Based on Java Software Development, 5 th Ed. By Lewis &Loftus."

Similar presentations


Ads by Google