Chapter5_2-Methods

Chapter5_2-Methods

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: type of the value must be declared. Returned values can be used by the calling method. Any method can return at most one value. If the method returns nothing, the keyword void must be used as the return type. • The parameters represent a list of variables whose values will be passed to the method for use by the method. • They are optional. • A method that does not accept parameters is declared with an empty set of parameters inside the parentheses. Page 6 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Types of methods • There 3 different criteria defining types of methods: • Modifiers: this criteria is also composed of 3 subcriteria: – Visibility: public or private (or protected in csc 113) – Shared between all instances or not: class member (static) or instance method. – Override able or not (final): to be discussed in CSC 113. • Return type: method with or without (void) return value. • Parameters: with or without parameters. Page 7 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Example of Methods with No-Parameters and No-Return value import java.util.Scanner; public class Course { // Attributes private String studentName; private String courseCode ; private static Scanner input = new Scanner(System.in); //Class att. // Methods public void enterDataFromKeyBoard() { System.out.print (“Enter the student name: ”); studentName = input.next(); System.out.print (“Enter the course code: ”); courseCode = input.next(); } public void displayData() { System.out.println (“The student name is: ” + studentName); System.out.println (“The the course code is: ”+ courseCode); } } Page 8 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Mess...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online