Docsity

CSIS 110 Lecture 24: Accessor and Mutator Methods in Java - Prof. Brian F. Hanks, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 โ€“ Lecture 24
Reading: Chapter 4
Accessor Methods
Accessor methods are used to find out information about an object, without modifying
the object. They use the object's instance variables.
Let's look at a method to get the year from a Date: getYear()
public int getYear() {
return year;
}
Accessor methods have a non-void return type and use the return statement.
Draw pic.
Mutator methods
Mutator methods modify an object. The mutator methods that we have seen are defined
with a formal parameter. When the mutator method is called, an argument must be
supplied. The value of the argument is assigned to the formal parameter.
public void setMonth( int m ) {
month = m;
}
Draw Pic.
Some rules about arguments:
- There must be the same number of arguments as formal parameters.
- The arguments are assigned to the formal parameters in order
- The types of the arguments must match the types of the formal parameters
- Arguments may be arbitrary expressions, as long as the type is correct
Constructor
- special method that is invoked when you create a new object
- Must have same name as the class name
- Does not have a return type - implicitly returns an object of the type
- Constructors are used to initialize new objects
- A constructor can have formal parameters
oWhen we create a new object, we can pass arguments to the constructor
that it can use to initialize the object.
oExample: Date( int m, int d, int y )
pf2

Partial preview of the text

Download CSIS 110 Lecture 24: Accessor and Mutator Methods in Java - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 โ€“ Lecture 24

Reading: Chapter 4 Accessor Methods Accessor methods are used to find out information about an object, without modifying the object. They use the object's instance variables. Let's look at a method to get the year from a Date: getYear() public int getYear() { return year; } Accessor methods have a non-void return type and use the return statement. Draw pic. Mutator methods Mutator methods modify an object. The mutator methods that we have seen are defined with a formal parameter. When the mutator method is called, an argument must be supplied. The value of the argument is assigned to the formal parameter. public void setMonth( int m ) { month = m; } Draw Pic. Some rules about arguments:

  • There must be the same number of arguments as formal parameters.
  • The arguments are assigned to the formal parameters in order
  • The types of the arguments must match the types of the formal parameters
  • Arguments may be arbitrary expressions, as long as the type is correct Constructor
  • special method that is invoked when you create a new object
  • Must have same name as the class name
  • Does not have a return type - implicitly returns an object of the type
  • Constructors are used to initialize new objects
  • A constructor can have formal parameters o When we create a new object, we can pass arguments to the constructor that it can use to initialize the object. o Example: Date( int m, int d, int y )

Printing Objects using toString() When we print an int or a String, we use System.out.print (or println). In our Date class, we have a method called printDate that we call when we want to print the Date. It would be nice if we could print a Date easily, using System.out. What happens if I try this? Date d1 = new Date( 3, 4, 2005 ); System.out.println( d1 ); How can I make Java do what I want? I can write a special instance method named toString(). It must be defined liked this: public String toString() { } toString is an accessor method - it must return a String containing the information we want to display. public String toString() { return month + "/" + day + "/" + year; } Let's try this When we ask println to display the value of an object, it checks to see if the object has a toString() method. If it does, it calls that method, and then prints the value that is returned.