220 likes | 332 Views
4. Writing Classes. Based on Java Software Development, 5 th Ed. By Lewis &Loftus. Topics. Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields. User-defined Classes. Java program consists of a set of classes .
E N D
4. Writing Classes Based on Java Software Development, 5th Ed. By Lewis &Loftus
Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields
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.
Classes & Objects • Class has • 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()
MAXVALUE, faceValue Classes • A class can contain data declarations and method declarations Data declarations Methoddeclarations
Classes & Objects • Necessary methods • 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
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){…}
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”;}
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).
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.
RollingDice Die faceValue:int main (args:String[]) : void Die()roll():int setFaceValue(intvalue):void getFaceValue():int toString():String UML Diagram • UML (Unified Modeling Language) diagrams show relationships among classes and objects.
Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields
Encapsulation • Two views of a class • 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
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
Visibility Modifiers • Instance variables • 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
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
Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields
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
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
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
Example Programs • Transactions.java • Account.java
Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields