Вы находитесь на странице: 1из 15

Wrapping

up Methods
Todays Subtopic

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Quiz
Quiz 7 40 minutes

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

When do we use staOc methods? (1)


When we want to implement funcOonality that relates to the class as a whole rather parOcular instances of the class Example from last class:

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

When do we use staOc methods? (2)


StaOc methods are also useful as helper methods to main they help you break up the funcOonality of main to prevent the main method from becoming too long Example from Long Assignment 2 on next page:
The main method of TestTicTacToeBoardB called makeMove() and checkWinner(), which were both staOc helper methods

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

When do we use staOc methods? (3)


StaOc methods are also useful when you need a method that does not have a relaOon to any object See the example on the next page a class that has only staOc methods for doing arithmeOc

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Some things to remember


A constructor is a method that is used to create an object A constructor does not have a return value Methods may call other methods Constructors may call other methods in the class Constructors may call other constructors

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Methods may call other methods


Example: In the Basket class, listContents() calls isEmpty()

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Constructors may call other methods


Example: Heres an alternaOve deniOon of the constructor for the Thing class, which calls its set methods

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

10

Constructors may call other constructors


This is done by using the keyword this as a method Example: I have dened 3 constructors for the Thing class, and the rst two call the third one.

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

11

Guidance on how to write methods


Read secOon 6.3 of the textbook Tips:
Decompose your problem into steps/subtasks
Write out your steps (the algorithm) for the task to be achieved in pseudocode

Then convert each step in pseudocode to Java code Make sure that your Java code is arranged in a logical manner Compile your code and address any compiler errors Write a test program that tests your method. Such a program is someOmes called a driver TesOng might reveal some problems in your code, so you might need to go back to one of the previous steps

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

12

Method Overloading
Two methods can have the same name, provided their signatures are dierent this is called method overloading

The setPopulaOon() method is overloaded

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

13

What is a methods signature?


Simply the methods name and the number and types of its parameters

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

14

equals() method
If you are wriOng a method to test whether two objects are equal, always call the method equals no other name is acceptable!
(Certain parts of Java depend on using that method to test to equality between objects)

Example of an equals method for the Town class:

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

15

Вам также может понравиться