Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Based on Java Software Development, 5th Ed. By Lewis &Loftus
4. Writing Classes Based on Java Software Development, 5th 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.
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). You will now write your own classes.

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

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

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

7 Constructor Method Constructor method(s) is used to instantiate objects. It can set initial values for objects. It has the same name as the class name. It has no return type. 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. It is called automatically when an object is passed to the System.out.println() mthod. E.g., String toString() { return “Die object”; }

9 Scope of Data Scope of data
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 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. 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.

11 UML Diagram 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 Interface
Internal (all variables and code visible) External (only public elements are visible) Interface 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 Information hiding Data and behavior abstraction

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

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

16 Accessors and Mutators
Given: class Fraction Accessors Methods which return the value of an instance variable E.g., getNumerator(), getDenominator() Mutators Mehtods which change the value of an instance variable E.g., setNumerator(), setDenominator() 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
boolean isEven (int num) return type method name 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
Boolean isEven (int num) result is a local variable. It is created each time the method is called, and is destroyed when it finishes executing { boolean result; if (num % 2 == 0) result = true; else result = false; return result; } The return expression must be consistent with the return type

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

21 Example Programs Transactions.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 "Based on Java Software Development, 5th Ed. By Lewis &Loftus"

Similar presentations


Ads by Google