Defining Classes and Methods
Defining Classes and Methods
Unit 4
Unit 4: Defining Classes and Methods
• Class and Method Definitions
• Objects and References
• Instance Variables
• Setters and Getters
Class and Method
Definitions
UNIT 4: DEFINING CLASSES AND METHODS
Class and Method Definitions
• A class as a blueprint
Class and Method Definitions
Sample
screen
output
Methods
• When you use a method you "invoke" or "call" it
• Two kinds of Java methods
• Return a single item
• Perform some other action – a void method
Sample
screen
output
Boolean-Valued Methods
• Methods can return a value of type boolean
• Use a boolean value in the return statement
Parameters of a Class Type
• When assignment operator used with objects of
class type
• Only memory address is copied
• Similar to use of parameter of class type
• Memory address of actual parameter passed to
formal parameter
• Formal parameter may access public elements of the
class
• Actual parameter thus can be changed by class
methods
Programming Example
• Sample program, listing 4.6
class DemoSpecies
• Note different parameter types and results
class ParametersDemo
• Parameters of a class type versus parameters of a
primitive type
Programming Example
Sample
screen
output
Programming Example
• UML Diagram
Programming Example
setDimension(double , double ): void
• Method to set the length and width of the rectangle.
getLength() const: double
• Method to return the length of the rectangle.
getWidth() const: double
• Method to return the width of the rectangle.
area() const: double
• Method to return the area of the rectangle.
Programming Example
perimeter() const: double
• Method to return the perimeter of the rectangle.
print() const: void
• Method to output the length and width of the rectangle.
rectangleType()
• default constructor; set length and width to zero
rectangleType(double , double )
• constructor with parameters; length = l; width = w;
double length
double width
(optional)
Graphics Supplement
• The Graphics Class
• The init Methods
• Adding Labels to an Applet
The Graphics Class
• An object of the Graphics class represents
an area of the screen
• Instance variables specify area of screen
represented
• When you run an Applet
• Suitable Graphics object created automatically
• This object used as an argument in the paint
method
The Graphics Class
• Some methods in class Graphics
The Graphics Class
• Some methods in class Graphics
Programming Example
• Multiple faces – using a Helping method
class MultipleFaces
Sample
screen
output
The init Method
• Method init may be defined for any applet
• Like paint, method init called
automatically when applet is run
• Method init similar to method main in an
application program
Adding Labels to Applet
• Provides a way to add text to an applet
• When component (such as a label) added to an
applet
• Use method init
• Do not use method paint
Adding Labels to Applet
class LabelDemo
Sample
screen
output
References
• Walter Savitch
• Java : An Introduction to Problem Solving and
Programming 6th ed. Boston, Pearson Education ©
2015