Lecture 4 - Implementing Methods

Lecture 4 - Implementing Methods - Implementing Methods...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 4 Implementing methods Implementing Methods Introduction • a method is a part of a class, and contains a particular set of instructions; • so far all the classes you have written have contained just one method, the main method; • normally, a method will perform a single well-defined task. Examples • a method could perform to calculate the area of a circle; • a method to display a particular message on the screen; • a method to convert a temperature from fahrenheit to celsius. Calling a method • when we get a method to perform its task we say that we are calling the method; • when we call a method, what we are actually doing is telling the program to: – jump to a new place (where the method instructions are stored); – carry out the set of instructions that it finds there; – when it has finished, return and carry on where it left off. Declaring and defining methods • program 4.1 prompts the user to enter his or her first name, family name and town; • each time the prompt is displayed, it is followed by a message. • we have had to type out the two lines that display the confidentiality message three times; • instead we could have written a method ; Declaring and defining methods • program 4.1 prompts the user to enter his or her first name, family name and town; • each time the prompt is displayed, it is followed by a message. • we have had to type out the two lines that display the confidentiality message three times; • instead we could have written a method ; • the body of this method (between the two curly brackets) contains the instructions; • the first line, which declares the method, is called the method header . Explanation of words in method header private • placing the word private in front of the method name means that the method cannot be accessed by any other class. • if you wanted the methods of your class to be used by other classes, you would declare your method as public ; • the method above is here purely to "help" the main method of this class, and so we declare it as private ; •a private method such as this, which is not accessible to other classes, is often referred to as a helper method. static • the meaning of this will not be explained fully until lecture 7; • for now, you just need to know is that this method has to be static , because it is going to be called from another method (that is, the main method) that is also static .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
void • it is possible for a method to send back some information once it terminates; • this particular method does not need to do so; • the word void indicates that the method does not send back any information. displayMessage() • this is the name that we have chosen to give our method; • it is followed by a pair of empty brackets; • if we want to send information into a method we list, in these brackets, the types of data that we are going to send in; • here, however, we do not have to send in any data, and the brackets are left empty. Calling a method
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/20/2008 for the course COMP 17011 taught by Professor Andynaftel during the Fall '07 term at University of Manchester.

Page1 / 6

Lecture 4 - Implementing Methods - Implementing Methods...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online