0% found this document useful (0 votes)
19 views17 pages

Writing Java Methods: Msury - Mahunnah@ifm - Ac.tz

This document discusses Java methods, including: - Method declarations specify the code executed when a method is called. Control returns to the calling point after completion. - Accessor and mutator methods retrieve and modify private instance variables, following naming conventions like getX() and setX(). - The toString() method returns a string representation of an object and is called when objects are concatenated or printed. - The Die and RollingDice classes demonstrate methods, with Die having roll(), getFaceValue(), and setFaceValue() methods and RollingDice as a driver program to test Die.

Uploaded by

steward materu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views17 pages

Writing Java Methods: Msury - Mahunnah@ifm - Ac.tz

This document discusses Java methods, including: - Method declarations specify the code executed when a method is called. Control returns to the calling point after completion. - Accessor and mutator methods retrieve and modify private instance variables, following naming conventions like getX() and setX(). - The toString() method returns a string representation of an object and is called when objects are concatenated or printed. - The Die and RollingDice classes demonstrate methods, with Die having roll(), getFaceValue(), and setFaceValue() methods and RollingDice as a driver program to test Die.

Uploaded by

steward materu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture 5

Writing Java Methods

Dr. Mahunnah, M
[email protected]
https://mahunnah.wordpress.com/
Method Declarations
 Let’s now examine method declarations in more detail
 A method declaration specifies the code that will be executed when
the method is invoked (called)
 When a method is invoked, the flow of control jumps to the method
and executes its code
 When complete, the flow returns to the place where the method
was called and continues
 The invocation may or may not return a value, depending on how
the method is defined
Method Control Flow
 If the called method is in the same class, only the method name is
needed
Method Control Flow
 The called method is often part of another class or object
Accessors and Mutators
 Because instance data is private, a class usually provides services
to access and modify data values
 An accessor method returns the current value of a variable
 A mutator method changes the value of a variable
 The names of accessor and mutator methods take the form getX
and setX, respectively, where X is the name of the value
 They are sometimes called “getters” and “setters”
Mutator Restrictions
 The use of mutators gives the class designer the ability to restrict a
client’s options to modify an object’s state
 A mutator is often designed so that the values of variables can be
set only within particular limits
 For example, the setFaceValue mutator of the Die class should
have restricted the value to the valid range (1 to MAX)
Die.java
Die.java
RollingDice.java
RollingDice.java
RollingDice.java Output
The Die Class

 The Die class contains two data values


 a constant MAX that represents the maximum face value
 an integer faceValue that represents the current face value
 The roll method uses the random method of the Math class to
determine a new face value
 There are also methods to explicitly set and retrieve the
The toString Method
 All classes that represent objects should define a toString
method

 The toString method returns a character string that represents


the object in some way

 It is called automatically when an object is concatenated to a string


or when it is passed to the println method
toString syntax
public String toString() {
code that returns a String representing this object;
}

 Method name, return, and parameters must match exactly.

 Example:
// Returns a String representing this Point.
public String toString() {
return "(" + x + ", " + y + ")";
}
Printing Objects
 By default, Java doesn't know how to print objects:

Point p = new Point();


p.x = 10;
p.y = 7;
System.out.println("p is " + p);
// without toString()method p is Point@9e8c34

// better, but cumbersome; p is (10, 7)


System.out.println("p is (" + p.x + ", " + p.y + ")");

// desired behavior
System.out.println("p is " + p); // p is (10, 7)
The toString method
 The toString() method tells Java how to convert an object into a
String
Point p1 = new Point(7, 2);
System.out.println("p1: " + p1);
//the above code is really calling the following:
System.out.println("p1: " + p1.toString());

 Every class has a toString, even if it isn't in your code.


 Default: class's name @ object's memory address (base 16)

Point@9e8c34
Driver Program
 A driver program drives the use of other, more interesting parts of a
program

 Driver programs are often used to test other parts of the software

 The RollingDice class contains a main method that drives the


use of the Die class, exercising its services

You might also like